我正在尝试使用矩阵旋转位图。然后我试图在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就会消失。任何人都可以建议这里出了什么问题..
由于
答案 0 :(得分:0)
所以我发现了什么问题..旋转Bitmap运行正常但是因为我没有在drawable上设置边界,所以“userImage”drawable被绘制在位置0,0,高度和宽度为0,0因此它是消失的。我通过改变hte旋转位图函数来修复,如下所示:
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 ...