2012年2月23日木曜日

androidソフトカーソルキーをつくった!

自分の初めてのandroid機HT-03Aには便利なトラックボールがあった。

最近のスマートフォンはタッチ専用になってフリックの苦手な自分にはカーソルを移動に苦労する。

それでソフトカーソルキー(十字キー・方向キー)なるものをつくってみた。

1.カーソルキー
// cx;中心x座標,cy;中心y;座標,cr;半径,cw;キー幅
void cursorKey(float cx,float cy,float cr,float cw){
    paint.setColor(Color.argb(127, 200, 200, 200));
    paint.setStyle(Paint.Style.FILL);
    canvas.drawCircle(cx,cy,cr+1,paint);
    paint.setColor(Color.argb(127, 255, 255, 255));
    Arrow(cx,cy-cr,cx+cw/2,cy-cr+cw,cx-cw/2,cy-cr+cw);    //up
    Arrow(cx,cy+cr,cx+cw/2,cy+cr-cw,cx-cw/2,cy+cr-cw);    //down
    Arrow(cx-cr,cy,cx-cr+cw,cy-cw/2,cx-cr+cw,cy+cw/2);    //left
    Arrow(cx+cr,cy,cx+cr-cw,cy-cw/2,cx+cr-cw,cy+cw/2);    //right
    canvas.drawCircle(cx,cy,cw/2,paint);
}
//三角矢印三点(x1,y1),(x2,y2),(x3,y3)
void Arrow(float x1,float y1,float x2,float y2,float x3,float y3){
   Path path=new Path();
   path.moveTo(x1, y1);
   path.lineTo(x2, y2);
   path.lineTo(x3, y3);
   canvas.drawPath(path, paint);
}

2.呼び出しとタッチイベントの処理
void gMain(){
//十字キー表示・非表示
if(cursorsw==1)cursorKey(cx,cy,cr,cw);
}
//タッチイベント
@Override
public boolean onTouchEvent(MotionEvent event) {
    int touchAction=event.getAction();
    if (touchAction==MotionEvent.ACTION_DOWN) {
        touchDown=true;
        touchX=(int)(event.getX());
        touchY=(int)(event.getY());
    }
       canvas=holder.lockCanvas();
       onTick(canvas);
       holder.unlockCanvasAndPost(canvas);
    return true;
}
//キャンバス
    private void onTick(Canvas canvas) {
    if(touchDown){
    touchDown=false;
            if(cursorsw==1)MenuPlus();
    }
void MenuPlus(){
if((touchX>cx+cw/2&&touchX<cx+cr)&&(touchY>cy-cw/2&&touchY<cy+cw/2)){
��RIGHT処理)
return;
}
if((touchX>cx-cr&&touchX<cx-cw/2)&&(touchY>cy-cw/2&&touchY<cy+cw/2)){
��LEFT処理)

return;
}
if((touchX>cx-cw/2&&touchX<cx+cw/2)&&(touchY>cy+cw/2&&touchY<cy+cr)){
��DOWN処理)

return;
}
if((touchX>cx-cw/2&&touchX<cx+cw/2)&&(touchY>cy-cr&&touchY<cy-cw/2)){
��UP処理)
return;
}
if((touchX>cx-cw/2&&touchX<cx+cw/2)&&(touchY>cy-cw/2&&touchY<cy+cw/2)){
��決定処理)
return;
}
    }



2 件のコメント:

  1. このソフトカーソルキー、すごく欲しいです。
    どうか配布していただけないでしょうか?

    返信削除
  2. サブルーチンにするほどのものでないのでここのをコピー&ペーストで使って頂きたい。

    返信削除