Android-使用Matrix對Bitmap進行處理

來源:互聯網
上載者:User

標籤:android   style   blog   http   java   使用   

1.Android中使用Matrix對映像進行縮放、旋轉、平移、斜切等變換的。

Matrix是一個3*3的矩陣,其值對應如下:

下面給出具體座標組應變形的屬性
|scaleX, skewX, translateX| 
|skewY, scaleY, translateY|
|0       ,0        , scale       |

Matrix提供了一些方法來控製圖片變換:
setTranslate(float dx,float dy):控制Matrix進行位移。
setSkew(float kx,float ky):控制Matrix進行傾斜,kx、ky為X、Y方向上的比例。
setSkew(float kx,float ky,float px,float py):控制Matrix以px、py為軸心進行傾斜,kx、ky為X、Y方向上的傾斜比例。
setRotate(float degrees):控制Matrix進行depress角度的旋轉,軸心為(0,0)。
setRotate(float degrees,float px,float py):控制Matrix進行depress角度的旋轉,軸心為(px,py)。
setScale(float sx,float sy):設定Matrix進行縮放,sx、sy為X、Y方向上的縮放比例。
setScale(float sx,float sy,float px,float py):設定Matrix以(px,py)為軸心進行縮放,sx、sy為X、Y方向上的縮放比例。
注意:以上的set方法,均有對應的post和pre方法,Matrix調用一系列set,pre,post方法時,可視為將這些方法插入到一個隊列.當然,按照隊列中從頭至尾的順序調用執行.其中pre表示在隊頭插入一個方法,post表示在隊尾插入一個方法.而set表示把當前隊列清空,並且總是位於隊列的最中間位置.當執行了一次set後:pre方法總是插入到set前部的隊列的最前面,post方法總是插入到set後部的隊列的最後面

Demo

package com.example.testaa;import org.androidannotations.annotations.AfterViews;import org.androidannotations.annotations.Click;import org.androidannotations.annotations.EActivity;import org.androidannotations.annotations.UiThread;import org.androidannotations.annotations.ViewById;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Matrix;import android.util.Log;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;@EActivity(R.layout.activity_main)public class MainActivity extends Activity {@ViewByIdImageView iv1;@ViewByIdImageView iv2;@ViewByIdButton btn1;@ViewByIdButton btn2;@ViewByIdButton btn3;@ViewByIdButton btn4;@ViewByIdButton btn5;Bitmap bitmap = null;/** * 載入完View之後進行的處理 */@AfterViewsvoid afterViewProcess() {bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lena);}/** * 縮小 */@Clickvoid btn1() {Matrix matrix = new Matrix();matrix.setScale(0.5f, 0.5f);Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(), matrix, true);iv2.setImageBitmap(bm);showToast(matrix);}    /**     * 先縮小後旋轉     */@Clickvoid btn2() {Matrix matrix = new Matrix();matrix.setScale(0.5f, 0.5f);// 縮小為原來的一半matrix.postRotate(45.0f);// 旋轉45度 == matrix.setSinCos(0.5f, 0.5f);Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(), matrix, true);iv2.setImageBitmap(bm);showToast(matrix);}/** * 平移 */@Clickvoid btn3() {Matrix matrix = new Matrix();matrix.setTranslate(bitmap.getWidth() / 2, bitmap.getHeight() / 2);// 向左下平移Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(), matrix, true);iv2.setImageBitmap(bm);showToast(matrix);}/** * 斜切 */@Clickvoid btn4() {Matrix matrix = new Matrix();matrix.setSkew(0.5f, 0.5f);// 斜切matrix.postScale(0.5f, 0.5f);// 縮小為原來的一半Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(), matrix, true);iv2.setImageBitmap(bm);showToast(matrix);}/** * 相當於自由變換 * 由一個矩形變成四邊形 */@Clickvoid btn5() {Matrix matrix = new Matrix();float[] src = new float[] { 0, 0, // 左上bitmap.getWidth(), 0,// 右上bitmap.getWidth(), bitmap.getHeight(),// 右下0, bitmap.getHeight() };// 左下float[] dst = new float[] { 0, 0, bitmap.getWidth(), 30,bitmap.getWidth(), bitmap.getHeight() - 30,0,bitmap.getHeight() };matrix.setPolyToPoly(src, 0, dst, 0, src.length/2);Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(), matrix, true);iv2.setImageBitmap(bm);showToast(matrix);}/** * 顯示矩陣中的值 * @param matrix */@UiThreadvoid showToast(Matrix matrix) {String string = "";float[] values = new float[9];matrix.getValues(values);for (int i = 0; i < values.length; i++) {string += "matrix.at" + i + "=" + values[i];}Toast.makeText(this, string, Toast.LENGTH_SHORT).show();Log.d("TEST", string);}}


以下是分別對映像進行如下操作的結果:



整個項目的:http://download.csdn.net/detail/nuptboyzhb/7261933


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.