Android Concept | Techbirds

Code to perform Scaling,translation,rotation on canvas in android :

public class Canvas extends View {

Paint paint; Paint paint1;

boolean flag = false;

// new code attributes float x_down = 0; float y_down = 0; PointF start = new PointF(); PointF mid = new PointF(); float oldDist = 1f; float oldRotation = 0; Matrix matrix = new Matrix(); Matrix matrix1 = new Matrix();

Matrix savedMatrix = new Matrix();

private static final int NONE = 0; private static final int DRAG = 1; private static final int ZOOM = 2;

int mode = NONE;

boolean matrixCheck = false;

int widthScreen;
int heightScreen;

Bitmap gintam;
Context context;

// end public Canvas(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; // TODO Auto-generated constructor stub

}

public Canvas(Context context) { super(context); this.context = context; // detector = new ScaleGestureDetector(getContext(), new // ScaleListener());

// TODO Auto-generated constructor stub

// gintama = BitmapFactory.decodeResource(getResources(), // R.drawable.gintama); DisplayMetrics dm = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay()

.getMetrics(dm);

widthScreen = dm.widthPixels;
heightScreen = dm.heightPixels;

matrix = new Matrix();

}

Bitmap background;
Bitmap foreground;

public static int loop = 1;

public Bitmap getViewBackground() { return background;

}

public void setBackground(Bitmap background) { this.background = background; gintam = background; flag = false;

loop = 1;

}

public Bitmap getForeground() {

return foreground;

}

public void setForeground(Bitmap foreground) {
this.foreground = foreground;

// gintam = foreground; flag = true; loop = 0;

}

float t1, t2, t3, t4, w = 100, h = 100;
float k1, k2;

float x = 160, y = 240; int pointer_first; int pointer_id_first; int pointer_second;

int pointer_id_second;

boolean isTransform;
boolean isTranslate = true;

public Canvas(Context context, AttributeSet attrs) { super(context, attrs);

}

float tx, ty, sx, sy; Rect rect; float transx, transy, width, height, scaleX, scaleY, skewX, skewY;

Matrix myNeo;

// Matrix newForeGround;

@Override protected void onDraw(android.graphics.Canvas canvas) {

// TODO Auto-generated method stub

canvas.save();

rect = new Rect(); try { if (flag == false) {

myNeo = new Matrix(matrix);

// if (EditorActivity.bagFlag == false) {

canvas.drawBitmap(gintam, matrix, null); // } else { // canvas.drawBitmap(gintam, myNeo, null); // EditorActivity.bagFlag=false; // } // myNeo = new Matrix(matrix); // float[] values = new float[9]; // matrix.getValues(values); // transx = values[Matrix.MTRANS_X]; // transy = values[Matrix.MTRANS_Y]; // width = values[Matrix.MSCALE_X] * background.getWidth(); // height = values[Matrix.MSCALE_Y] * background.getHeight(); // scaleX = values[Matrix.MSCALE_X]; // scaleY = values[Matrix.MSCALE_Y];

// skewX=values[Matrix.MSKEW_X];

} else if (flag == true) {

// .drawBitmap(background, matrix, paint1); // newForeGround = new Matrix(matrix); int i = 1; canvas.drawBitmap(gintam, myNeo, null); // canvas.restore();

canvas.drawBitmap(foreground, matrix, null);

} } catch (Exception ex) { ex.printStackTrace(); } canvas.restore(); super.onDraw(canvas);

}

@Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: mode = DRAG; x_down = event.getX(); y_down = event.getY(); savedMatrix.set(matrix); break; case MotionEvent.ACTION_POINTER_DOWN: mode = ZOOM; oldDist = spacing(event); oldRotation = rotation(event); savedMatrix.set(matrix); midPoint(mid, event); break; case MotionEvent.ACTION_MOVE: if (mode == ZOOM) { matrix1.set(savedMatrix); float rotation = rotation(event) – oldRotation; float newDist = spacing(event); float scale = newDist / oldDist; matrix1.postScale(scale, scale, mid.x, mid.y);// matrix1.postRotate(rotation, mid.x, mid.y);// matrixCheck = matrixCheck(); if (matrixCheck == false) { matrix.set(matrix1); invalidate(); } } else if (mode == DRAG) { matrix1.set(savedMatrix); matrix1.postTranslate(event.getX() – x_down, event.getY() – y_down);// matrixCheck = matrixCheck(); matrixCheck = matrixCheck(); if (matrixCheck == false) { matrix.set(matrix1); invalidate(); } } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: mode = NONE; break; } return true;

}

private boolean matrixCheck() { float[] f = new float[9]; matrix1.getValues(f); // 4 float x1 = f[0] * 0 + f[1] * 0 + f[2]; float y1 = f[3] * 0 + f[4] * 0 + f[5]; float x2 = f[0] * gintam.getWidth() + f[1] * 0 + f[2]; float y2 = f[3] * gintam.getWidth() + f[4] * 0 + f[5]; float x3 = f[0] * 0 + f[1] * gintam.getHeight() + f[2]; float y3 = f[3] * 0 + f[4] * gintam.getHeight() + f[5]; float x4 = f[0] * gintam.getWidth() + f[1] * gintam.getHeight() + f[2]; float y4 = f[3] * gintam.getWidth() + f[4] * gintam.getHeight() + f[5]; // double width = Math.sqrt((x1 – x2) * (x1 – x2) + (y1 – y2) * (y1 – y2)); // if (width < widthScreen / 3 || width > widthScreen * 3) { return true; } // if ((x1 < widthScreen / 3 && x2 < widthScreen / 3 && x3 < widthScreen / 3 && x4 < widthScreen / 3) || (x1 > widthScreen * 2 / 3 && x2 > widthScreen * 2 / 3 && x3 > widthScreen * 2 / 3 && x4 > widthScreen * 2 / 3) || (y1 < heightScreen / 3 && y2 < heightScreen / 3 && y3 < heightScreen / 3 && y4 < heightScreen / 3) || (y1 > heightScreen * 2 / 3 && y2 > heightScreen * 2 / 3 && y3 > heightScreen * 2 / 3 && y4 > heightScreen * 2 / 3)) { return true; } return false;

}

private float spacing(MotionEvent event) { float x = event.getX(0) – event.getX(1); float y = event.getY(0) – event.getY(1); return FloatMath.sqrt(x * x + y * y);

}

// private void midPoint(PointF point, MotionEvent event) { float x = event.getX(0) + event.getX(1); float y = event.getY(0) + event.getY(1); point.set(x / 2, y / 2);

}

// private float rotation(MotionEvent event) { double delta_x = (event.getX(0) – event.getX(1)); double delta_y = (event.getY(0) – event.getY(1)); double radians = Math.atan2(delta_y, delta_x); return (float) Math.toDegrees(radians);

}

}

546 total views, 1 views today

Share this Onfacebook-6327875twitter-8644951linkedin-8724523google-3352802