一手遮天 Android - 输入: Touch 在自定义控件中处理触摸事件;处理 Activity 的触摸事件

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

一手遮天 Android - 输入: Touch 在自定义控件中处理触摸事件;处理 Activity 的触摸事件

示例如下:

/input/TouchDemo4.java

/**
 * Touch 在自定义控件中处理触摸事件;处理 Activity 的触摸事件
 *
 * 本例演示
 * 1、在自定义控件中处理触摸事件,参见 view/input/TouchDemo4_CustomView.java(演示了如何在自定义控件内部监听 touch 事件)
 * 2、处理 Activity 的触摸事件,参见本 activity 的 @Override boolean onTouchEvent()
 */

package com.webabcd.androiddemo.input;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;

import com.webabcd.androiddemo.R;

public class TouchDemo4 extends AppCompatActivity {

    private TextView mTextView1;

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

        mTextView1 = findViewById(R.id.textView1);
    }

    // 处理 activity 的触摸事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mTextView1.append("onTouchEvent");

        return true;
    }
}

\input\TouchDemo4_CustomView.java

/**
 * 一个自定义的 view
 * @Override boolean onTouchEvent() - 在控件内部监听触摸事件
 *
 *
 * 本例绘制了一个蓝色圆圈,其会跟随当前触摸点的位置(演示了如何在自定义控件内部监听 touch 事件)
 */

package com.webabcd.androiddemo.input;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class TouchDemo4_CustomView extends View {

    // 圆圈的位置
    public float X = 50;
    public float Y = 50;

    private Paint mPaint = new Paint();

    public TouchDemo4_CustomView(Context context, AttributeSet set)
    {
        super(context,set);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 绘制一个蓝色圆圈
        mPaint.setColor(Color.BLUE);
        canvas.drawCircle(X,Y,30, mPaint);
    }

    // 在控件内部处理触摸事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.X = event.getX();
        this.Y = event.getY();

        // 通知组件重新绘制
        this.invalidate();

        return true;
    }
}

/layout/activity_input_touchdemo4.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">

    <!--
        用于演示如何在自定义控件内部监听 touch 事件
    -->
    <com.webabcd.androiddemo.input.TouchDemo4_CustomView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/red"
        android:layout_weight="1"/>

    <!--
        用于演示如何监听 activity 的 touch 事件
    -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/green"
        android:layout_weight="1">
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </FrameLayout>

</LinearLayout>

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

posted @ 2021-06-02 09:15  webabcd  阅读(122)  评论(0编辑  收藏  举报