onDraw中画一幅已经存在的图,图片缩放操作。
private int width1; // 手机宽度
private int height1; // 手机高度
private int widhtImg; // 图片宽度
private int heightImg; // 图片高度
private double scalePoint; // 缩放比例
// 获取图片资源
Bitmap dstBmp = BitmapFactory.decodeResource(getResources(), R.mipmap.shujia, null);
widhtImg = dstBmp.getWidth();
heightImg = dstBmp.getHeight();
scalePoint = width1 / widhtImg > height1 / heightImg ? height1 / heightImg : width1 / widhtImg;
//按比例缩放图片大小
RectF rectF = new RectF(0, 0, (float) (widhtImg * scalePoint), (float) (heightImg * scalePoint));
canvas.drawBitmap(dstBmp, null, rectF, mPaint);
// 获取手机屏幕宽度和高度方法
public void initWidthAndHeight() {
Resources resources = this.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
float density = dm.density;
width1 = dm.widthPixels;
height1 = dm.heightPixels;
}