1、Android 图片加载过大导致内存溢出解决办法
public void loadImage(){
//取得手机窗体的宽高
WindowManager wm = this.getWind
owManager();
int screenWidth = wm.getDefaultDisplay().getWidth();
int screenHeight = wm.getDefaultDisplay().getHeight();
//通过图片文件的信息,得到图片的宽高
BitmapFactory.Options options = new Options();//解析位图附加条件
options.inJustDecodeBounds = true; //不去解析图像,只得到图像的文件信息
Bitmap bitmap = BitmapFactory.decodeFile("File path",options);
//得到图片宽高
int imageWidth = options.outWidth;
int imageHeight = options.outHeight;
//计算缩放比
int scaleWidth = imageWidth / screenWidth;
int scaleHeight = imageHeight / screenHeight;
//取得合适缩放比
int scale = 1;
if(scaleWidth > scaleHeight && scaleHeight > 1){
scale = scaleWidth;
}
if(scaleHeight > scaleWidth && scaleWidth > 1){
scale = scaleHeight;
}
//加载真是图片
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile("File Path", options);
//将图片显示在你要显示的控件上
//建议使用WeakReference代替强引用
}
2.拷贝图片
public void copyImage(){
//导入位图
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),R.drawable.ic_lock_idle_alarm);
//创建一个图形的副本,此时的这个副本是空白的
Bitmap alterBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
//准备一个画板,在上面放上准备好的空白的位图
Canvas canvas = new Canvas();
//准备一个画笔
Paint paint = new Paint();
paint.setColor(Color.WHITE);
//画图
Matrix m = new Matrix();
canvas.drawBitmap(bitmap,m,paint);
}
3、图形的缩放
//图形缩放函数(行列式)
Matrix matrix = new Matrix();
matrix.setValues(new float[] {
1, 0, 0,
0, 1, 0,
0, 0, 1
});
x = 1x + 0y + 0z
y = 0x + 1y + 0z
z = 0x + 0y + 1z
通过canvas.drawBitmap(bmp, matrix, paint);
创建bitmap;
(1)水平缩放0.5
(2)垂直拉扯2倍
matrix.setScale(1.5f,1);//水平点放大到1.5f,垂直1
4、图形的旋转
Matrix matrix = new Matrix();
matrix.setRotate(15);
canvas.drawBitmap(bmp, matrix, paint);
5、消除锯齿
paint.setAntiAlias(true);
6、指定圆心的旋转
matrix.setRotate(15,bmp.getWidth()/2,bmp.getHeight()/2);
Matrix matrix = new Matrix();
matrix.setRotate(15,bmp.getWidth()/2,bmp.getHeight()/2);
alteredBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(),
matrix, false);
alteredImageView.setImageBitmap(alteredBitmap);