|
テクパー2020 テクニカルヘルパー |
アンドロイドアプリ開発 |
|
◆ タッチイベントの取得 ・ 画面タッチのイベントを取得(処理)します |
3.生成された 「 TouchActionView 」 を書き換え
package com.proto.touchaction;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
public class TouchActionView extends View {
private int touchX=0; // タッチX座標
private int touchY=0; // タッチY座標
private int touchAction=-999; // タッチアクション
private int ballX=0; // ボールX座標
private int ballY=0; // ボールY座標
private int ballAction=-999; // ボールアクション
private int cnt=0; // 描画カウント
// コンストラクタ
public TouchActionView(Context context) {
super(context);
// 背景色の指定 (android.graphics.Colorの定義)
setBackgroundColor(Color.WHITE);
// フォーカス指定(タッチ操作受け取り)
setFocusable(true);
}
//描画
@Override
protected void onDraw(Canvas canvas) {
String str;
// 描画オブジェクトの生成
Paint paint=new Paint();
paint.setAntiAlias(true); // 文字の縁を滑らかに描く
paint.setTextSize(18); // 文字サイズ
paint.setColor(Color.MAGENTA); // 文字色
// 描画カウント
++cnt;
canvas.drawText("DrowCount="+cnt,0,20,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("タッチ操作: "+str,0,20*3,paint);
// タッチ座標の描画
canvas.drawText("タッチ座標:X="+touchX+", Y="+touchY,0,20*4,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("トラックボール操作: "+str,0,20*6,paint);
// トラックボール座標の描画
canvas.drawText("トラックボール座標:X="+ballX+", Y="+ballY,0,20*7,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;
}
}
|
package com.proto.touchaction;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
//import android.view.Window;
public class TouchAction extends Activity implements Runnable {
private TouchActionView touchactionView;
private Handler handler;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ウィンドウタイトルの非表示
// requestWindowFeature(Window.FEATURE_NO_TITLE);
// setContentView(R.layout.main); 書き換え
touchactionView=new TouchActionView(this);
setContentView(touchactionView);
}
// アプリの再開
@Override
public void onResume() {
super.onResume();
handler=new Handler();
handler.postDelayed(this,0);
}
// アプリの一時停止
@Override
public void onPause() {
super.onPause();
handler.removeCallbacks(this);
handler=null;
}
// 実行アプリ
public void run() {
touchactionView.invalidate();
if (handler!=null) handler.postDelayed(this,100);
}
}
|
public class TouchAction extends Activity implements Runnable {
implements Runnable の追加
|
android.view.View のViewクラスでフォーカスの指定 // フォーカス指定 (true:フォーカス取得の有効) public void setFocusable (boolean focusable) |
android.view.MotionEvent のMotionEventクラスのコンスタント // アクション情報 MotionEvent.ACTION_XXXX |
android.view.View のViewクラスでイベントの処理 // タッチ画面のイベント処理 public boolean onTouchEvent (MotionEvent event) |
android.view.View のViewクラスでイベントの処理 // トラックボールのイベント処理 public boolean onTrackballEvent (MotionEvent event) |
| Copyright (C) 2010 プログラミングのテクニックをあなたに!!(リトル・ヘルパー) All Rights Reserved. |