8.28筆記 Failure getting entry for 0x...、fresco獲取bitmap

程序崩潰:

Failure getting entry for 0x00…
主要原因是因為資源不存在,如果是多語言,R.string.name只存在于中文語言,而不存在英文語言,也會崩潰,而且比較難發現。

讓fresco的SimpleDraweeView根據圖片比例調整寬度

做項目的時候,首頁的圖片大小不一致,如果尺寸都固定了,要麼圖片會被壓小,要麼圖片就會被放大,部分像素看不到,如果是系統控件可以這樣設置:
http://blog.csdn.net/xx0624236/article/details/7592153

android:adjustViewBounds="true"  
android:maxHeight="150.0dip"  
android:maxWidth="150.0dip"  
android:minHeight="33.0dip"  
android:minWidth="48.0dip"  

但是,項目用的是SimpleDraweeView,發現根本不生效,於是重新它,奇怪的是,以下方法不根本沒有被調用:

    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
    }

    @Override
    public void setImageDrawable(Drawable drawable) {
        super.setImageDrawable(drawable);
    }

getDrawable 和 getTopLevelDrawable都是facebook定義的drawable,獲取getBounds得到的和view的寬高一樣,想獲取drawable的bitmap又是一層一層的,不知到那層才能獲取到bitmap,心塞塞的,最後,上了google搜索,搜索結果果然不一樣:
搜索以下幾個結果:

  http://stackoverflow.com/questions/30477827/why-use-fresco-datasource-to-get-bitmap-is-empty
 http://icemanlife.com/?p=138
  http://relex.me/photo-drawee-view/(他遇到的問題和我一樣,真該吐槽以下,封裝太好,改起來麻煩)

最後,解決了,用了1個多小時時間,這段代碼,用于固定高度,根據網絡圖片自動調節view的寬度,使他不變形看上去好看一些。

/**
 * 獲取bitmap在調整尺寸,狗日的facebook圖片加載
 * http://stackoverflow.com/questions/30477827/why-use-fresco-datasource-to-get-bitmap-is-empty
 * http://icemanlife.com/?p=138
 * http://relex.me/photo-drawee-view/
 *
 * Created by george.yang on 2015/8/28.
 */
public class CustomDraweeView extends SimpleDraweeView {
    public CustomDraweeView(Context context, GenericDraweeHierarchy hierarchy) {
        super(context, hierarchy);
    }

    public CustomDraweeView(Context context) {
        super(context);
    }

    public CustomDraweeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomDraweeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }


    private Uri mUri;
    private boolean isResize;

    @Override
    public void setImageURI(Uri uri) {
        super.setImageURI(uri);
        if (mUri!=uri) {
            mUri = uri;
            isResize = false;
        }

        getTopLevelDrawable().getBounds()
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mUri!=null) {
            resetWidth(mUri);
        }
        super.onDraw(canvas);
    }

    private void resetWidth (Uri uri) {
        if (isResize) {
            return;
        }

        com.facebook.imagepipeline.request.ImageRequest imageRequest =  ImageRequestBuilder
                .newBuilderWithSource(uri)
                .setProgressiveRenderingEnabled(true)
                .build();
        ImagePipeline imagePipeline = Fresco.getImagePipeline();
        DataSource<CloseableReference<CloseableImage>> dataSource =
                imagePipeline.fetchImageFromBitmapCache(imageRequest, null);
        dataSource.subscribe(new BaseBitmapDataSubscriber() {
            @Override
            public void onNewResultImpl(@Nullable Bitmap bitmap) {
                if (bitmap != null) {
                    ViewGroup.LayoutParams params = CustomDraweeView.this.getLayoutParams();
                    try {
                        float tagWidth = params.height*1f / (bitmap.getHeight()*1f/bitmap.getWidth());
                        params.width = (int)tagWidth;
                        CustomDraweeView.this.setLayoutParams(params);
                        isResize = true;
                    } catch (Exception e) {

                    }
                }
            }

            @Override
            public void onFailureImpl(DataSource dataSource) {
            }
        }, CallerThreadExecutor.getInstance());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值