如何在Android中用手指手势交换Imageview和TextView

时间:2012-02-23 10:08:02

标签: android android-animation android-gesture

我有一个包含ImageViewTextView布局的活动。 ImageView TextvVew分别用于显示人物的图像和姓名。我需要从右到左交换,相反用户手指手势。同时,必须相应地更改名称和图像。 有没有办法实现这个?如果有人能够让我知道一个关于这个的教程,那将是很好的,因为我是Android开发的新手。

谢谢。

我尝试了以下方法,但它没有检测到任何手势。

MyGroupActivity.java

import goldenOld.pkg.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.widget.Toast;

public class MyGroupActivity extends Activity implements OnGestureListener{

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.tab_mygroup);
}

public boolean onDown(MotionEvent arg0) {
    // TODO Auto-generated method stub
    return false;
}

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    Toast mToast2 =  Toast.makeText(getApplicationContext(), "Fling", Toast.LENGTH_SHORT);
    mToast2.show();
    try {
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
            return false;
        // right to left swipe
        if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
        }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
        }
        else if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            Toast.makeText(getApplicationContext(), "Swipe up", Toast.LENGTH_SHORT).show();
        }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            Toast.makeText(getApplicationContext(), "Swipe down", Toast.LENGTH_SHORT).show();
        }
    } catch (Exception e) {
        // nothing
        Toast mToast =  Toast.makeText(getApplicationContext(), "Exception", Toast.LENGTH_SHORT);
        mToast.show();
    }

    return true;

}

public void onLongPress(MotionEvent arg0) {
    Toast mToast =  Toast.makeText(getApplicationContext(), "Long Press", Toast.LENGTH_SHORT);
    mToast.show();
}

public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
        float arg3) {
    // TODO Auto-generated method stub
    return false;
}

public void onShowPress(MotionEvent arg0) {
    // TODO Auto-generated method stub

}

public boolean onSingleTapUp(MotionEvent arg0) {
    Toast mToast =  Toast.makeText(getApplicationContext(), "Single Tap", Toast.LENGTH_SHORT);
    mToast.show();
    return true;
}

public class MyGestureDetector extends SimpleOnGestureListener {

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Toast mToast =  Toast.makeText(getApplicationContext(), "Fling", Toast.LENGTH_SHORT);
        mToast.show();
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;

            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                //do what you want on left swipe
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                //do what you want on right swipe
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }

    public boolean onSingleTapUp(MotionEvent arg0) {
        Toast mToast =  Toast.makeText(getApplicationContext(), "Single Tap", Toast.LENGTH_SHORT);
        mToast.show();
        return true;
    }

}

}

2 个答案:

答案 0 :(得分:5)

我用它来进行手势检测

public class MyGestureDetector extends SimpleOnGestureListener {

        private static final int SWIPE_MIN_DISTANCE = 120;
        private static final int SWIPE_MAX_OFF_PATH = 250;
        private static final int SWIPE_THRESHOLD_VELOCITY = 200;
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;

                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    //do what you want on left swipe
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    //do what you want on right swipe
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }

    }

答案 1 :(得分:1)

您必须使用手势检测器。请参阅以下示例:

另请注意,onFling()是回调方法,当您的活动检测到任何手势时,该方法将被调用。所以这里只有你必须编写检测用户滑动方向的逻辑。