package com.androidGui; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; public class AndroidGui extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new SampleView(this)); } private class SampleView extends View { private Paint mPaint; private float imageX; private float imageY; private Bitmap mBitmap; public SampleView(Context context) { super(context); // TODO 自動生成されたコンストラクター・スタブ mPaint = new Paint(); } @Override protected void onDraw(Canvas canvas) { // 画像表示 mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon); canvas.drawBitmap(mBitmap, imageX - mBitmap.getWidth() / 2, imageY - mBitmap.getHeight() / 2, mPaint); } @Override public boolean onTouchEvent(MotionEvent event) { // 触る if (event.getAction() == MotionEvent.ACTION_DOWN) { imageX = event.getX(); imageY = event.getY(); } // 触ったままスライド else if (event.getAction() == MotionEvent.ACTION_MOVE) { imageX = event.getX(); imageY = event.getY(); } // 離す else if (event.getAction() == MotionEvent.ACTION_UP) { imageX = event.getX(); imageY = event.getY(); } // 再描画の指示 invalidate(); return true; } } }
ダウンロード
0 件のコメント:
コメントを投稿