广告
返回顶部
首页 > 资讯 > 移动开发 >Android通过单点触摸移动图片
  • 865
分享到

Android通过单点触摸移动图片

2024-04-02 19:04:59 865人浏览 泡泡鱼
摘要

本文实例为大家分享了Android通过单点触摸移动图片的具体代码,供大家参考,具体内容如下 编写布局资源文件 先准备一张图片放入drawable内 这里主要就是将图片显示出来并设置

本文实例为大家分享了Android通过单点触摸移动图片的具体代码,供大家参考,具体内容如下

编写布局资源文件

先准备一张图片放入drawable内

这里主要就是将图片显示出来并设置id(android:scaleType="fitXY"表示图片按原比例设置大小)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bk019"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/ivImages"
        android:layout_width="100dp"
        android:layout_height="120dp"
        android:scaleType="fitXY"
        android:src="@drawable/bk031" />


</LinearLayout>

编写主布局文件

(tag是为了看移动图片时的数据)

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "move_images_by_touch";
    private ImageView ivImages;
    private LinearLayout root;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //利用布局资源文件设置用户界面
        setContentView(R.layout.activity_main);
        //通过资源标识符获取控件实例
        ivImages = findViewById(R.id.ivImages);
        root = findViewById(R.id.root);

        //设置根布局可以获取焦点
        root.setFocusable(true);
        //让布局获取焦点
        root.requestFocus();


        //给根布局注册完触摸监听器,实现触摸监听器接口,编写触摸事件代码
        root.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                //根据触摸动作执行不同的操作
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:  //触点按下
                        Log.d(TAG, "ACTION_DOWN"+event.getX() + "," + event.getY());
                        break;
                    case MotionEvent.ACTION_MOVE: //触点移动
                        Log.d(TAG, "ACTION_MOVE"+event.getX() + "," + event.getY());
                        break;
                    case MotionEvent.ACTION_UP: //触点放开
                        Log.d(TAG, "ACTION_UP"+event.getX() + "," + event.getY());
                        break;
                }
                //设置图像控件坐标
                ivImages.setX(event.getX()-ivImages.getWidth()/2);
                ivImages.setY(event.getY()-ivImages.getHeight()/2);
                return true;//设置为真,三个事件:down-->move-->up依次执行
            }
        });
    }
}

效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: Android通过单点触摸移动图片

本文链接: https://www.lsjlt.com/news/147041.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作