Skip to content

Instantly share code, notes, and snippets.

@paveldudka
Last active June 5, 2018 19:33
Show Gist options
  • Save paveldudka/cb74124d614bc0da76f1238f475f2fe8 to your computer and use it in GitHub Desktop.
Save paveldudka/cb74124d614bc0da76f1238f475f2fe8 to your computer and use it in GitHub Desktop.
public class SquareBitmapView extends View {
int backgroundColor;
public SquareBitmapView(Context context) {
this(context,null);
init(null);
}
public SquareBitmapView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
init(attrs);
}
public SquareBitmapView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
init(attrs);
}
public SquareBitmapView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(attrs);
}
private void init(@Nullable AttributeSet attrs) {
TypedArray a = getContext().getTheme().obtainStyledAttributes(
attrs,
R.styleable.SquareBitmapView,
0, 0);
backgroundColor = a.getColor(R.styleable.SquareBitmapView_backgroundColor, 0xFFFF0000);
}
@Override public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case ACTION_UP:
handleViewClick();
case ACTION_CANCEL:
cancelGesture();
default:
}
return false;
}
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(backgroundColor);
Bitmap bmp = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.small_image);
Rect destination = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
canvas.drawBitmap(bmp, null, destination, null);
}
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
//make it square
width = Math.min(width, height);
height = Math.min(width, height);
}
private void cancelGesture() {...}
private void handleViewClick() {...}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment