android之Matrix

 

今天我学习下android中和图片相关的Matrix.

先看代码示例:

view plain copy to clipboard print ?
  1. package cn.com.chenzheng_java;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapFactory;  
  6. import android.graphics.Matrix;  
  7. import android.graphics.drawable.BitmapDrawable;  
  8. import android.os.Bundle;  
  9. import android.view.ViewGroup.LayoutParams;  
  10. import android.widget.ImageView;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.ImageView.ScaleType;  
  13.   
  14. public class MatrixActivity extends Activity{  
  15.       
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.               setTitle("eoeAndroid教程: 缩放和旋转图片 -by:IceskYsl");  
  20.                     LinearLayout linLayout = new LinearLayout(this);  
  21.               
  22.                     // 加载需要操作的图片,这里是eoeAndroid的logo图片  
  23.                     Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),  
  24.                            R.drawable.blackk);  
  25.           
  26.                     //获取这个图片的宽和高   
  27.                     int width = bitmapOrg.getWidth();  
  28.                     int height = bitmapOrg.getHeight();  
  29.               
  30.                    //定义预转换成的图片的宽度和高度  
  31.               int newWidth = 200;  
  32.                    int newHeight = 200;  
  33.           
  34.                     //计算缩放率,新尺寸除原始尺寸  
  35.                    float scaleWidth = ((float) newWidth) / width;  
  36.                       float scaleHeight = ((float) newHeight) / height;  
  37.                   
  38.                       // 创建操作图片用的matrix对象  
  39.                       Matrix matrix = new Matrix();  
  40.                   
  41.                         // 缩放图片动作  
  42.                         matrix.postScale(scaleWidth, scaleHeight);  
  43.                   
  44.                         //旋转图片 动作  
  45.                         matrix.postRotate(45);  
  46.                   
  47.                         // 创建新的图片  
  48.                         Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 00,  
  49.                                           width, height, matrix, true);  
  50.                   
  51.                         //将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView, ImageButton中  
  52.                        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);  
  53.                   
  54.                        //创建一个ImageView  
  55.                        ImageView imageView = new ImageView(this);  
  56.                   
  57.                         // 设置ImageView的图片为上面转换的图片  
  58.                         imageView.setImageDrawable(bmd);  
  59.                   
  60.                         //将图片居中显示  
  61.                         imageView.setScaleType(ScaleType.CENTER);  
  62.                   
  63.                         //将ImageView添加到布局模板中  
  64.                         linLayout.addView(imageView,  
  65.                           new LinearLayout.LayoutParams(  
  66.                                       LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT  
  67.                                )  
  68.                        );  
  69.                   
  70.                        // 设置为本activity的模板  
  71.                        setContentView(linLayout);  
  72.                    }  
  73.   
  74.   
  75.           
  76.           
  77.     }  
package cn.com.chenzheng_java; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.ViewGroup.LayoutParams; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ImageView.ScaleType; public class MatrixActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle("eoeAndroid教程: 缩放和旋转图片 -by:IceskYsl"); LinearLayout linLayout = new LinearLayout(this); // 加载需要操作的图片,这里是eoeAndroid的logo图片 Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.blackk); //获取这个图片的宽和高 int width = bitmapOrg.getWidth(); int height = bitmapOrg.getHeight(); //定义预转换成的图片的宽度和高度 int newWidth = 200; int newHeight = 200; //计算缩放率,新尺寸除原始尺寸 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 创建操作图片用的matrix对象 Matrix matrix = new Matrix(); // 缩放图片动作 matrix.postScale(scaleWidth, scaleHeight); //旋转图片 动作 matrix.postRotate(45); // 创建新的图片 Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true); //将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView, ImageButton中 BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); //创建一个ImageView ImageView imageView = new ImageView(this); // 设置ImageView的图片为上面转换的图片 imageView.setImageDrawable(bmd); //将图片居中显示 imageView.setScaleType(ScaleType.CENTER); //将ImageView添加到布局模板中 linLayout.addView(imageView, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT ) ); // 设置为本activity的模板 setContentView(linLayout); } }

 

结果示意:

android之Matrix_第1张图片

-------------------------------------------------------------------------------------

现在,我们来详细介绍下android.graphics.Matrix类的主要应用。

Matrix的操作,总共分为translate(平移),rotate(旋转),scale(缩放)和skew(倾斜)四种,每一种变换在Android的API里都提供了set, post和pre三种操作方式,除了translate,其他三种操作都可以指定中心点。

旋转、缩放和倾斜都可以围绕一个中心点来进行,如果不指定,默认情况下,是围绕(0,0)点来进行。

具体的方法及使用方法请参考API、这里就不一一列出了,实在是太多了。

你可能感兴趣的:(android,api,Class,float,Matrix)