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就消失了。任何人都可以請提出來是怎麼回事錯在這裏..
感謝
那麼,什麼是你的要求是什麼呢?你想旋轉userImage並在畫布上繪製它? –
是的,這是真的..我想旋轉圖像,並繪製圖像「可繪製」,而不是BitMapdrawable ...因爲我在後面處理一些事件,這是必要的... ... – vinzzz