码迷,mamicode.com
首页 > 移动开发 > 详细

Android图片旋转,缩放,位移,倾斜,对称完整演示样例(一)——imageView.setImageMatrix(matrix)和Matrix

时间:2017-05-30 23:07:54      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:new t   html   旋转   too   log   let   rgb   style   str   

MainActivity例如以下:
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.app.Activity;
import android.graphics.Matrix;
/**
 * Demo描写叙述:
 * 利用mImageView.setImageMatrix(matrix)实现
 * 图片的平移,缩放,旋转,倾斜以及对称
 * 
 * 參考资料:
 * 0 http://blog.csdn.net/pathuang68/article/details/6991988
 * 1 http://blog.csdn.net/mingli198611/article/details/7830633
 * 
 * Thank you very much
 */
public class MainActivity extends Activity {
    private TestMatrixImageView mTestMatrixImageView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mTestMatrixImageView=new TestMatrixImageView(MainActivity.this);
		mTestMatrixImageView.setScaleType(ImageView.ScaleType.MATRIX);//?

? mTestMatrixImageView.setOnTouchListener(new TouchListenerImpl()); setContentView(mTestMatrixImageView); } private class TouchListenerImpl implements OnTouchListener{ @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction()==MotionEvent.ACTION_UP) { //1 測试平移 testTranslate(); //2 測试环绕图片中心点旋转 //testRotate(); //3 測试环绕原点旋转后平移 //testRotateAndTranslate(); //4 缩放 //testScale(); //5 水平倾斜 //testSkewX(); //6 垂直倾斜 //testSkewY(); //7 水平且垂直倾斜 //testSkewXY(); //8 水平对称 //testSymmetryX(); //9 垂直对称 //testSymmetryY(); //10 关于X=Y对称 //testSymmetryXY(); } return true; } } //平移 private void testTranslate(){ Matrix matrix=new Matrix(); int width=mTestMatrixImageView.getBitmap().getWidth(); int height=mTestMatrixImageView.getBitmap().getHeight(); matrix.postTranslate(width, height); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } //环绕图片中心点旋转 private void testRotate(){ Matrix matrix=new Matrix(); int width=mTestMatrixImageView.getBitmap().getWidth(); int height=mTestMatrixImageView.getBitmap().getHeight(); matrix.postRotate(45f, width/2, height/2); matrix.postTranslate(width, height); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } //环绕原点旋转后平移 //注意下面三行代码的运行顺序: //matrix.setRotate(45f); //matrix.preTranslate(-width, -height); //matrix.postTranslate(width, height); //先运行matrix.preTranslate(-width, -height); //后运行matrix.setRotate(45f); //再运行matrix.postTranslate(width, height); private void testRotateAndTranslate() { Matrix matrix = new Matrix(); int width = mTestMatrixImageView.getBitmap().getWidth(); int height = mTestMatrixImageView.getBitmap().getHeight(); matrix.setRotate(45f); matrix.preTranslate(-width, -height); matrix.postTranslate(width, height); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } //缩放 private void testScale() { Matrix matrix = new Matrix(); matrix.setScale(0.5f, 0.5f); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } //水平倾斜 private void testSkewX() { Matrix matrix = new Matrix(); matrix.setSkew(0.5f, 0); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } // 垂直倾斜 private void testSkewY() { Matrix matrix = new Matrix(); matrix.setSkew(0, 0.5f); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } // 水平且垂直倾斜 private void testSkewXY() { Matrix matrix = new Matrix(); matrix.setSkew(0.5f, 0.5f); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } // 水平对称--图片关于X轴对称 private void testSymmetryX() { Matrix matrix = new Matrix(); int height = mTestMatrixImageView.getBitmap().getHeight(); float matrixValues[] = { 1f, 0f, 0f, 0f, -1f, 0f, 0f, 0f, 1f }; matrix.setValues(matrixValues); //若是matrix.postTranslate(0, height); //表示将图片上下倒置 matrix.postTranslate(0, height*2); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } // 垂直对称--图片关于Y轴对称 private void testSymmetryY() { Matrix matrix = new Matrix(); int width=mTestMatrixImageView.getBitmap().getWidth(); float matrixValues[] = {-1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f}; matrix.setValues(matrixValues); //若是matrix.postTranslate(width,0); //表示将图片左右倒置 matrix.postTranslate(width*2, 0); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } // 关于X=Y对称--图片关于X=Y轴对称 private void testSymmetryXY() { Matrix matrix = new Matrix(); int width = mTestMatrixImageView.getBitmap().getWidth(); int height = mTestMatrixImageView.getBitmap().getHeight(); float matrixValues[] = { 0f, -1f, 0f, -1f, 0f, 0f, 0f, 0f, 1f }; matrix.setValues(matrixValues); matrix.postTranslate(width+height, width+height); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } //获取变换矩阵Matrix中的每一个值 private void showMatrixEveryValue(Matrix matrix){ float matrixValues []=new float[9]; matrix.getValues(matrixValues); for (int i = 0; i <3; i++) { String valueString=""; for (int j = 0; j < 3; j++) { valueString=matrixValues[3*i+j]+""; System.out.println("第"+(i+1)+"行的第"+(j+1)+"列的值为"+valueString); } } } }


TestMatrixImageView例如以下:

package cn.testmatrix;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.widget.ImageView;
/**
 * 总结:
 * 在MainActivity中运行:
 * mTestMatrixImageView.setImageMatrix(matrix);
 * 时此自己定义View会先调用setImageMatrix(Matrix matrix)
 * 然后调用onDraw(Canvas canvas)
 */
public class TestMatrixImageView extends ImageView{
	private Matrix mMatrix;
    private Bitmap mBitmap;
	public TestMatrixImageView(Context context) {
		super(context);
		mMatrix=new Matrix();
		mBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.icon);
	}
	@Override
	protected void onDraw(Canvas canvas) {
		System.out.println("---> onDraw");
		//画原图
		canvas.drawBitmap(mBitmap, 0, 0, null);
		//画经过Matrix变化后的图
		canvas.drawBitmap(mBitmap, mMatrix, null);
		super.onDraw(canvas);
	}
	@Override
	public void setImageMatrix(Matrix matrix) {
		System.out.println("---> setImageMatrix");
		this.mMatrix.set(matrix);
		super.setImageMatrix(matrix);
	}
	
	public Bitmap getBitmap(){
		System.out.println("---> getBitmap");
		return mBitmap;
	}

}

main.xml例如以下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>


Android图片旋转,缩放,位移,倾斜,对称完整演示样例(一)——imageView.setImageMatrix(matrix)和Matrix

标签:new t   html   旋转   too   log   let   rgb   style   str   

原文地址:http://www.cnblogs.com/brucemengbm/p/6921501.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!