Skip to content

Instantly share code, notes, and snippets.

@vinaysshenoy
Last active August 5, 2017 17:47
Show Gist options
  • Save vinaysshenoy/fde43f25feec2baeeac6fd61f7f3a4da to your computer and use it in GitHub Desktop.
Save vinaysshenoy/fde43f25feec2baeeac6fd61f7f3a4da to your computer and use it in GitHub Desktop.
Custom view for playing with matrices
package com.vinaysshenoy.multitouch.widget.matrixtest;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import static com.vinaysshenoy.multitouch.Utils.dpToPx;
public class MatrixTestView extends View {
//Total drawing area available to the View
private RectF drawRect;
//The content rect that will be drawn
private RectF content;
//The rect to hold the transformed content rect
private RectF mappedContent;
private Paint contentPaint;
private Paint centerMarkerPaint;
private float centerMarkerRadius;
//Matrix to hold the transformations
private Matrix contentTransformMatrix;
public MatrixTestView(Context context) {
super(context);
init(context, null);
}
public MatrixTestView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public MatrixTestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
public MatrixTestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, AttributeSet set) {
drawRect = new RectF();
content = new RectF();
mappedContent = new RectF();
contentPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
contentPaint.setStyle(Paint.Style.FILL);
contentPaint.setStrokeWidth(dpToPx(1.0F));
contentPaint.setColor(Color.LTGRAY);
centerMarkerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
centerMarkerPaint.setStyle(Paint.Style.FILL);
centerMarkerPaint.setStrokeWidth(dpToPx(1.0F));
centerMarkerPaint.setColor(Color.BLACK);
centerMarkerRadius = dpToPx(8F);
contentTransformMatrix = new Matrix();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (w > 0 && h > 0) {
drawRect.set(0, 0, w, h);
//Initialize some value to the content rect
content.set(drawRect);
content.inset(w / 3, h / 3);
//Apply the matrix transform on the content rect and store it in the mapped rect
contentTransformMatrix.mapRect(mappedContent, content);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (drawRect.width() > 0F && drawRect.height() > 0F) {
canvas.drawRect(mappedContent, contentPaint);
canvas.drawCircle(drawRect.centerX(), drawRect.centerY(), centerMarkerRadius, centerMarkerPaint);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment