Android

開発

画像の回転描画

通常

  • res/drawableの下に画像ファイルを置く。例:chick.png
  • 描画コードは以下のような感じ
    Drawable d = getContext().getResources().getDrawable(R.drawable.chick);
    d.setBounds(100, 100, 100 + d.getIntrinsicWidth(), 100 + d.getIntrinsicHeight());
    d.draw(canvas);

xmlで回転

  • 以下のようなxmlファイルを作ってres/drawableの下に置く。例:chick_r.png
    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
       android:drawable="@drawable/chick"
       android:fromDegrees="180"
       android:pivotX="50%"
       android:pivotY="50%" 
       android:visible="true"/>
  • 描画コードは以下のような感じ。座標をマイナスにせんといかんらしい。
    Drawable d = getContext().getResources().getDrawable(R.drawable.chick_r);
    d.setBounds(-100, -100, -100 + d.getIntrinsicWidth(), -100 + d.getIntrinsicHeight());
    d.draw(canvas);

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のコンストラクタ部分を以下のように変更。

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));

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2010-04-23 (金) 21:30:18 (760d)
.NET Ajax AmazonWebServices Android C# C++ CMS Cacoo DB Exam Generics Java JavaScript Local Oracle PHP Perl Photo Programming Pukiwiki Python Qt Review Ruby SQLServer Seasar Software Tips Tool VB WebService Windows WindowsMobile XHTML XML XMLMaster XSLT game pdf wkhtmltopdf