Created
October 16, 2014 04:01
-
-
Save akira-sasaki/7da55450e9916aa09d8d to your computer and use it in GitHub Desktop.
Touch002
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
// タッチ中. | |
else if ( event.getAction() == MotionEvent.ACTION_MOVE) { | |
// X,Y座標の取得. | |
x = (int) event.getX(); | |
y = (int) event.getY(); | |
// 再描画の処理. | |
invalidate(); | |
} | |
// 離した時. | |
else if ( event.getAction() == MotionEvent.ACTION_UP) { | |
// X,Y座標を原点に戻す. | |
x = 0; | |
y = 0; | |
// 再描画の処理. | |
invalidate(); | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment