您现在的位置是:群英 > 开发技术 > 移动开发
Android触摸移动图片的功能怎么实现
Admin发表于 2022-05-17 11:47:20830 次浏览
在实际案例的操作过程中,我们可能会遇到“Android触摸移动图片的功能怎么实现”这样的问题,那么我们该如何处理和解决这样的情况呢?这篇小编就给大家总结了一些方法,具有一定的借鉴价值,希望对大家有所帮助,接下来就让小编带领大家一起了解看看吧。


本文实例为大家分享了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依次执行
            }
        });
    }
}

效果


到此这篇关于“Android触摸移动图片的功能怎么实现”的文章就介绍到这了,感谢各位的阅读,更多相关Android触摸移动图片的功能怎么实现内容,欢迎关注群英网络资讯频道,小编将为大家输出更多高质量的实用文章!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

标签: 触摸移动图片
相关信息推荐
2022-09-01 17:56:30 
摘要:javascript是全栈语言;JavaScript是一种解释型的脚本语言,传统意义下的JavaScript仅仅是指运行在客户端浏览器中的脚本语言,随着“Node.js”的出现,将JavaScript应用到了服务器端,因此JavaScript开始称为了全栈语言。
2022-02-19 18:02:34 
摘要:这篇文章我们来了解Python中print输出格式的相关内容,print是Python学习中的基础知识,因此本文就给大家来介绍一下,下文有详细的介绍,有需要的朋友可以参考,接下来就跟随小编来一起学习一下吧!
2022-09-15 17:48:13 
摘要:本篇文章给大家带来了关于java的相关知识,其中主要整理了Map接口的使用以及面试知识点的相关问题,包括了Map接口存储特点、常用实现类、创建方法、常用方法等等内容,下面一起来看一下,希望对大家有帮助。
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部