您的位置:首页 > 大数据 > 人工智能

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

2015-08-28 18:35 806 查看

程序崩潰:

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/(他遇到的問題和我一樣,真該吐槽以下,封裝太好,改起來麻煩)[/code] 
最後,解決了,用了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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: