Bitmap和Matrix旋转图片

public class RotateImageViewActivity extends Activity {

private Button mButton1;
private Button mButton2;
private TextView mTextView;
private ImageView mImageView;
private int scaleTimes;
private int scaleAngle;


protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.rotate_image);

mButton1 = (Button) findViewById(R.id.rotate_image_Button1);
mButton2 = (Button) findViewById(R.id.rotate_image_Button2);
mImageView = (ImageView) findViewById(R.id.rotate_image_ImageView);
mTextView = (TextView) findViewById(R.id.rotate_image_TextView1);
scaleTimes = 2;
scaleAngle = 1;

Log.v("Lowry", "==================="+mButton1.toString());

final Bitmap mySourceBmp = BitmapFactory.decodeResource(getResources(), R.drawable.hippo);
final int widthOrig = mySourceBmp.getWidth();
final int heightOrig = mySourceBmp.getHeight();

mButton1.setOnClickListener(new OnClickListener(){

public void onClick(View v) {
scaleAngle--;
if(scaleAngle<-5){
scaleAngle=-5;
}//根据旋转次数记录旋转的角度


int newWidth = widthOrig*scaleTimes;
int newHeight = heightOrig*scaleTimes;

float scaleWidth = ((float)newWidth)/widthOrig;
float scaleHeight = ((float)newHeight)/heightOrig;

Log.i("Lowry", "++++widthOrig="+widthOrig+";heightOrig="
+heightOrig+";newWidth="+newWidth+";newHeight="+newHeight+";scaleWidth="+scaleWidth);

Matrix mt = new Matrix();
mt.postScale(scaleWidth, scaleHeight);
//mt.setScale(2.0f,2.0f);设置图片的比例
mt.setRotate(20*scaleAngle);
//mt.postRotate(20*scaleAngle)与其同意,设置旋转的角度
Bitmap newSizeBitmap = Bitmap.createBitmap(mySourceBmp,0,0,widthOrig,heightOrig,mt,true);
//根据原来的Bitmap,mt,生成新的Bitmap对象

BitmapDrawable bd = new BitmapDrawable(newSizeBitmap);

mImageView.setImageDrawable(bd);
mTextView.setText(Integer.toString(20*scaleAngle));


}

});


mButton2.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
scaleAngle++;
if(scaleAngle>5){
scaleAngle = 5;
}

int newWidth = widthOrig*scaleTimes;
int newHeight = heightOrig*scaleTimes;

float scaleWidth = ((float)newWidth)/widthOrig;
float scaleHeight = ((float)newHeight)/heightOrig;


Matrix mt = new Matrix();
mt.postScale(scaleHeight, scaleWidth);
mt.setRotate(20*scaleAngle);

Bitmap newb = Bitmap.createBitmap(mySourceBmp,0,0, widthOrig, heightOrig, mt, true);

BitmapDrawable bd = new BitmapDrawable(newb);

mImageView.setImageDrawable(bd);

mTextView.setText(Integer.toString(20*scaleAngle));


}

});


}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值