我想在多点触控事件中旋转、缩放和移动图像,它似乎工作正常,但工作不完美。我真的很想修复我的错误代码,所以请帮助我。我的代码在那里
public class ImageControl extends Activity {
DragView dragView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dragView = new DragView(this);
setContentView(dragView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.image_control, menu);
return true;
}
class DragView extends ImageView
{
private Bitmap bitmap;
private float width;
private float height;
private float startX=0;
private float startY=0;
private float userX=0;
private float userY=0;
private Paint paint;
private float oldDistance = 1f;
private float newDistance = 1f;
private Rect rect;
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
private int lastAngle=0;
private int thisAngle=0;
private int deltaAngle;
private int angle;
int rotateX, rotateY;
private Matrix mat;
private float x1;
private float y1;
public DragView(Context context){
super(context);
init();
setImage(context);
}
private void init(){
paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);
mat = new Matrix();
}
private void setImage(Context context){
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.android);
width = bitmap.getWidth();
height = bitmap.getHeight();
}
public void onDraw(Canvas canvas){
if(bitmap!=null)
{
canvas.drawBitmap(bitmap, mat, null);
canvas.drawRect(userX, userY, userX+width, userY+height, paint);
}
}
public Rect getRect(){
rect = new Rect();
rect.set((int)userX, (int)userY, (int)(userX+width), (int)(userY+height));
return rect;
}
public void setXY(float x, float y){
startX = x;
startY= y;
}
public boolean onTouchEvent(MotionEvent event){
x1 = event.getX(0);
y1 = event.getY(0);
int act = event.getAction();
switch(act&MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_DOWN:
setXY(x1, y1);
mode=DRAG;
break;
case MotionEvent.ACTION_MOVE:
if(mode==ZOOM){
newDistance = spacing(event);
float scale2 = FloatMath.sqrt(((newDistance-oldDistance)*(newDistance-oldDistance))
/(height*height+width*width));
float scale = newDistance / oldDistance;
if(newDistance - oldDistance > 0){
setZoom(scale, scale2);
}else if(oldDistance - newDistance > 0){
setZoom(scale, -scale2);
}
setRotate(event, x1, y1);
}
else if(mode==DRAG){
setLastXY(x1-startX, y1-startY);
setXY(x1, y1);
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_POINTER_DOWN:
mode = ZOOM;
float value = spacing(event);
oldDistance = value;
newDistance = value;
break;
case MotionEvent.ACTION_CANCEL:
default:
break;
}
return true;
}
private void setLastXY(float x, float y){
userX += x;
userY += y;
mat.setTranslate(userX, userY);
invalidate();
}
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 setZoom(float scale, float scale2){
userY=userY-(height*scale2/2);
userX=userX-(width*scale2/2);
height=height*(1+scale2);
width=width*(1+scale2);
Log.d("ZoomTAG", "scale:"+scale);
mat.postScale(scale, scale, userX+width/2, userY+height/2);
oldDistance = newDistance;
invalidate();
}
private void setRotate(MotionEvent event, float x1, float y1){
float x2 = event.getX(1);
float y2 = event.getY(1);
thisAngle = (int)Math.toDegrees(Math.atan2(-(y2-y1), x2-x1));
if(lastAngle==0){
lastAngle=thisAngle;
}
deltaAngle = thisAngle-lastAngle;
angle += -deltaAngle;
lastAngle = thisAngle;
float minX = Math.min(x1, x2);
float minY = Math.min(y1, y2);
rotateX = (int)minX + (Math.abs((int)(x1-x2))/2);
rotateY = (int)minY + (Math.abs((int)(y1-y2))/2);
Log.d("TAG", "Angle : " + angle);
mat.postRotate(angle, userX+width/2, userY+height/2);
invalidate();
}
}
}