Android實現圖片順時逆時旋轉及拖拽顯示效果

來源:互聯網
上載者:User

1、首先說一下兩個類:

MatrixClass 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(傾斜)四種,每一種變換在Android的API裡都提供了set,post和pre三種操作方式,除了translate,其他三種操作都可以指定中心點。set是直接設定Matrix的值,每次set一次,整個Matrix的數組都會變掉。post是後乘,當前的矩陣乘以參數給出的矩陣。可以連續多次使用post,來完成所需的整個變換。

接下來我們用到了兩個方法:

平移方法:兩個參數分別是要移到的x、y座標

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

和旋轉方法:第一個參數是旋轉多少度,正數是順時針,負數是逆時針;第二三參數是按某個點旋轉的x、y座標;

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

PointFClass Overview

PointF holds two float coordinates

PointF有兩個浮點座標

我們要用到該類的一個方法:設定點的x和y座標

final void set(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();@Overridepublic 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綁上觸摸事件監聽}@Overridepublic 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 {@Overridepublic 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://download.csdn.net/download/rhljiayou/4286882

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.