• 一手遮天 Android


    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

    一手遮天 Android - 输入: Touch 基础(触摸位置,事件冒泡)

    示例如下:

    /input/TouchDemo2.java

    /**
     * Touch 基础
     *
     * setOnTouchListener() - 触摸事件
     *     MotionEvent 的 getX() - 触摸点相对于监听控件自身的 x 位置
     *     MotionEvent 的 getY() -  触摸点相对于监听控件自身的 y 位置
     *     MotionEvent 的 getRawX() - 触摸点相对于整个屏幕的 x 位置
     *     MotionEvent 的 getRawY() - 触摸点相对于整个屏幕的 y 位置
     *     返回 true 代表事件不冒泡
     *     返回 false 代表事件冒泡(如果在 MotionEvent.ACTION_DOWN 中返回 false,则之后不会收到 MotionEvent.ACTION_MOVE 和 MotionEvent.ACTION_UP)
     *
     *
     * 本例演示
     * 1、如何在 setOnTouchListener() 中获取触摸点的位置信息
     * 2、事件冒泡
     */
    
    
    package com.webabcd.androiddemo.input;
    
    import android.annotation.SuppressLint;
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.FrameLayout;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.webabcd.androiddemo.R;
    
    public class TouchDemo2 extends AppCompatActivity {
    
        private FrameLayout mFrameLayout1;
        private ImageView mImageView1;
        private TextView mTextView1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_input_touchdemo2);
    
            mFrameLayout1 = findViewById(R.id.frameLayout1);
            mImageView1 = findViewById(R.id.imageView1);
            mTextView1 = findViewById(R.id.textView1);
    
            sample();
        }
    
        @SuppressLint("ClickableViewAccessibility")
        private void sample() {
            mFrameLayout1.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    mTextView1.append(String.format("mFrameLayout1 setOnClickListener, x:%f, y:%f, rawX:%f, rawY:%f
    ", event.getX(), event.getY(), event.getRawX(), event.getRawY()));
    
                    return false;
                }
            });
    
            mImageView1.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    mTextView1.append(String.format("mImageView1 setOnClickListener, x:%f, y:%f, rawX:%f, rawY:%f
    ", event.getX(), event.getY(), event.getRawX(), event.getRawY()));
    
                    return false;
                }
            });
        }
    }
    
    

    /layout/activity_input_touchdemo2.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <FrameLayout
            android:id="@+id/frameLayout1"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="@color/orange">
    
            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:scaleType="fitXY"
                android:layout_gravity="center"
                android:src="@drawable/img_sample_son" />
    
        </FrameLayout>
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    
    

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

  • 相关阅读:
    JAVA中“==”和equals
    C++中各种容器的类型与特点
    程序员面试宝典 笔记 第七章
    程序员面试宝典 笔记(第六章 预处理 const 和sizeof())
    某学长面经
    tomcat 启动日志乱码
    Jenkins关闭和重启实现方式
    linux下svn版本控制的常用命令大全
    Java中的增强 for 循环 foreach
    JS 中 cookie 的使用
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_input_TouchDemo2.html
Copyright © 2020-2023  润新知