btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i = ++i;
ImageView view = (ImageView)findViewById(R.id.imgView);
// 1、首先加载要操作的图片
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.aa);
//2、得到以上加载图片的高度跟宽度
int height = bitmap.getHeight();
int width = bitmap.getWidth();
//3、定义要缩放成最终的图片高度跟宽度
int nHeight = 150;
int nWidth = 180;
//4、计算缩放比例
float scaleWidth = ((float) nWidth)/width;
float scaleHeight = ((float) nHeight)/height;
//5、创建Matrix对象 Matrix是在Android中用于操作图像的类
Matrix matrix = new Matrix();
//6、使用Matrix对象跟缩放比例实现缩放图片
matrix.postScale(scaleWidth, scaleHeight);
//同样的,图片旋转只需要通过Matrix改变图片角度即可,生成图片跟7相同。
Log.i("chens", "======i======"+i);
if (i % 2 ==0 ) {
matrix.postRotate(60);
}else {
matrix.postRotate(0);
}
//7、生成缩放后的图片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,width, height, matrix, true);
view.setImageBitmap(resizedBitmap);
}
});