简体   繁体   English

Android Studio-如何将onFling应用于imageView?

[英]Android Studio - How to apply onFling to an imageView?

I'm pretty new to developing apps, so at the moment I'm just trying to learn the basics. 我对开发应用程序还很陌生,因此目前我只是在尝试学习基础知识。

So far my code runs and if I swipe the screen the image moves (which is what I want). 到目前为止,我的代码已运行,并且如果我滑动屏幕,图像就会移动(这就是我想要的)。 However, at the moment the image moves if I swipe anywhere on the screen, but I only want it to move when I swipe the image. 但是,目前,如果我在屏幕上的任意位置滑动图像,图像就会移动,但是我只希望在滑动图像时图像才移动。

Here's my Main Activity code: 这是我的主要活动代码:

package com.colourdrop.swipeattempt;

import android.os.Bundle;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


public class MyActivity extends AppCompatActivity implements
        GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{

    private TextView text;
    private ImageView image;
    private GestureDetectorCompat gestureDetector;

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

        text = (TextView) findViewById(R.id.textView);
        image = (ImageView) findViewById(R.id.image);
        this.gestureDetector = new GestureDetectorCompat(this, this);
        gestureDetector.setOnDoubleTapListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public void update(View v){
        text.setText("You pressed the button!");
    }

    ////// BEGIN GESTURES //////
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        text.setText("onSingleTapConfirmed");
        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        text.setText("onDoubleTap");
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        text.setText("onDoubleTapEvent");
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        text.setText("onDown");
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        text.setText("onShowPress");

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        text.setText("onSingleTapUp");
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        text.setText("onScroll");
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        text.setText("onLongPress");
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        text.setText("onFling");
        image.setX(image.getX()+200);

        return false;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.gestureDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }
}

Instead of delegating the Activity's onTouchEvent to the GestureDetector , simply delegate your imageView's onTouch . 无需将Activity的onTouchEventGestureDetector ,只需委托onTouchonTouch

image.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM