Friday, 30 August 2013

android mark down the values from martix

android mark down the values from martix

as you known matrix in android is a 3X3 matrix. I used it to scare.rotate
or move a image by touch events and record it with a matrix. but now it's
a question the server can't use this matrix directly. so I need to get all
the value change to scale,moving x,y and rotation. but it seem that the
mid point has some problems. here is the test code.two same imageViews in
the same layout. When I touch resizeImage and change it ,the saveImage
must change as same as the resizeImage.
public class MainActivity extends Activity {
private float lastScale;
private PointF lastMidPoint;
private PointF originPoint;
private PointF lastPoint;
private float lastRotation;
private boolean hasMove;
private boolean hasScare;
private boolean hasRotate;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lastMidPoint = new PointF();
originPoint = new PointF();
lastPoint = new PointF();
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
final ImageView saveImage=(ImageView) findViewById(R.id.b);
saveImage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Matrix m=new Matrix();
if(hasScare){
m.postScale(lastScale, lastScale, (lastMidPoint.x),
(lastMidPoint.y));// ¿s·Å
m.postRotate(lastRotation, (lastMidPoint.x),
(lastMidPoint.y));// ÐýÞD
saveImage.setImageMatrix(m);
//Log.e("Óұ߱任","Óұ߱任");
}
if(hasMove){
float moveX = lastPoint.x - originPoint.x;
float moveY = lastPoint.y - originPoint.y;
m.postTranslate(lastPoint.x -
originPoint.x,lastPoint.y - originPoint.y); //move
saveImage.setImageMatrix(m);
//Log.e("ÓÒ±ßÒÆ¶¯","ÓÒ±ßÒÆ¶¯x:"+moveX+" y:"+moveY);
}
hasMove = false;
hasScare = false;
hasRotate = false;
//System.out.println("1111");
saveImage.postInvalidate();
}
});
final ImageView resizeImage=(ImageView) findViewById(R.id.a);
resizeImage.setOnTouchListener(new OnTouchListener() {
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;
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
DisplayMetrics dm = new DisplayMetrics();
MainActivity.this.getWindowManager().getDefaultDisplay().getMetrics(dm);
widthScreen = dm.widthPixels;
heightScreen = dm.heightPixels;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: //°´Ïµĵã
mode = DRAG;
x_down = event.getX();
y_down = event.getY();
originPoint.set(event.getX(), event.getY());
savedMatrix.set(matrix);
Log.e("°´ÏÂx", ""+x_down);
Log.e("°´ÏÂy", ""+y_down);
System.out.println(savedMatrix.toShortString());
break;
case MotionEvent.ACTION_POINTER_DOWN: //ÖÐÐĵã
mode = ZOOM;
oldDist = spacing(event);
oldRotation = rotation(event); //rotation
Log.e("origin mid", mid.x+" "+mid.y);
savedMatrix.set(matrix);
midPoint(mid, event);
Log.e("change mid", mid.x+""+mid.y);
//System.out.println(savedMatrix.toShortString());
break;
case MotionEvent.ACTION_MOVE:
if (mode == ZOOM) {
matrix.set(savedMatrix);
//System.out.println(savedMatrix.toShortString());
float rotation = rotation(event) -
oldRotation; //rotation
lastRotation = rotation;
float newDist = spacing(event);
float scale =( newDist / oldDist);
//scale
lastScale = scale;
Log.e("mid.x", mid.x+"");
Log.e("mid.y",mid.y+"");
lastMidPoint.set(mid.x, mid.y);
matrix.postScale(scale, scale, (mid.x),
(mid.y));// ¿s·Å
matrix.postRotate(rotation, (mid.x),
(mid.y));// ÐýÞD
Log.e("Ðýת½Ç¶È", ""+rotation);
Log.e("Ëõ·Å", ""+scale);
resizeImage.setImageMatrix(matrix);
hasScare = true;
//
resizeImage.postInvalidate();
} else if (mode == DRAG) {
matrix.set(savedMatrix);
lastPoint.set(event.getX(),event.getY());
float moveX = event.getX() - x_down;
float moveY = event.getY() - y_down;
//Log.e("×ó±ßÒÆ¶¯", "x:"+moveX + " y:"+moveY);
matrix.postTranslate(moveX,moveY);// Æ½ÒÆ
resizeImage.setImageMatrix(matrix);
hasMove = true;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
}
//Log.e("last mid", mid.x+" "+mid.y);
return true;
}
// ´¥ÅöÁ½µã¼ä¾àÀë
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);
}
if anyone has any idea please tell me.I'm appreciated.

No comments:

Post a Comment