首页 > 代码库 > 通过Matrix缩放图片
通过Matrix缩放图片
public class MainActivity extends Activity implements OnClickListener {
private ImageView iv;
private Bitmap bitmapR04;
private Boolean change = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
bitmapR04 = BitmapFactory
.decodeResource(getResources(), R.drawable.r04);
iv.setImageBitmap(bitmapR04);
iv.setOnClickListener(this);
}
@SuppressLint("NewApi") @SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
if (!change) {
Bitmap yScaleBitmap = getYScaleBitmap(bitmapR04);
iv.setImageDrawable(new BitmapDrawable(yScaleBitmap));
// iv.setImageBitmap(yScaleBitmap);
change = true;
} else {
iv.setImageBitmap(bitmapR04);
change = false;
}
}
public Bitmap getYScaleBitmap(Bitmap bitmapScale){
Bitmap blankBitmap = Bitmap.createBitmap(bitmapScale.getWidth() ,
bitmapScale.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(blankBitmap);
// 创建一个矩阵对象
Matrix matrix = new Matrix();
//下面参数进行设置图片缩放
matrix.setScale(1f, 1.18f, 0, 800);
canvas.drawBitmap(bitmapScale, matrix, null);
return blankBitmap;
}
}
备注:尝试设置matrix.setScale(X轴缩放,Y轴缩放,,);//后面两个参数是相对于缩放的位置放置,尝试设置,建议数值>100以上进行设置
通过Matrix缩放图片