旋转图像在android中超出界限

时间:2012-05-30 06:53:30

标签: android

在我的代码中,图像旋转,缩放和拖动都是成功的。但是当我旋转图像时,它会越过边界。请帮我旋转带边框的图片

我的代码如下

这是我的提款

@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;

}

1 个答案:

答案 0 :(得分:1)

尝试使用将图像旋转90度的代码

Matrix matrix = new Matrix();
matrix = collisionImage.getImageMatrix();
//collisionImage is ImageView
Log.i("matrix coordinate rotate 1 collision ", "" + matrix.toString());
Matrix maxMatrix = new Matrix();
maxMatrix.setRotate(ROTATIONByNinety, collisionBitmap.getWidth() / 2, collisionBitmap.getHeight() / 2);
Bitmap newBitmap = Bitmap.createBitmap(collisionBitmap, 0, 0, (int) collisionBitmap.getWidth(), (int) collisionBitmap.getHeight(), maxMatrix, true);
collisionImage.setImageBitmap(newBitmap);
ROTATIONByNinety = ROTATIONByNinety + 90;
if (ROTATIONByNinety == 360) {
    ROTATIONByNinety = 0;
}
// changeImage(newBitmap);
Log.i("maxMatrix coordinate rotate 2 collision", maxMatrix.toString());
Log.i("matrix coordinate rotate 2 collision", matrix.toString());
collisionImage.setImageMatrix(matrix);
collisionImage.invalidate();