是否可以根据点击发生的坐标执行事件?

时间:2016-09-09 14:47:02

标签: android android-animation

我尝试使用动画和手势创建应用。到目前为止,我已经获得了手势部分,可以根据手势创建图像视图并使其移动。

但我想要做的是根据点击发生的位置显示某个图像。例如,我的整个布局只是一个字段的图片。图片的前1/4是山。当有人点击该部分时,我想要一个飞狗图片发生,当在该字段中发生点击时,我想要一个跑狗图片发生。关于如何合并坐标或其他东西的任何建议?

public class FieldActivity extends AppCompatActivity {

    public static final String DEBUG_TAG = FieldActivity.class.getSimpleName();
    public TextView mGestureText;
    public ImageView mCorgiFly;
    final Handler handler = new Handler();
    public ImageView mGoldenRun;
    private GestureDetector mGestureDetector;

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

        mGestureText = (TextView) findViewById(R.id.text_view);

        // Create an object of our Custom Gesture Detector Class
        CustomGestureDetector customGestureDetector = new CustomGestureDetector();
        // Create a GestureDetector
        mGestureDetector = new GestureDetector(this, customGestureDetector);
        // Attach listeners that'll be called for double-tap and related gestures
        mGestureDetector.setOnDoubleTapListener(customGestureDetector);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.slideshowopenermenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.menu_item_memories:
                Intent i = new Intent(this, GalleryActivity.class);
                startActivity(i);
                break;
        }
        return super.onOptionsItemSelected(item);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mGestureDetector.onTouchEvent(event);

        return super.onTouchEvent(event);
    }


    class CustomGestureDetector implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {

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

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            mCorgiFly = (ImageView)findViewById(R.id.superman);
            mCorgiFly.setImageResource(R.drawable.flyingcorgi);
           // handler.postDelayed(new Runnable() {
             //   @Override
               // public void run() {
                 //   mCorgiFly.setImageDrawable(null);
                //}
            //}, 500);
            TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
                    0.0f, 0.0f);          //  new TranslateAnimation(xFrom,xTo, yFrom,yTo)
            animation.setDuration(1000);  // animation duration
           // animation.setRepeatCount(5);  // animation repeat count
            //animation.setRepeatMode(2);   // repeat animation (left to right, right to left )
            //animation.setFillAfter(true);

            mCorgiFly.startAnimation(animation);  // start animation
            return true;
        }

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

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

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

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

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

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

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            mGestureText.setText("onFling " + e1.getX() + " - " + e2.getX());

            if (e1.getX() < e2.getX()) {
                Log.d(DEBUG_TAG, "Left to Right swipe performed");
            }

            if (e1.getX() > e2.getX()) {
                Log.d(DEBUG_TAG, "Right to Left swipe performed");
            }

            if (e1.getY() < e2.getY()) {
                Log.d(DEBUG_TAG, "Up to Down swipe performed");
            }

            if (e1.getY() > e2.getY()) {
                Log.d(DEBUG_TAG, "Down to Up swipe performed");
            }

            return true;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

是的,你需要使用一个监听器并用期望设定点比较点击坐标,如果匹配范围则触发回调..