Android プログラミング 基本API
雑なメモ
public class StringView extends View implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{ private ArrayList<String>info; private GestureDetector gestureDetector; private Bitmap image; private int keyCode = -999; private HashMap<String,PointF> points = new HashMap<String,PointF>(); public StringView(Context context) { super(context); setBackgroundColor(Color.WHITE); // ジェスチャーディテクターの生成 info = new ArrayList<String>(); info.add("GESTURE>"); gestureDetector = new GestureDetector(context,this); Resources resources = context.getResources(); image = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher); setFocusable(true); // 方向キーやプログラムでフォーカスを移すことができるかどうか指定 setFocusableInTouchMode(true); // タッチモードの時にフォーカスを移すことができるかどうかの指定 } @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setTextSize(48); canvas.drawText("横: " + getWidth(), 0, 100, paint); canvas.drawText("縦: " + getHeight(), 0, 200, paint); // canvas.drawBitmap(image,500,500,null); String str = ""; if(keyCode == KeyEvent.KEYCODE_DPAD_UP) str = "DPAD_UP"; if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN) str = "DPAD_DOWN"; if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT) str = "DPAD_LEFT"; if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) str = "DPAD_RIGHT"; if(keyCode == KeyEvent.KEYCODE_BACK) str = "BACK"; if(keyCode == KeyEvent.KEYCODE_MENU) str = "MENU"; canvas.drawText("KeyCode: " + str, 0, 400, paint); // canvas.drawText("Touch: ",0,600,paint); // タッチXY座標の描画 Object[] keys = points.keySet().toArray(); for (int i=0;i<keys.length;i++){ PointF pos = (PointF)points.get(keys[i]); canvas.drawText((int)pos.x+","+(int)pos.y,0,700+48*i,paint); } // 情報の描画 for (int i=0;i<info.size();i++){ canvas.drawText((String)info.get(i),300,800+48*i,paint); } } @Override public boolean onTouchEvent(MotionEvent event) { // アクション種別とタッチ獲得 int action =event.getAction(); int count = event.getPointerCount(); // アクションインデックスとポインタIDの獲得 int index = event.getActionIndex(); int pointerID = event.getPointerId(index); // タッチ位置の取得 switch(action&MotionEvent.ACTION_MASK){ case MotionEvent.ACTION_DOWN: //指が1本の時 case MotionEvent.ACTION_POINTER_DOWN: //指が2本目以降の時 points.put(""+pointerID,new PointF(event.getX(),event.getY())); break; case MotionEvent.ACTION_MOVE: for(int i=0;i<count;i++){ PointF pos = points.get(""+event.getPointerId(i)); pos.x = event.getX(i); pos.y = event.getY(i); } break; case MotionEvent.ACTION_UP: //指が1本目の時 case MotionEvent.ACTION_POINTER_UP: //指が2本目以降の時 case MotionEvent.ACTION_CANCEL: points.remove(""+pointerID); break; } gestureDetector.onTouchEvent(event); invalidate(); return true; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { this.keyCode = keyCode; // 画面の再描画 invalidate(); return true; } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { this.keyCode = -999; invalidate(); return true; } // 情報の追加 private void addInfo(String str){ info.add(1,str); while(info.size() > 30) info.remove(info.size()-1); invalidate(); } // ダウン時 @Override public boolean onDown(MotionEvent e) { addInfo("Down"); return false; } // プレス後ムーブなし @Override public void onShowPress(MotionEvent e) { addInfo("ShowPress"); } // アップ時 @Override public boolean onSingleTapUp(MotionEvent e) { addInfo("Up"); return false; } // 長押し時 @Override public void onLongPress(MotionEvent e) { addInfo("LongPress"); } // フリック時 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { addInfo("Fling("+(int)velocityX+","+(int)velocityY+")"); return false; } // スクロール時 @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { addInfo("SCroll("+(int)distanceX+","+(int)distanceY+")"); return false; } // シングルタップ時 @Override public boolean onSingleTapConfirmed(MotionEvent e) { addInfo("SingleTap"); return false; } // ダブルタップ時 @Override public boolean onDoubleTap(MotionEvent e) { addInfo("DoubleTap"); return false; } // ダブルタップイベント時 @Override public boolean onDoubleTapEvent(MotionEvent e) { addInfo("DoubleTapEvent"); return false; } }
参考文献
Androidプログラミングバイブル SDK5/4対応 (smart phone programming bible)
- 作者: 布留川英一
- 出版社/メーカー: ソシム
- 発売日: 2015/01/26
- メディア: 単行本
- この商品を含むブログ (2件) を見る