博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android实现图片顺时逆时旋转及拖拽显示效果
阅读量:7229 次
发布时间:2019-06-29

本文共 3413 字,大约阅读时间需要 11 分钟。

1、首先说一下两个类:

 

Matrix

Class Overview

 

The Matrix class holds a 3x3 matrix for transforming coordinates. Matrix does not have a constructor, so it must be explicitly initialized using either reset() - to construct an identity matrix, or one of the set..() functions (e.g. setTranslate, setRotate, etc.).

矩阵类拥有3 x3的坐标变换矩阵。没有一个构造函数矩阵,所以它必须显式初始化的使用或重置()-如何构建一个矩阵,或者一个场景……()的功能(例如,setRotate setTranslate等。)

Matrix的操作,总共分为translate(平移),rotate(旋转),scale(缩放)和skew(倾斜)四种,每一种变换在的API里都提供了set,post和pre三种操作方式,除了translate,其他三种操作都可以指定中心点。set是直接设置Matrix的值,每次set一次,整个Matrix的数组都会变掉。post是后乘,当前的矩阵乘以参数给出的矩阵。可以连续多次使用post,来完成所需的整个变换。

接下来我们用到了两个方法:

平移方法:两个参数分别是要移到的x、y坐标

 

boolean (float dx, float dy)
Postconcats the matrix with the specified translation.

和旋转方法:第一个参数是旋转多少度,正数是顺时针,负数是逆时针;第二三参数是按某个点旋转的x、y坐标;

 

 

boolean (float degrees, float px, float py)
Postconcats the matrix with the specified rotation.

 

 

 

PointF

Class Overview

 

PointF holds two float coordinates

PointF有两个浮点坐标

我们要用到该类的一个方法:设置点的x和y坐标

 

final void (float x, float y)
Set the point's x and y coordinates

2、接下来是案例:

 

首先看一下效果图:

  旋转拖拽后

布局很简单在此不再给出!直接看java代码:

public class MovePictureActivity extends Activity implements OnClickListener {    private Button button1, button2;    private ImageView image;    PointF startPoint = new PointF();// 有两PointF浮坐标    Matrix matrix = new Matrix();    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        init();    }    private void init() {        button1 = (Button) findViewById(R.id.button1);        button2 = (Button) findViewById(R.id.button2);        button1.setOnClickListener(this);        button2.setOnClickListener(this);        image = (ImageView) findViewById(R.id.image);        image.setOnTouchListener(new ImageViewOnTouchListener());// 为image绑上触摸事件监听    }    @Override    public void onClick(View v) {        switch (v.getId()) {        case R.id.button1:            matrix.postRotate(90, image.getWidth() / 2, image.getHeight() / 2);// 顺时针旋转90度,并且以image.getWidth()/2、image.getHeight()/2为中心旋转;            break;        case R.id.button2:            matrix.postRotate(-90, image.getWidth() / 2, image.getHeight() / 2);// 逆时针旋转90度            break;        }        image.setImageMatrix(matrix);    }    private class ImageViewOnTouchListener implements OnTouchListener {        @Override        public boolean onTouch(View v, MotionEvent event) {            switch (event.getAction() & MotionEvent.ACTION_MASK) {
// 这里取出来的是event.getAction()返回的值的低八位,MotionEvent.ACTION_MASK是255, case MotionEvent.ACTION_DOWN: startPoint.set(event.getX(), event.getY()); break; case MotionEvent.ACTION_MOVE:// 移动过程,该事件会不断被触发 float dx = event.getX() - startPoint.x; float dy = event.getY() - startPoint.y; matrix.postTranslate(dx, dy); startPoint.set(event.getX(), event.getY()); break; } image.setImageMatrix(matrix); return true; } }}

 

为image绑定监听事件,

image.setOnTouchListener(new ImageViewOnTouchListener());// 为image绑上触摸事件监听

View.OnTouchListener

该接口:

 

Interface definition for a callback to be invoked when a touch event is dispatched to this view. The callback will be invoked before the touch event is given to the view.

接口定义作为一个回调函数被调用时被派遣去触摸事件这一观点。回调函数被调用之前会触摸事件是给你尽情的观看。

原代码下载地址:

 

 

 

 

 

转载地址:http://iwdfm.baihongyu.com/

你可能感兴趣的文章
Java新版本的开发已正式进入轨道,版本号18.3
查看>>
从零开始的webpack生活-0x009:FilesLoader装载文件
查看>>
在electron中实现跨域请求,无需更改服务器端设置
查看>>
gitlab-ci配置详解(一)
查看>>
听说你叫Java(二)–Servlet请求
查看>>
案例分享〡三拾众筹持续交付开发流程支撑创新业务
查看>>
FreeWheel业务系统微服务化过程经验分享
查看>>
移动互联网下半场,iOS开发者如何“高薪”成长?
查看>>
Atlassian是怎样进行持续交付的?且听 Steve Smith一一道来
查看>>
Web Storage相关
查看>>
[PHP内核探索]PHP中的哈希表
查看>>
Apache-drill Architechture
查看>>
WordPress 5.2 Beta 3 发布,要求 PHP 5.6.20 以上版本
查看>>
通通连起来——无处不在的流
查看>>
互联网+时代,看云计算如何改变传统行业
查看>>
ZFS ARC & L2ARC zfs-$ver/module/zfs/arc.c
查看>>
c++类默认拷贝构造函数---浅复制
查看>>
2019年最火热的Golang项目
查看>>
可实现RSSD云硬盘120万IOPS的SPDK IO路径优化实践
查看>>
Vue项目部署遇到的坑(你肯定会遇到!)
查看>>