Android †開発 †画像の回転描画 †通常 †
xmlで回転 †
matrixで回転 †Drawable d = getContext().getResources().getDrawable(R.drawable.chick); d.setBounds(100, 100, 100 + d.getIntrinsicWidth(), 100 + d.getIntrinsicHeight()); int sc = canvas.save(); Rect r = d.getBounds(); Matrix matrix = new Matrix(); matrix.postRotate(180); matrix.postTranslate(r.left + r.right, r.top + r.bottom); canvas.concat(matrix); d.draw(canvas); canvas.restoreToCount(sc); 画像をアニメーション †viewではなく、drawableをアニメーションした。 移動 †タッチから1秒間かけて右下に移動させる。 public class TestView extends View {
private Animation animation;
private Drawable drawable;
public TestView(Context context, AttributeSet attrs) {
super(context, attrs);
requestFocus();
setClickable(true);
drawable = getContext().getResources().getDrawable(R.drawable.chick);
drawable.setBounds(100, 100, 100 + drawable.getIntrinsicWidth(), 100 + drawable.getIntrinsicHeight());
}
@Override
public boolean onTouchEvent(android.view.MotionEvent event) {
if (event.getAction() != MotionEvent.ACTION_DOWN) {
return false;
}
animation = new TranslateAnimation(0, 100, 0, 100);
animation.setDuration(1000);
animation.setRepeatCount(0);
animation.initialize(10, 10, 10, 10);
animation.startNow();
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
TestView.this.animation = null;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
});
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
if (animation == null) {
drawable.draw(canvas);
return;
}
int sc = canvas.save();
Transformation transformation = new Transformation();
animation.getTransformation(AnimationUtils.currentAnimationTimeMillis(), transformation);
canvas.concat(transformation.getMatrix());
drawable.draw(canvas);
canvas.restoreToCount(sc);
invalidate();
}
}
回転 †タッチから1秒間かけて180度反転させる。 animation = new RotateAnimation(0, 180, Animation.ABSOLUTE, drawable.getBounds() .exactCenterX(), Animation.ABSOLUTE, drawable.getBounds().exactCenterY()); フェードイン †タッチして1秒間かけてフェードインする。 animationのコンストラクタ部分を以下のように変更。 animation = new AlphaAnimation(1, 0); onDrawの以下の部分を変更。 // canvas.concat(transformation.getMatrix()); drawable.setAlpha((int) (transformation.getAlpha() * 255)); |