2013-11-04 147 views
0

我想旋转使用矩阵的位图。然后我试图在画布上绘制它。但它只是失望!Android旋转位图并绘制画布

这里是我下面的代码:

public class MultitouchView extends View { 

private static final int INVALID_POINTER_ID = -1; 
private static final String TAG = "MultitouchView"; 
private Drawable image; 
private Drawable userImage; 

Bitmap userOriginal; 
Bitmap userResult; 

private float scaleFactor = 1.0f; 
private ScaleGestureDetector scaleGestureDetector; 

private float mPosX; 
private float mPosY; 

private float mLastTouchX; 
private float mLastTouchY; 
private int mActivePointerId = INVALID_POINTER_ID; 

private int direction = 0; 

Context context; 

public MultitouchView(Context context) { 
    super(context); 
    image = context.getResources().getDrawable(
      R.drawable.plain_black_wallpaper); 
    image.setBounds(0, 0, image.getIntrinsicWidth(), 
      image.getIntrinsicHeight()); 

    userOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.navigation); 

    userImage = context.getResources().getDrawable(R.drawable.navigation); 
    userImage.setBounds(500, 500, 550, 550); 

    setFocusable(true); 

    scaleGestureDetector = new ScaleGestureDetector(context, 
      new ScaleListener()); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    // Set the image bounderies 
    canvas.save(); 
    //scale canvas 
    canvas.scale(scaleFactor, scaleFactor); 
    image.draw(canvas); 
    canvas.translate(mPosX, mPosY); 

    //rotate user drawable 

    userImage.draw(canvas); 

    canvas.restore(); 
} 

public void setDirection(int direction) { 
     this.direction = direction; 
     rotateBitmap(); 
     invalidate(); 
     } 

private void rotateBitmap() { 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(this.direction); 
    userResult = Bitmap.createBitmap(userOriginal,0,0, userOriginal.getWidth(), userOriginal.getHeight(), matrix,true); 
    userImage=new BitmapDrawable(this.getResources(),userResult); 

} 

}

我已删除了规模帆布和事件侦听器等功能。使代码小巧易读。

我周期性地调用了setDirection方法,但之后它被称为“userImage”drawable就消失了。任何人都可以请提出来是怎么回事错在这里..

感谢

+0

那么,什么是你的要求是什么呢?你想旋转userImage并在画布上绘制它? –

+0

是的,这是真的..我想旋转图像,并绘制图像“可绘制”,而不是BitMapdrawable ...因为我在后面处理一些事件,这是必要的... ... – vinzzz

回答

0

于是我发现了什么是错误的。该旋转位图被罚款运行,但由于我不设界限上绘制了“userImage “drawable在位置0,0被绘制,高度和宽度为0,0,因此它是DISAPPEARING。我固定,通过改变兴田旋转功能的位图如下图所示:

private void rotateBitmap() { 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(this.direction); 
    userResult = Bitmap.createBitmap(userOriginal,0,0, userOriginal.getWidth(), userOriginal.getHeight(), matrix,true); 
    userImage=new BitmapDrawable(this.getResources(),userResult); 
    userImage.setBounds(500, 500, 550, 550); 

} 

需要落实的userImage.Setbounds ...