//获取Bitmap有关属性public void getBitmapInfo(Bitmap bitmap) { int height = bitmap.getHeight(); int width = bitmap.getWidth(); int density = bitmap.getDensity(); int memory = bitmap.getAllocationByteCount(); Log.d("Bitmap","height:"+ height + ",width" + width +",density" + density + ",memory:" + memory);}
//计算图片采样率,需要从Option获取宽高属性,以及需要的宽高 private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { int inSampleSize = 1; int width = options.outWidth; int height = options.outHeight; if (width > reqWidth || height > reqHeight) { int hafWidth = width / 2; int hafHeight = height / 2; while (hafWidth > reqWidth && hafHeight > reqHeight) { inSampleSize *= 2; hafWidth /= inSampleSize; hafHeight /= inSampleSize; } } return inSampleSize; }
//对外提供的获取所需要的裁剪后的图片 public Bitmap getReqBitmap(Resources resources, int id, int reqWidth, int reqHeight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(resources, id, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(resources, id, options); }public Bitmap optimizeBitmap(Resources resources, int id) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; options.inSampleSize = 2; return BitmapFactory.decodeResource(resources, id, options); } public void getBitmapInfo(Bitmap bitmap) { int height = bitmap.getHeight(); int width = bitmap.getWidth(); int density = bitmap.getDensity(); int memory = bitmap.getAllocationByteCount(); Log.d("Bitmap", "height:" + height + ",width" + width + ",density" + density + ",memory:" + memory); } public void clear() { for (ImageView iv : imageViewList) { Bitmap bitmap = ((BitmapDrawable) iv.getDrawable()).getBitmap(); if (bitmap != null && !bitmap.isRecycled()) { bitmap.recycle(); bitmap = null; iv.setImageBitmap(null); } } imageViewList.clear(); }
//图片区域显示public void showPart() { try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; InputStream inputStream = getAssets().open("long1.jpg"); BitmapFactory.decodeStream(inputStream, null, options); Rect rect = new Rect(options.outWidth / 2 - 100, options.outHeight / 2 - 100, options.outWidth / 2 + 100, options.outHeight / 2 + 100); options.inJustDecodeBounds = false; BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(inputStream, false); Bitmap bitmap = decoder.decodeRegion(rect, options); imageView.setImageBitmap(bitmap); } catch (IOException e) { e.printStackTrace(); } }