将位图附加到ImageView

给定


ImageView image = R.findViewById(R.id.imageView);

image.setImageBitmap(someBitmap);

是否可以检索位图?


慕哥9229398
浏览 358回答 3
3回答

胡子哥哥

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

鸿蒙传说

这将使您Bitmap从中受益ImageView。但是,它与您设置的位图对象不同。这是一个新的。imageView.buildDrawingCache();Bitmap bitmap = imageView.getDrawingCache();===编辑=== imageView.setDrawingCacheEnabled(true); imageView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); imageView.layout(0, 0,                   imageView.getMeasuredWidth(), imageView.getMeasuredHeight());  imageView.buildDrawingCache(true); Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache()); imageView.setDrawingCacheEnabled(false);

慕后森

这段代码更好。public static  byte[] getByteArrayFromImageView(ImageView imageView)    {        BitmapDrawable bitmapDrawable = ((BitmapDrawable) imageView.getDrawable());        Bitmap bitmap;        if(bitmapDrawable==null){            imageView.buildDrawingCache();            bitmap = imageView.getDrawingCache();            imageView.buildDrawingCache(false);        }else        {            bitmap = bitmapDrawable .getBitmap();        }        ByteArrayOutputStream stream = new ByteArrayOutputStream();        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);        return stream.toByteArray();    }
打开App,查看更多内容
随时随地看视频慕课网APP