在我的应用程序中,我有一个长长的绿草位图。我必须修复位图,使其不会移动其底部,并且应将其放置在屏幕的左下角。所以我必须将其枢轴固定在位图的底部中心。之后,我应该将位图(如草随风移动)向左移动,一些像素和向右移动,一些像素距离。但位图不应移动其枢轴位置。我无法固定枢轴位置并实现草的移动。我正在发布我已经完成的代码。请帮我解决这个问题。
private float direction = 1;
private float mangle = 0;
Bitmap green_1 = BitmapFactory.decodeResource(getResources(),
R.drawable.green_1);
private void drawPaper(Canvas canvas){
Paint paint=new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
if (mangle >= 3) {
direction=-0.2f;
} else if (mangle<=-3) {
direction=0.2f;
}
mangle = mangle + direction;
Matrix matrix = new Matrix();
matrix.postTranslate(0,heightOfCanvas-green_1.getHeight());
matrix.postTranslate(0, mangle);
canvas.drawBitmap(green_1, matrix, paint);
}
提前谢谢。
只需使用以下方法旋转画布:
canvas.rotate(deg, xpivot, ypivot)
参见:画布#旋转(浮点,浮点,浮点)