2

好みの画面にジェスチャーを追加しようとしていますが、肯定的な結果が得られません。私の質問は、好みの画面にジェスチャーを追加できますか?はいの場合、以下のコードを使用してジェスチャーを好みに追加する方法です(好みに応じて機能しません)しかし、2つのアクティビティを切り替えるために機能しています)

@Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub

        return gestureDetector.onTouchEvent(event);
    }

    SimpleOnGestureListener simpleOnGestureListener
    = new SimpleOnGestureListener(){


        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            String swipe = "";
            float sensitvity = 50;

            // TODO Auto-generated method stub
            if((e1.getX() - e2.getX()) > sensitvity){
                // for left
            Intent i=new Intent(getApplicationContext(),MyActivity.class);
                startActivity(i);
                finish();


            }else if((e2.getX() - e1.getX()) > sensitvity){
                //for right
            }else{
                swipe += "\n";
            }

            if((e1.getY() - e2.getY()) > sensitvity){
                //Swipe Up
            }else if((e2.getY() - e1.getY()) > sensitvity){
                //Swipe Down
            }



            return super.onFling(e1, e2, velocityX, velocityY);
        }
    };

    GestureDetector gestureDetector
    = new GestureDetector(simpleOnGestureListener);
4

1 に答える 1

0

最初にMainActivityを作成し、implements OnGestureListener 次にジェスチャーを定義します

    private GestureDetector gestureScanner;

onCreateにジェスチャを追加します。gestureScanner = new GestureDetector(this); これがジェスチャのメソッドです

@Override
public boolean onTouchEvent(final MotionEvent event)
{
    return gestureScanner.onTouchEvent(event);
}
public boolean onDown(final MotionEvent e)
{

    return true;
}
public boolean onFling(final MotionEvent e1, final MotionEvent e2,
        final float velocityX, final float velocityY)
{
    return true;
}
public void onLongPress(final MotionEvent e) {

}
public boolean onScroll(final MotionEvent e1, final MotionEvent e2,
        final float distanceX, final float distanceY)
{
    return true;
}
public void onShowPress(final MotionEvent e)
{

}
public boolean onSingleTapUp(final MotionEvent e)
{
    showHide(); // show hide the statusBar On Small Phones prefer to
    // keep it on Single Tab Cuz On Swipe Down is Kind laggy!!!
    // WTF !! :D
    return true;
}

ご覧onSingleTapUp(final MotionEvent e)のとおり、ステータスバーを非表示にするために使用しています。

タッチイベントには6つの異なる方法があります。「ジェスチャーイベント」では、そのうちの1つを選択してインテントを設定し、ユーザーが「言わせて」スクロールすると、他のアクティビティが起動します。

public boolean onScroll(final MotionEvent e1, final MotionEvent e2,
        final float distanceX, final float distanceY)
{
        Intent i=new Intent(getApplicationContext(),MyActivity.class);
            startActivity(i);
            finish();

    return true;
}

それを試してみてください、そして私はそれがあなたに利益をもたらすことを願っています。

于 2013-02-23T11:09:15.673 に答える