将View对象拖离屏幕并将其替换为另一个

时间:2015-01-01 17:01:36

标签: android object view gesture drag

我有ImageButton并且已经实现了用于收听触摸事件和检测手势的界面。我想知道我必须实现哪种具体方法才能将我的ImageButton拖到屏幕外(让我觉得它看不见),然后用另外一个替换它。我认为它应该是onFling()onScroll(),但我不知道如何编码它。这是代码和图像:

public class fourthActivity extends ActionBarActivity implements View.OnClickListener {

    private ImageButton ib;
    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fourth_activity);

        // listen to touch events
        ib = (ImageButton) findViewById(R.id.ib3);
        ib.setOnClickListener(this);
        gestureDetector = new GestureDetector(this,new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View view,MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        };
        ib.setOnTouchListener(gestureListener);
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(fourthActivity.this,"Hello",Toast.LENGTH_SHORT).show();
    }

    class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onDown (MotionEvent event) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return true;
        }
    }

0 个答案:

没有答案