我正在尝试使用 matrix 旋转位图。然后我试图在 Canvas 上绘制它。但它只是消失了!
下面是我的代码:
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 就消失了。谁能建议这里出了什么问题..
谢谢