简体   繁体   English

滑动从ViewPager中的ImageView获取位图

[英]Get bitmap from imageview in viewpager with glide

I'm showing in imageview with glide. 我正在滑动显示在imageview中。 User can save image on button click. 用户可以单击按钮保存图像。

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

when i use this code it save whole screen image(Image and black space). 当我使用此代码时,它将保存整个屏幕图像(图像和黑色空间)。

    imageViewPreview.setDrawingCacheEnabled(true);
    imageViewPreview.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    imageViewPreview.layout(0, 0,
            imageViewPreview.getMeasuredWidth(), imageViewPreview.getMeasuredHeight());
    imageViewPreview.buildDrawingCache(true);
    Bitmap bitmap = Bitmap.createBitmap(imageViewPreview.getDrawingCache());
    imageViewPreview.setDrawingCacheEnabled(false);

when is use this, sometime it work but image get cropped in half or image go to up like "android:layout_alignParentTop="true" . 什么时候使用它,有时它可以工作,但是图像被裁剪一半或图像像“ android:layout_alignParentTop =” true“一样上升。

You can get Bitmap from DrawingCache. 您可以从DrawingCache获取Bitmap After Settting image in ImageView you can get it as below: ImageView设置图像后,您可以如下所示:

    imageViewPreview.setDrawingCacheEnabled(true);
    imageViewPreview.buildDrawingCache(true);
    final Bitmap bmp = Bitmap.createBitmap(layout.getDrawingCache());
    imageViewPreview.setDrawingCacheEnabled(false);

If you need full size image then this is not right way i think .A better solution would be to get it From Glide as Bitmap . 如果您需要全尺寸的图像,那么我认为这是不正确的方式。更好的解决方案是从Glide获得Bitmap If full image was loaded already it will be quick (from cache) if not then it will load the full image from server or you can still use Drawing cache in case of No internet connection and glide failure. 如果已经加载了完整图像,则将快速(从缓存中)加载(如果不是),那么它将从服务器加载完整图像,或者在没有互联网连接和滑行失败的情况下,您仍然可以使用Drawing cache

Glide.with(imageView.getContext())
    .asBitmap()
    .load("imageUrl")
    .into(new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
           // Use resource here    
           }
        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM