从ImageView获取缩放的位图,可以通过以下步骤实现:
Drawable drawable = imageView.getDrawable();
if (drawable instanceof BitmapDrawable) {
// 是BitmapDrawable类型,可以直接获取Bitmap对象
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
} else {
// 不是BitmapDrawable类型,需要将Drawable转换为Bitmap
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
}
float scaleFactor = (float) drawable.getIntrinsicWidth() / (float) imageView.getWidth();
int scaledWidth = (int) (drawable.getIntrinsicWidth() / scaleFactor);
int scaledHeight = (int) (drawable.getIntrinsicHeight() / scaleFactor);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
现在,scaledBitmap
就是ImageView中显示的缩放后的位图。
领取专属 10元无门槛券
手把手带您无忧上云