2011年3月8日火曜日

Androidその16 ~タッチイベントの処理~

どうも、69です。

移転後最初の
今日はタッチイベントの処理です。
この辺が終われば、ちょっとアプリっぽいの
作りたいと思います。


では、早速↓

本日用意するのは


TouchEx.java
TouchView,java


の二つです。

*****TouchEx.java*********************************


package info.webry.at.androidid.touchex;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;

public class TouchEx extends Activity {
    /** Called when the activity is first created. */

private TouchView touchView;
private TickHandler tickHandler;
    //アプリの初期化
    @Override
    public void onCreate(Bundle android) {
        super.onCreate(android);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        touchView=new TouchView(this);
        setContentView(touchView);
    }
  
    //アプリの再開
    @Override
    public void onResume(){
     super.onResume();
     tickHandler = new TickHandler();
     tickHandler.sleep(0);
    }
  
    //アプリの一時停止
    @Override
    public void onPause(){
     super.onPause();
     tickHandler=null;
    }
  
    //定期処理ハンドラ
    public class TickHandler extends Handler{
     //ハンドルメッセージ
     @Override
     public void handleMessage(Message msg){
     touchView.invalidate();
     if(tickHandler != null)tickHandler.sleep(100);
     }
    
     //スリープ
     public void sleep(long delayMills){
     removeMessages(0);
     sendMessageDelayed(obtainMessage(0),delayMills);
     }
    }
}


*************************************************

*****TouchView.java*******************************


package info.webry.at.androidid.touchex;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.view.MotionEvent;

//タッチイベントの処理
public class TouchView extends View{
private int touchX=0;
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);
}

@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";
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);
}

//タッチイベントの処理
@Override
public boolean onTouchEvent(MotionEvent event){
touchX=(int)event.getX();
touchY=(int)event.getY();
touchAction=event.getAction();
return true;
}

//トラックボールイベントの処理
@Override
public boolean onTrackballEvent(MotionEvent event){
ballX=(int)(event.getX()*100);
ballY=(int)(event.getY()*100);
ballAction=event.getAction();
return true;
}

}


*************************************************

解説はまた次回。

おしまい。

0 件のコメント:

コメントを投稿