动作 | 常量 |
---|---|
按下 | MotionEvent.ACTION_DOWN |
移动 | MotionEvent.ACTION_MOVE |
放开 | MotionEvent.ACTION_UP |
基于Empty Activity模板创建安卓应用 - MoveMickeyByTouch2
单击【Finish】按钮
<resources><string name="app_name">通过触摸移动米老鼠</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""xmlns:tools=""android:id="@+id/root"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/background" android:orientation="vertical"tools:context=".MainActivity"><ImageViewandroid:id="@+id/iv_mickey"android:layout_width="100dp"android:layout_height="120dp"android:scaleType="fitXY"android:src="@drawable/mickey" />
</LinearLayout>
package vemickeybytouch2;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;public class MainActivity extends AppCompatActivity {private LinearLayout root; // 线性根布局private ImageView ivMickey; // 米老鼠图像控件private LinearLayout.LayoutParams layoutParams; // 布局参数private static final String TAG = "move_mickey_by_touch"; // 标记常量@Overrideprotected void onCreate(Bundle savedInstanceState) {Create(savedInstanceState);// 利用布局资源文件设置用户界面setContentView(R.layout.activity_main);// 通过资源标识符获取控件实例root = findViewById();ivMickey = findViewById(R.id.iv_mickey);// 让根布局获取焦点root.setFocusable(true);questFocus();// 获取米老鼠图像控件的布局参数layoutParams = (LinearLayout.LayoutParams) LayoutParams();// 给线性根布局注册触摸监听器,实现触摸监听器接口,编写触摸事件代码root.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// 根据不同触摸动作执行不同操作switch (Action()) {case MotionEvent.ACTION_DOWN: // 0, 触点按下// 调试信息输出触点坐标Log.d(TAG, "ACTION_DOWN(" + X() + ", " + Y() + ")");break;case MotionEvent.ACTION_MOVE: // 2, 触点移动// 调试信息输出触点坐标Log.d(TAG, "ACTION_MOVE(" + X() + ", " + Y() + ")");break;case MotionEvent.ACTION_UP: // 1, 触点放开// 调试信息输出触点坐标Log.d(TAG, "ACTION_UP(" + X() + ", " + Y() + ")");break;}// 根据变化的触点坐标来更新米老鼠图像控件的布局参数layoutParams.leftMargin = (int) X();pMargin = (int) Y();// 重新设置米老鼠图像控件的布局参数ivMickey.setLayoutParams(layoutParams);return true; // 设置为true,三个事件:down-->move-->up才会依次执行}});}
}
package vemickeybytouch2;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;public class MainActivity extends AppCompatActivity {private LinearLayout root; // 线性根布局private ImageView ivMickey; // 米老鼠图像控件private static final String TAG = "move_mickey_by_touch"; // 标记常量@Overrideprotected void onCreate(Bundle savedInstanceState) {Create(savedInstanceState);// 利用布局资源文件设置用户界面setContentView(R.layout.activity_main);// 通过资源标识符获取控件实例root = findViewById();ivMickey = findViewById(R.id.iv_mickey);// 让根布局获取焦点root.setFocusable(true);questFocus();// 给线性根布局注册触摸监听器,实现触摸监听器接口,编写触摸事件代码root.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// 根据不同触摸动作执行不同操作switch (Action()) {case MotionEvent.ACTION_DOWN: // 0, 触点按下// 调试信息输出触点坐标Log.d(TAG, "ACTION_DOWN(" + X() + ", " + Y() + ")");break;case MotionEvent.ACTION_MOVE: // 2, 触点移动// 调试信息输出触点坐标Log.d(TAG, "ACTION_MOVE(" + X() + ", " + Y() + ")");break;case MotionEvent.ACTION_UP: // 1, 触点放开// 调试信息输出触点坐标Log.d(TAG, "ACTION_UP(" + X() + ", " + Y() + ")");break;}// 设置米老鼠图像控件的坐标ivMickey.X() - Width() / 2);ivMickey.Y() - Height() / 2);return true; // 设置为true,三个事件:down-->move-->up才会依次执行}});}
}
<resources><string name="app_name">通过多点触摸缩放米老鼠</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""xmlns:tools=""android:id="@+id/root"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/background"android:orientation="vertical"tools:context=".MainActivity"><ImageViewandroid:id="@+id/iv_mickey"android:layout_width="100dp"android:layout_height="120dp"android:scaleType="fitXY"android:src="@drawable/mickey" />
</LinearLayout>
package ickeybytouch;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;public class MainActivity extends AppCompatActivity {private LinearLayout root; // 线性根布局private ImageView ivMickey; // 米老鼠图像控件private float x1, y1; // 第一个触点的坐标private float x2, y2; // 第二个触点的坐标private float nextX1, nextY1; // 第一个触点下一次的坐标private float nextX2, nextY2; // 第二个触点下一次的坐标private float distance; // 两个触点之间的距离private float nextDistance; // 两个触点之间下一次的距离private LinearLayout.LayoutParams layoutParams; // 布局参数@Overrideprotected void onCreate(Bundle savedInstanceState) {Create(savedInstanceState);// 利用布局资源文件设置用户界面setContentView(R.layout.activity_main);// 通过资源标识符获取控件实例root = findViewById();ivMickey = findViewById(R.id.iv_mickey);// 让根布局获取焦点root.setFocusable(true);questFocus();// 获取米老鼠图像控件的布局参数layoutParams = (LinearLayout.LayoutParams) LayoutParams();// 给根布局注册触摸监听器,实现触摸监听器接口,编写触摸事件方法root.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// 判断触点个数if (PointerCount() == 1) { // 单点触摸 - 移动米老鼠if (Action() == MotionEvent.ACTION_MOVE) {// 根据变化的触点坐标来更新米老鼠图像控件的布局参数layoutParams.leftMargin = (int) (X() - Width() / 2);pMargin = (int) (Y() - Height() / 2);}} else if (PointerCount() == 2) { // 两点触摸 - 缩放米老鼠// 判断触点动作switch (Action()) {case MotionEvent.ACTION_DOWN: // 触点按下// 获取第一个触点的坐标x1 = X(0);y1 = Y(0);// 获取第二个触点的坐标x2 = X(1);y2 = Y(1);// 计算两个触点之间的距离distance = (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));break;case MotionEvent.ACTION_MOVE: // 触点移动// 获取第一个触点下一次的坐标nextX1 = X(0);nextY1 = Y(0);// 获取第二个触点下一次的坐标nextX2 = X(1);nextY2 = Y(1);// 计算两个触点之间下一次的距离nextDistance = (float) Math.sqrt((nextX1 - nextX2) * (nextX1 - nextX2) + (nextY1 - nextY2) * (nextY1 - nextY2));break;case MotionEvent.ACTION_UP: // 触点放开break;}// 修改米老鼠图像控件的布局参数if (nextDistance > distance) { // 放大图片if (layoutParams.width < 1500) { // 控制最大尺寸layoutParams.width = (int) (layoutParams.width * 1.05);layoutParams.height = (int) (layoutParams.height * 1.05);}} else { // 缩小图片if (layoutParams.width > 10) { // 控制最小尺寸layoutParams.width = (int) (layoutParams.width / 1.05);layoutParams.height = (int) (layoutParams.height / 1.05);}}// 第一个触点坐标进行迭代x1 = nextX1;y1 = nextY1;// 第二个触点坐标进行迭代x2 = nextX2;y2 = nextY2;// 重新计算两个触点之间的距离distance = (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));}// 重新设置米老鼠图像控件的布局参数ivMickey.setLayoutParams(layoutParams);return true;}});}
}
本文发布于:2024-02-02 04:12:16,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170681833541273.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |