【问题标题】:Android rotate Bitmap and draw CanvasAndroid旋转Bitmap并绘制Canvas
【发布时间】:2013-11-04 05:04:51
【问题描述】:

我正在尝试使用 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 就消失了。任何人都可以建议这里出了什么问题..

谢谢

【问题讨论】:

  • 那么,您的具体要求是什么?你想旋转 userImage 并在 Canvas 上绘制它吗?
  • 是的,那是真的..我想旋转图像并绘制图像“可绘制”而不是 BitMapdrawable...因为我稍后在该可绘制对象上处理一些事件,这是必要的...跨度>

标签: android matrix bitmap rotation


【解决方案1】:

所以我发现了问题所在.. 旋转位图运行良好,但由于我没有在可绘制对象上设置边界,因此“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...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 2016-12-06
    • 2017-02-01
    • 2013-08-18
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多