android 绘图Matrix的使用介绍

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

    set是直接设置Matrix的值,每次set一次,整个Matrix的数组都会变掉。
    post是后乘,当前的矩阵乘以参数给出的矩阵。可以连续多次使用post,来完成所需的整个变换。例如,要将一个图片旋
转30度,然后平移到(100,100)的地方,那么可以这样做:


  1. Bitmap bmp = ((BitmapDrawable)getResources().getDrawable(R.drawable.show)).getBitmap();  
  2.         mBitmap = bmp;  
  3.         /*首先,将缩放为100*100。这里scale的参数是比例。有一点要注意,如果直接用100/ 
  4. bmp.getWidth()的话,会得到0,因为是整型相除,所以必须其中有一个是float型的,直接用100f就好。*/  
  5.         mMatrix.setScale(100f/bmp.getWidth(), 100f/bmp.getHeight());  
  6.                 //平移到(100,100)处  
  7.         mMatrix.postTranslate(100100);  
  8.                 //倾斜x和y轴,以(100,100)为中心。  
  9.         mMatrix.postSkew(0.2f, 0.2f, 100100);
在android.graphics.Matrix中有对应旋转的函数:
Matrix matrix = new Matrix();

matrix.setRotate(90);

使用下面的方式也可以实现垂直镜像:
Matrix matrix = new Matrix();
matrix.setScale (1.0,-1.0);
matrix.postTraslate(0, fHeight);


缩放

matrix.postScale(scaleWidth, scaleHeight);//后面两个参数是缩放比例

  1.  //指定矩阵(x轴不变,y轴相反) 就是把图片倒过来
  2.       matrix.preScale(1, -1);


android图片缩小和放大Matrix

public class ex04_22 extends Activity{
 

  
 private ImageView mImageView;
 private Button btn1,btn2;
 private TextView mTextView;
 private AbsoluteLayout layout1;
 private Bitmap bmp;
 private int id=0;
 private int displayWidth,displayHeight;
 private float scaleWidth=1,scaleHeight=1;
 private final static Stringfilename="/data/data/ex04_22.lcs/ex04_22_2.png";
   @Override
    public voidonCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       //取得屏幕分辨率
       DisplayMetrics dm=new DisplayMetrics();
       getWindowManager().getDefaultDisplay().getMetrics(dm);
       displayWidth=dm.widthPixels;
       displayHeight=dm.heightPixels-80;
       
       bmp=BitmapFactory.decodeResource(this.getResources(),R.drawable.ex04_22_1);
       layout1=(AbsoluteLayout)findViewById(R.id.layout1);
       mImageView=(ImageView)findViewById(R.id.myImageView);
       btn1=(Button)findViewById(R.id.myButton1);
       btn1.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
         small();
        }
       });
       btn2=(Button)findViewById(R.id.myButton2);
       btn2.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
         big();
        }
       });
    }
    private voidsmall(){
    //获得Bitmap的高和宽
    int bmpWidth=bmp.getWidth();
    int bmpHeight=bmp.getHeight();
    //设置缩小比例
    double scale=0.8;
    //计算出这次要缩小的比例
    scaleWidth=(float)(scaleWidth*scale);
    scaleHeight=(float)(scaleHeight*scale);
    //产生resize后的Bitmap对象
    Matrix matrix=new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap resizeBmp=Bitmap.createBitmap(bmp, 0, 0,bmpWidth, bmpHeight, matrix, true);
    if(id==0){
     layout1.removeView(mImageView);
    }
    else{
     layout1.removeView((ImageView)findViewById(id));
     
    }
    id++;
    ImageView imageView=new ImageView(this);
    imageView.setId(id);
    imageView.setImageBitmap(resizeBmp);
    layout1.addView(imageView);
    setContentView(layout1);
    btn2.setEnabled(true);
    }
    private voidbig(){
    //获得Bitmap的高和宽
    int bmpWidth=bmp.getWidth();
    int bmpHeight=bmp.getHeight();
    //设置缩小比例
    double scale=1.25;
    //计算出这次要缩小的比例
    scaleWidth=(float)(scaleWidth*scale);
    scaleHeight=(float)(scaleHeight*scale);
    //产生resize后的Bitmap对象
    Matrix matrix=new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap resizeBmp=Bitmap.createBitmap(bmp, 0, 0,bmpWidth, bmpHeight, matrix, true);
    if(id==0){
     layout1.removeView(mImageView);
    }
    else{
     layout1.removeView((ImageView)findViewById(id));
     
    }
    id++;
    ImageView imageView=new ImageView(this);
    imageView.setId(id);
    imageView.setImageBitmap(resizeBmp);
    layout1.addView(imageView);
    setContentView(layout1);
    if(scaleWidth*scale*bmpWidth>displayWidth||scaleHeight*scale*scaleHeight>displayHeight){
     btn2.setEnabled(false);
    }
    }
}


  1.   
  2. public class MyView extends View {  
  3.       
  4.     private Bitmap mBitmap;  
  5.     private Matrix mMatrix = new Matrix();  
  6.       
  7.     public MyView(Context context) {  
  8.         super(context);  
  9.         initialize();  
  10.     }  
  11.   
  12.     private void initialize() {  
  13.           
  14.         Bitmap bmp = ((BitmapDrawable)getResources().getDrawable(R.drawable.show)).getBitmap();  
  15.         mBitmap = bmp;  
  16.         /*首先,将缩放为100*100。这里scale的参数是比例。有一点要注意,如果直接用100/ 
  17. bmp.getWidth()的话,会得到0,因为是整型相除,所以必须其中有一个是float型的,直接用100f就好。*/  
  18.         mMatrix.setScale(100f/bmp.getWidth(), 100f/bmp.getHeight());  
  19.                 //平移到(100,100)处  
  20.         mMatrix.postTranslate(100100);  
  21.                 //倾斜x和y轴,以(100,100)为中心。  
  22.         mMatrix.postSkew(0.2f, 0.2f, 100100);  
  23.     }  
  24.       
  25.     @Override protected void onDraw(Canvas canvas) {  
  26. //      super.onDraw(canvas);  //如果界面上还有其他元素需要绘制,只需要将这句话写上就行了。  
  27.           
  28.         canvas.drawBitmap(mBitmap, mMatrix, null);  
  29.     }  

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值