在我的代码中,图像旋转、缩放和拖动都是成功的。但是当我旋转图像时,它会超出边界。请帮我旋转带边框的图像
我的代码如下
这是我的ondraw
@Override
public void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.save();
canvas.drawBitmap(image, matrix, new Paint());
canvas.drawRect(getBounds() , borderPaint);
canvas.restore();
}
和 touchEvent 方法
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean handled = false;
int x = 0;
int y = 0;
Log.d("SimpImageView", "(int)event.getX() " + (int)event.getX());
Log.d("SimpImageView", "(int)event.getY() " + (int)event.getY());
if(isTouchedInBounds((int)event.getX(), (int)event.getY())){
Log.d("SimpImageView", "Touched in Bounds");
handled = true;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
start.set(event.getX(), event.getY());
lastPosition.x = getBounds().left;
lastPosition.y = getBounds().top;
savedMatrix.set(matrix);
Log.d("VerticalLabelView", "mode=DRAG");
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
mode = ZOOM;
savedMatrix.set(matrix);
oldScale = spacing(event);
midPoint(mid, event);
Log.d("VerticalLabelView", "mode=ZOOM");
oldRotation = rotation(event);
break;
case MotionEvent.ACTION_UP:
mode = NONE;
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d("VerticalLabelView", "mode=NONE");
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
x = (int)(event.getX() - start.x);
y = (int)(event.getY() - start.y);
// setPosition((int)lastPosition.x + x, (int)lastPosition.y + y,getImage().getWidth(),getImage().getHeight());
//matrix.setTranslate(lastPosition.x + x, lastPosition.y + y);
matrix.postTranslate(event.getX() - start.x, event.getY()
- start.y);
float[] values = new float[9];
matrix.getValues(values);
float globalX = values[2];
float globalY = values[5];
float imageWidth = values[0]* image.getWidth();
float imageHeight = values[4] *image.getHeight();
setPosition((int)globalX, (int)globalY, (int)imageWidth, (int)imageHeight);
}else if(mode == ZOOM){
// if(newWidth < 500){
newScale = spacing(event);
newRotation = rotation(event);
angle = newRotation - oldRotation;
//Matrix matrix = new Matrix();
float scale =newScale/oldScale;
int width = image.getWidth();
int height = image.getHeight();
newWidth = (int) (image.getWidth()*scale);
newHeight = (int) (image.getHeight()*scale);
Log.d("new Widht ", "new widht="+image.getWidth()*scale);
matrix.set(savedMatrix);
float scaleWidth = 0;
float scaleHeight;
scaleWidth = ((float) newWidth) / width;
scaleHeight= ((float) newHeight) / height;
matrix.postScale(scaleWidth, scaleHeight,mid.x,mid.y);
matrix.postRotate(angle,mid.x,mid.y);
float[] values = new float[9];
matrix.getValues(values);
float globalX = values[2];
float globalY = values[5];
float imageWidth = values[0]* image.getWidth();
float imageHeight = values[4] *image.getHeight();
setBounds((int)globalX, (int)globalY, (int)imageWidth,(int)imageHeight);
Log.d("VerticalLabelView", "AAAAAAAAAAA==globalX="+globalX);
Log.d("VerticalLabelView", "AAAAAAAAAAA==globalY="+globalY);
Log.d("VerticalLabelView", "AAAAAAAAAAA==width="+width);
Log.d("VerticalLabelView", "AAAAAAAAAAA==height="+height);
Log.d("VerticalLabelView", "AAAAAAAAAAA==values[1]="+values[1]);
Log.d("VerticalLabelView", "AAAAAAAAAAA==values[3]="+values[3]);
Log.d("VerticalLabelView", "AAAAAAAAAAA==values[6]="+values[6]);
Log.d("VerticalLabelView", "AAAAAAAAAAA==values[7]="+values[7]);
}
break;
// }
}
}
/* Log.d("VerticalLabelView", "AAAAAAAAAAA==globalX="+image.getWidth());
Log.d("VerticalLabelView", "AAAAAAAAAAA==globalY="+globalY);
Log.d("VerticalLabelView", "AAAAAAAAAAA==width="+width);
Log.d("VerticalLabelView", "AAAAAAAAAAA==height="+height);*/
return handled;
}