I am trying to rotate a bitmap using the code below.
The code works fine on emulator (although the infinite rotation has a small lag but that’s another story) but when tested on the actual device the rotation is wrong (wrong pivot I think).
I appreciate any help.
G
public class CircleAnimation extends View {
Bitmap bitmap;
Paint paint;
RotateAnimation rotate;
AlphaAnimation blend;
ScaleAnimation scale;
AnimationSet spriteAnimation;
float centerX;
float centerY;
float offsetX;
float offsetY;
public CircleAnimation(Context context) {
super(context);
// TODO Auto-generated constructor stub
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tile);
offsetX = bitmap.getWidth() / 2;
offsetY = bitmap.getHeight() / 2;
paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
}
@Override
protected void onDraw(Canvas canvas){
//Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tile);
//canvas.drawBitmap(myBitmap, 0, 0, null);
super.onDraw(canvas);
if (spriteAnimation == null) {
centerX = canvas.getWidth() / 2;
centerY = canvas.getHeight() / 2;
createAnimation(canvas);
}
canvas.drawBitmap(bitmap, centerX - offsetX, centerY - offsetY, paint);
}
private void createAnimation(final Canvas canvas) {
rotate = new RotateAnimation(0, 360, centerX, centerY);
rotate.setRepeatCount(Animation.INFINITE);
rotate.setInterpolator(new LinearInterpolator());
rotate.setStartOffset(0);
spriteAnimation = new AnimationSet(true);
spriteAnimation.addAnimation(rotate);
spriteAnimation.setDuration(1000);
startAnimation(spriteAnimation);
}
}
Ok I found the problem.
I had set the minimum SDK to 8 and target SDK to 8 as well. And I was testing on 15.
Changed the target to 15 and it worked.
:p
G