从资源文件目录获取图片:
Drawable drawable = ContextCompat.getDrawable(this, R.mipmap.ic_launcher);
drawable.getBounds(); // 获取Drawable的大小范围
将Drawable转换成Bitmap
Drawable drawable = ContextCompat.getDrawable(this, R.mipmap.ic_launcher); drawable.setBounds(0,0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.draw(canvas);
iv.setImageBitmap(bitmap); //将Drawable转换成的Bitmap设置到ImageView.
获取设置在ImageView上的图片:
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
将资源文件中的图片转成Bitmap有第二种方法:
imageView.setImageResource(R.mipmap.ic_launcher);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
二 : 将Bitmap转为Drawable的方法:
BitmapDrawable bitmapDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
参考:ImageView.setImageBitmap()
获取ImageView上的图片的方法 (CircleImageView)
private Bitmap getBitmapFromDrawable(Drawable drawable) { if (drawable == null) { return null; } if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); }else if (drawable instanceof GlideBitmapDrawable) { GlideBitmapDrawable glideBitmapDrawable = (GlideBitmapDrawable) drawable; return glideBitmapDrawable.getBitmap(); }else if (drawable instanceof SquaringDrawable){ } try { Bitmap bitmap; if (drawable instanceof ColorDrawable) { bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG); } else { bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG); } Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } catch (Exception e) { e.printStackTrace(); return null; } }