Skip to content

Instantly share code, notes, and snippets.

@akira-sasaki
Last active August 29, 2015 14:07
Show Gist options
  • Save akira-sasaki/cc3d6b6d0e28086395e7 to your computer and use it in GitHub Desktop.
Save akira-sasaki/cc3d6b6d0e28086395e7 to your computer and use it in GitHub Desktop.
package gclue.com.touch001;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// MyViewを画面に設定.
MyView mView = new MyView(this);
setContentView(mView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
/**
* 描画用のクラス.
*/
class MyView extends View {
/** 画像のx座標. */
private int x = 0;
/** 画像のy座標. */
private int y = 0;
/** 画像を格納する変数. */
private Bitmap mTaikoImage;
/**
* コンストラクタ.
*
* @param context コンテキスト
*/
public MyView( Context context ) {
super(context);
setFocusable( true );
// Resourceインスタンスの生成.
Resources res = this.getContext().getResources();
// 画像の読み込み(res/drawable/gclue_logo.gif).
mTaikoImage = BitmapFactory.decodeResource(res, R.drawable.taiko);
}
/**
* 描画処理.
*
* @param canvas 描画用のCanvas
*/
@Override
protected void onDraw( Canvas canvas ) {
super.onDraw( canvas );
// 背景色を設定.
canvas.drawColor( Color.BLUE );
// Paintのインスタンスを作成.
Paint mainPaint = new Paint();
// Bitmapイメージの描画
canvas.drawBitmap( mTaikoImage, x, y, mainPaint );
}
/**
* タッチイベント.
*/
@Override
public boolean onTouchEvent( MotionEvent event ) {
// タッチした時.
if ( event.getAction() == MotionEvent.ACTION_DOWN ) {
// X,Y座標の取得.
x = (int) event.getX();
y = (int) event.getY();
// 再描画の処理.
invalidate();
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment