2012-07-20 02:23:49 +00:00
|
|
|
package org.thoughtcrime.securesms.components;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.Resources;
|
2013-06-21 18:56:59 +00:00
|
|
|
import android.content.res.TypedArray;
|
2012-07-20 02:23:49 +00:00
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
|
|
|
|
public class ImageDivet extends ImageView {
|
|
|
|
private static final float CORNER_OFFSET = 12F;
|
2014-02-19 06:08:24 +00:00
|
|
|
private static final String[] POSITIONS = new String[] {"bottom_right"};
|
2012-07-20 02:23:49 +00:00
|
|
|
|
|
|
|
private Drawable drawable;
|
|
|
|
|
|
|
|
private int drawableIntrinsicWidth;
|
|
|
|
private int drawableIntrinsicHeight;
|
|
|
|
private int position;
|
|
|
|
private float density;
|
|
|
|
|
|
|
|
public ImageDivet(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
initialize(attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ImageDivet(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
initialize(attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ImageDivet(Context context) {
|
|
|
|
super(context);
|
|
|
|
initialize(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initialize(AttributeSet attrs) {
|
|
|
|
if (attrs != null) {
|
|
|
|
position = attrs.getAttributeListValue(null, "position", POSITIONS, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
density = getContext().getResources().getDisplayMetrics().density;
|
|
|
|
setDrawable();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setDrawable() {
|
2014-02-19 06:08:24 +00:00
|
|
|
int attributes[] = new int[] {R.attr.lower_right_divet};
|
2013-06-21 18:56:59 +00:00
|
|
|
|
|
|
|
TypedArray drawables = getContext().obtainStyledAttributes(attributes);
|
2012-07-20 02:23:49 +00:00
|
|
|
|
|
|
|
switch (position) {
|
|
|
|
case 0:
|
2013-06-21 18:56:59 +00:00
|
|
|
drawable = drawables.getDrawable(0);
|
2012-07-20 02:23:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
drawableIntrinsicWidth = drawable.getIntrinsicWidth();
|
|
|
|
drawableIntrinsicHeight = drawable.getIntrinsicHeight();
|
2013-06-21 18:56:59 +00:00
|
|
|
|
|
|
|
drawables.recycle();
|
2012-07-20 02:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDraw(Canvas c) {
|
|
|
|
super.onDraw(c);
|
|
|
|
c.save();
|
|
|
|
computeBounds(c);
|
|
|
|
drawable.draw(c);
|
|
|
|
c.restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPosition(int position) {
|
|
|
|
this.position = position;
|
|
|
|
setDrawable();
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getPosition() {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getCloseOffset() {
|
|
|
|
return CORNER_OFFSET * density;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ImageView asImageView() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getFarOffset() {
|
|
|
|
return getCloseOffset() + drawableIntrinsicHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void computeBounds(Canvas c) {
|
|
|
|
final int right = getWidth();
|
2014-02-19 06:08:24 +00:00
|
|
|
final int bottom = getHeight();
|
2012-07-20 02:23:49 +00:00
|
|
|
|
|
|
|
switch (position) {
|
|
|
|
case 0:
|
|
|
|
drawable.setBounds(
|
2014-02-19 06:08:24 +00:00
|
|
|
right - drawableIntrinsicWidth,
|
|
|
|
bottom - drawableIntrinsicHeight,
|
|
|
|
right,
|
|
|
|
bottom);
|
2012-07-20 02:23:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|