Android实现图片拖拉功能
作者:光仔December 时间:2023-03-27 14:57:51
这次记录的是实现Android图片用手拖拉的功能,,供大家参考,具体内容如下
编译环境:eclipse
Android版本4.0
创建工程过程略
实现图片在页面内的拖拉
原理图:
计算出手指移动的位移,通过matrix矩阵对象,将图片空间矩阵化之后根据手指位移让矩阵移动,
从而达到图片移动的效果
实现图片在页面内的拖拉
mian.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.dragscale.MainActivity" >
<!--scaleType="matrix"采用矩阵来实现图片的拖拉或放大-->
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/keep"
android:scaleType="matrix"
android:id="@+id/image"/>
</LinearLayout>
MainActivity.java:
package com.example.dragscale;
import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imageView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView)this.findViewById(R.id.image);
//添加触摸监听对象(控件被触摸就会触发TouchListener类)
imageView.setOnTouchListener(new TouchListener());
}
private final class TouchListener implements OnTouchListener{
private PointF startPoint= new PointF();//PointF(浮点对)
private Matrix matrix=new Matrix();//矩阵对象
private Matrix currentMatrix=new Matrix();//存放照片当前的矩阵
//参数1:用户触摸的控件;参数2:用户触摸所产生的事件
public boolean onTouch(View v, MotionEvent event) {
//判断事件的类型
//得到低八位才能获取动作,所以要屏蔽高八位(通过与运算&255)
//ACTION_MASK就是一个常量,代表255
switch (event.getAction()&MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN://手指下压
currentMatrix.set(imageView.getImageMatrix());//记录ImageView当前的移动位置
startPoint.set(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE://手指在屏幕移动,改事件会不断被调用
float dx=event.getX()-startPoint.x;//得到在x轴的移动距离
float dy=event.getY()-startPoint.y;//得到在y轴的移动距离
matrix.set(currentMatrix);//在没有进行移动之前的位置基础上进行移动
//实现位置的移动
matrix.postTranslate(dx, dy);
break;
case MotionEvent.ACTION_UP://手指离开屏幕
break;
case MotionEvent.ACTION_POINTER_1_UP://当屏幕上已经有手指离开屏幕,屏幕上还有一个手指,就会触发这个事件
break;
case MotionEvent.ACTION_POINTER_1_DOWN://当屏幕上已经有触点(手指),再有一个手指按下屏幕,就会触发这个事件
break;
default:
break;
}
//将imageView的矩阵位置改变
imageView.setImageMatrix(matrix);
return true;
}
}
}
来源:http://blog.csdn.net/acmman/article/details/41049567
标签:Android,图片拖拉
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Data JPA实现动态查询的两种方法
2022-08-04 17:02:27
C#检查字符串是否是合法URL地址的方法
2022-08-09 16:24:06
功能强大的TraceId 搭配 ELK使用详解
2021-09-16 02:26:43
![](https://img.aspxhome.com/file/2023/7/125267_0s.png)
java自定义注解实现前后台参数校验的实例
2023-04-27 23:53:21
![](https://img.aspxhome.com/file/2023/5/92695_0s.png)
c#读写App.config,ConfigurationManager.AppSettings 不生效的解决方法
2021-10-07 22:34:42
![](https://img.aspxhome.com/file/2023/6/74816_0s.jpg)
介绍Jersey-Jersey入门基础
2023-11-18 06:34:26
Android自定义EditText右侧带图片控件
2023-08-30 03:09:47
![](https://img.aspxhome.com/file/2023/6/126796_0s.jpg)
Android图片裁剪功能实现代码
2021-06-13 11:03:14
![](https://img.aspxhome.com/file/2023/4/138674_0s.jpg)
JPA Specification常用查询+排序实例
2023-11-23 04:56:32
![](https://img.aspxhome.com/file/2023/3/59423_0s.png)
Windows 10上JDK环境安装配置图文教程
2023-05-31 19:38:03
![](https://img.aspxhome.com/file/2023/7/86737_0s.png)
Android自定义View多种效果解析
2022-05-26 06:59:46
![](https://img.aspxhome.com/file/2023/2/139242_0s.png)
基于C#实现的木马程序实例详解
2023-02-25 20:05:03
Android事件分发机制全面解析
2023-05-11 06:53:49
c#操作ftp类分享
2023-02-27 12:50:17
C#使用Task实现异步方法
2022-09-02 20:26:53
![](https://img.aspxhome.com/file/2023/6/70436_0s.jpg)
完美解决Spring Boot前端的Access-Control-Allow-Origin跨域问题
2023-09-19 10:16:28
Android编辑框EditText与焦点变更监视器及文本变化监视器实现流程详解
2021-06-21 04:48:14
![](https://img.aspxhome.com/file/2023/6/98676_0s.png)
FasfDFS整合Java实现文件上传下载功能实例详解
2022-05-27 02:30:46
![](https://img.aspxhome.com/file/2023/8/70268_0s.png)
c#编写webservice服务引用实例分享
2023-05-11 19:28:41
Android实现本地Service方法控制音乐播放
2021-12-16 07:22:31
![](https://img.aspxhome.com/file/2023/0/129140_0s.jpg)