android手机开发之图片旋转的二种方式

android手机开发之图片旋转
一、每次根据旋转矩阵从原位图创建出旋转后的新位图。但是缺点就是要船舰新的位图。  
// createa matrix for the manipulation  
Matrix matrix = new Matrix();  
// rotate the Bitmap  
matrix.postRotate(45);  


// recreate the new Bitmap  
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,  
width, height, matrix, true);  


// make a Drawable from Bitmap to allow to set the BitMap  
// to the ImageView, ImageButton or what ever  
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);  


二、将旋转图片转为旋转画布。效率较高。Android官方Demo:LunarLander正是旋转画布,从而达到旋转图片,但是从效果上看,有些失真。(旋转图片时,一般旋转中心为图片的中心。)  
(旋转方向为顺时针,若角度为负则为逆时针 )  


//注意画布的状态保存和恢复  
canvas.save();  
//参数分别为:旋转角度,图片X中心,图片Y中心。  
canvas.rotate(angle, getCenterX(),getCenterY());  
spriteImage.setBounds(bounds);  
spriteImage.draw(canvas);  
canvas.restore();


目标: 


本文将讲述如何如何在Android中使用Matrix实现图片的缩放和旋转,通过本文学习,你将学会如何通过Matrix操作图像。 


 


 


代码示例:


直接上代码了,我在代码中附带了详细的解释,代码如下:


package com.eoeandroid.demo.testcode;



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 bitmaptest extends Activity {


public void onCreate(Bundle icicle) { 

        super.onCreate(icicle); 
        setTitle("eoeAndroid教程: 缩放和旋转图片 -by:IceskYsl");
        LinearLayout linLayout = new LinearLayout(this); 

        // 加载需要操作的图片,这里是eoeAndroid的logo图片
       Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
               R.drawable.eoe_android);
        //获取这个图片的宽和高
        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); 
    } 

}


 
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值