どうも、69です。
早速前回 タッチイベントの処理 の解説です。
*****TouchEx.java***************************************
↓TouchExクラスの定義
public class TouchEx extends Activity {
/** Called when the activity is first created. */
private TouchView touchView;→変数touchView。型はTouchView
/** Called when the activity is first created. */
private TouchView touchView;→変数touchView。型はTouchView
private TickHandler tickHandler;→変数tickHandler。型はTickHandler
//アプリの初期化
@Override
public void onCreate(Bundle android) {
super.onCreate(android);
requestWindowFeature(Window.FEATURE_NO_TITLE);
touchView=new TouchView(this);→TouchViewオブジェクトの生成。引数:this(TouchEx)
setContentView(touchView);→実画面に表示するビューの指定
}
//アプリの再開→onResumeメソッドの定義
@Override
public void onResume(){
super.onResume();
tickHandler = new TickHandler();→TickHandlerオブジェクトの生成
tickHandler.sleep(0);→tickHandlerオブジェクトのsleepメソッドを利用
}
//アプリの一時停止→onPauseメソッド定義
@Override
public void onPause(){
super.onPause();
tickHandler=null;→tickHandlerを空(null)に。
}
//定期処理ハンドラ→TickHandlerクラスの定義
public class TickHandler extends Handler{
//ハンドルメッセージ→handlerMessageメソッドの定義
@Override
public void handleMessage(Message msg){
@Override
public void handleMessage(Message msg){
touchView.invalidate();→touchViewオブジェクトのinvalidateメソッドを利用
if(tickHandler != null)tickHandler.sleep(100);→もし「tickHandlerがnull」じゃなかったら「tickHandlerオブジェクトのsleepメソッドを引数100で利用」
}
//スリープ→sleepメソッドの定義
public void sleep(long delayMills){
removeMessages(0);→メッセージの削除
sendMessageDelayed(obtainMessage(0),delayMills);→メッセージの送信
}
}
}
*************************************************
*****TouchView.java*******************************
//タッチイベントの処理→TouchViewクラスの定義
public class TouchView extends View{
}
//スリープ→sleepメソッドの定義
public void sleep(long delayMills){
removeMessages(0);→メッセージの削除
sendMessageDelayed(obtainMessage(0),delayMills);→メッセージの送信
}
}
}
*************************************************
*****TouchView.java*******************************
//タッチイベントの処理→TouchViewクラスの定義
public class TouchView extends View{
↓変数touchX,touchY,touchAction,ballX,ballY,ballActionの宣言、初期値設定
private int touchX=0;
private int touchY=0;
private int touchAction=-999;
private int ballX=0;
private int ballY=0;
private int ballAction=-999;
private int touchY=0;
private int touchAction=-999;
private int ballX=0;
private int ballY=0;
private int ballAction=-999;
//コンストラクタ
public TouchView(Context context){
super(context);
setBackgroundColor(Color.WHITE);
//フォーカス指定
setFocusable(true);→フォーカス有効化
}
public TouchView(Context context){
super(context);
setBackgroundColor(Color.WHITE);
//フォーカス指定
setFocusable(true);→フォーカス有効化
}
//onDrawオブジェクトの定義 @Override
protected void onDraw(Canvas canvas){
String str;
//描画オブジェクトの生成
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(32);
//タッチXY座標の描画
canvas.drawText("TouchXY>"+touchX+","+touchY, 0, 40*1, paint);
//タッチアクションの描画
str="NONE";
protected void onDraw(Canvas canvas){
String str;
//描画オブジェクトの生成
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(32);
//タッチXY座標の描画
canvas.drawText("TouchXY>"+touchX+","+touchY, 0, 40*1, paint);
//タッチアクションの描画
str="NONE";
↓もし「touchActionがMotionEvent.ACTION_~」だったら「strにACTION_~を代入」 if(touchAction==MotionEvent.ACTION_DOWN) str="ACTION_DOWN";
if(touchAction==MotionEvent.ACTION_MOVE) str="ACTION_MOVE";
if(touchAction==MotionEvent.ACTION_UP) str="ACTION_UP";
if(touchAction==MotionEvent.ACTION_CANCEL) str="ACTION_CANCEL";
canvas.drawText("TouchAction>"+str, 0, 40*2, paint);
//ボールXY座標の描画
canvas.drawText("TrackballXY>"+ballX+","+ballY, 0, 40*3, paint);
//ボールアクションの描画
str="NONE";
if(ballAction==MotionEvent.ACTION_DOWN) str="ACTION_DOWN";
if(ballAction==MotionEvent.ACTION_MOVE) str="ACTION_MOVE";
if(ballAction==MotionEvent.ACTION_UP) str="ACTION_UP";
if(ballAction==MotionEvent.ACTION_CANCEL) str="ACTION_CANCEL";
canvas.drawText("TrackballAction>"+str, 0, 40*4, paint);
}
//タッチイベントの処理→onTouchEventの定義
@Override
public boolean onTouchEvent(MotionEvent event){
touchX=(int)event.getX();→x座標の取得
touchY=(int)event.getY();→y座標の取得
touchAction=event.getAction();→Actionの取得
return true;
}
//トラックボールイベントの処理
@Override
public boolean onTrackballEvent(MotionEvent event){
ballX=(int)(event.getX()*100);
ballY=(int)(event.getY()*100);
ballAction=event.getAction();
return true;
}
}
if(touchAction==MotionEvent.ACTION_MOVE) str="ACTION_MOVE";
if(touchAction==MotionEvent.ACTION_UP) str="ACTION_UP";
if(touchAction==MotionEvent.ACTION_CANCEL) str="ACTION_CANCEL";
canvas.drawText("TouchAction>"+str, 0, 40*2, paint);
//ボールXY座標の描画
canvas.drawText("TrackballXY>"+ballX+","+ballY, 0, 40*3, paint);
//ボールアクションの描画
str="NONE";
if(ballAction==MotionEvent.ACTION_DOWN) str="ACTION_DOWN";
if(ballAction==MotionEvent.ACTION_MOVE) str="ACTION_MOVE";
if(ballAction==MotionEvent.ACTION_UP) str="ACTION_UP";
if(ballAction==MotionEvent.ACTION_CANCEL) str="ACTION_CANCEL";
canvas.drawText("TrackballAction>"+str, 0, 40*4, paint);
}
//タッチイベントの処理→onTouchEventの定義
@Override
public boolean onTouchEvent(MotionEvent event){
touchX=(int)event.getX();→x座標の取得
touchY=(int)event.getY();→y座標の取得
touchAction=event.getAction();→Actionの取得
return true;
}
//トラックボールイベントの処理
@Override
public boolean onTrackballEvent(MotionEvent event){
ballX=(int)(event.getX()*100);
ballY=(int)(event.getY()*100);
ballAction=event.getAction();
return true;
}
}
*************************************************
*****主に利用したパッケージ*****************
import android.app.Activity;
void onCreate(Bundle android)→アプリの起動時に呼ばれる。引数:android 呼び出し時に渡されるデータ
void requestWindowFeature(int featureID)→ウィンドウの特徴IDの指定。引数:ID
void setContentView(View view)→実画面に表示するビューの指定。引数:ビュー
import android.os.Handler;
void removeMessage(int ID)→メッセージの削除。引数:メッセージID
boolean sendMessageDelayed(Message msg,long delayMillis)→メッセージの送信。引数:メッセージ,送信までの時間(ミリ秒)
void handleMessage(Message msg)→メッセージ受信。引数:メッセージ
import android.view.View;
boolean onTouchEvent(MotionEvent event)→タッチ発生時に呼ばれる。引数:タッチイベント。戻り値:処理が完了したか
boolean onTrackballEvent(MotionEvent event)→トラックボールイベント発生時に呼ばれる。引数:ボールイベント。戻り値:処理が完了したか
import android.view.MotionEvent;
float getX()→タッチイベントのX座標取得。戻り値:X座標
float getY()→タッチイベントのY座標取得。戻り値:Y座標
int getAction()→タッチイベントのアクション種別の取得。戻り値:種別
**********************************************************
今日のところは
こんなところです。
おしまい。
0 件のコメント:
コメントを投稿