下面的代碼用於以相同的速度旋轉兩個圖像,我想以不同的速度旋轉兩個圖像,我想在我的應用程序中使用兩個畫布,如何在此應用程序中添加兩個畫布以便(canvas.rotate(方向,寬度/ 2,高度/ 2);)我可以以不同的速度旋轉兩個圖像。謝謝以不同的速度旋轉兩個圖像
public class Rotate extends ImageView {
Paint paint;
int direction = 0;
int degree = 0;
int rot;
private float x;
private float y;
private float centerX;
private float centerY;
private float newX;
private float newY;
boolean rotation = false;
private Bitmap mBitmap;
private Bitmap mWood;
private Bitmap icon, mBitmap1, rollerDesk;
private Canvas ball2Rotate;
private Bitmap ball;
Bitmap rollerBitmap;
public Rotate(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(2);
paint.setStyle(Style.STROKE);
Options opts = new Options();
opts.inDither = true;
opts.inPreferredConfig = Bitmap.Config.RGB_565;
mWood = BitmapFactory.decodeResource(getResources(),
R.drawable.newwood, opts);
Bitmap ball = BitmapFactory.decodeResource(getResources(),
R.drawable.roller);
mBitmap = Bitmap.createScaledBitmap(ball, 50, 50, true);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.ball, opts);
mBitmap1 = Bitmap.createScaledBitmap(icon, 50, 50, true);
rollerBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.imagesnic, opts);
rollerDesk = Bitmap.createScaledBitmap(rollerBitmap, 400, 400, true);
this.setImageResource(R.drawable.casinonic);
}
@Override
protected void onDraw(Canvas canvas) {
int height = this.getHeight();
int width = this.getWidth();
ball2Rotate = canvas;
final Bitmap bitmap = mBitmap;
final Bitmap bitmap1 = mBitmap1;
centerX = width/2;
centerY = height/2;
canvas.drawBitmap(mWood, 0, 0, null);
canvas.drawBitmap(bitmap, width/2, height/2, paint);
ball = bitmap1;
// rotateball(canvas, bitmap1);
canvas.rotate(direction, width/2, height/2);
// canvas.rotate(direction, width/2, height/2);
super.onDraw(canvas);
this.invalidate();
canvas.drawBitmap(rollerDesk, width/12, height/4, paint);
canvas.drawBitmap(bitmap1, 220, 160, paint);
if (rotation) {
direction += rot;
rot = rot + 1;
Log.d("Direction values in ONDRAW" + direction, "Rotation values "
+ rot);
if (rot == 100) {
rotation = false;
}
}
}
private void rotateball(Canvas canvas, Bitmap bitmap1) {
canvas.drawBitmap(bitmap1, newX + 220, newY + 400, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
x = event.getX();
y = event.getY();
System.out.println("x and y coordinates" + x + y);
newX = centerX - x;
newY = centerY - y;
rotation = false;
updateRotation(newX, newY);
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
x = event.getX();
y = event.getY();
newX = centerX - x;
newY = centerY - y;
rotation = false;
updateRotation(newX, newY);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
x = event.getX();
y = event.getY();
newX = centerX - x;
newY = centerY - y;
rot = 0;
rotation = true;
updateRotation(newX, newY);
}
return true;
}
private void setDirection1(int degree2) {
for (int j = 0; j < 10; j++) {
this.direction = degree2;
degree2 += 5;
}
this.invalidate();
}
private void updateRotation(float newX2, float newY2) {
degree = (int) Math.toDegrees(Math.atan2(newY, newX));
setDirection1(degree);
}
}
我想同時旋轉兩個圖像 – Sharana