0

我正在尝试使用 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 就消失了。谁能建议这里出了什么问题..

谢谢

4

1 回答 1

0

所以我发现出了什么问题..旋转位图运行良好,但由于我没有在可绘制对象上设置边界,“userImage”可绘制对象被绘制在位置 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 需要实现...

于 2013-11-04T17:04:20.020 回答