zoukankan      html  css  js  c++  java
  • Android-采用Matrix对Bitmap加工

    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 {
    
    	@ViewById
    	ImageView iv1;
    
    	@ViewById
    	ImageView iv2;
    
    	@ViewById
    	Button btn1;
    
    	@ViewById
    	Button btn2;
    
    	@ViewById
    	Button btn3;
    
    	@ViewById
    	Button btn4;
    
    	@ViewById
    	Button btn5;
    
    	Bitmap bitmap = null;
    
    	/**
    	 * 载入完View之后进行的处理
    	 */
    	@AfterViews
    	void afterViewProcess() {
    		bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lena);
    
    	}
    
    	/**
    	 * 缩小
    	 */
    	@Click
    	void 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);
    	}
    
        /**
         * 先缩小后旋转
         */
    	@Click
    	void 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);
    	}
    
    	/**
    	 * 平移
    	 */
    	@Click
    	void 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);
    	}
    
    	/**
    	 * 斜切
    	 */
    	@Click
    	void 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);
    	}
    
    	/**
    	 * 相当于自由变换
    	 * 由一个矩形变成四边形
    	 */
    	@Click
    	void 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
    	 */
    	@UiThread
    	void 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


    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    PowerDesigner生成sql及说明文档
    Visual Studio 2005 Team System & UML
    检查数据库数据字段命名规范与合法性的脚本
    常用的快速Web原型图设计工具
    用户需求说明书模板
    数据库设计说明书
    Visual SourceSafe 命名约定和限制
    需求管理工具DOORS介绍
    C#编码命名规则
    数据库对象命名规范
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4803396.html
Copyright © 2011-2022 走看看