qq_24287783
在六月等一场雪
采纳率83.3%
2018-12-24 18:30 阅读 1.3k

Fresco加载网络图片部分设备出现显示部分图片为黑色并崩溃

1.在使用Fresco显示网络图片在天猫魔盒M17设备时,出现部分图片显示为黑色,且一段时间后程序崩溃。

相关代码:
public class TvSimpleDraweeView extends SimpleDraweeView implements ShimmerViewBase {

private float mCornerRadius;
private Paint mPaint;
private Path mPath;
private Drawable mForeground;
private Rect paddingRect;

private ShimmerViewHelper shimmerViewHelper;

public TvSimpleDraweeView(Context context) {
    this(context, null);
}

public TvSimpleDraweeView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public TvSimpleDraweeView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(attrs);
    shimmerViewHelper = new ShimmerViewHelper(this, new Paint(), attrs);
    shimmerViewHelper.setPrimaryColor(0x66ffffff);
    shimmerViewHelper.setReflectionColor(0x01ffffff);
    setWillNotDraw(false);
}

private void init(AttributeSet attrs) {
    if (attrs != null) {
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable
                .TvSimpleDraweeView);
        mCornerRadius = ta.getDimension(R.styleable.TvSimpleDraweeView_tvCornerRadius, 0f);
        mForeground = ta.getDrawable(R.styleable.TvSimpleDraweeView_tvForegroundDrawable);
        ta.recycle();
    }
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    mPath = new Path();
    if (mForeground == null) {
        mForeground = ContextCompat.getDrawable(getContext(), R.drawable.rect_common_cursor);
    }
    paddingRect = new Rect();
    mForeground.getPadding(paddingRect);
    setFocusable(true);
    setClickable(true);
}

@Override
public void draw(Canvas canvas) {
    int count = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

    super.draw(canvas);
    canvas.drawPath(mPath, mPaint);
    canvas.restoreToCount(count);

    if (hasFocus() || showFocusForced) {
        mForeground.draw(canvas);
    }
    if (shimmerViewHelper != null) {
        shimmerViewHelper.onDraw(canvas);
    }
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    mPath.reset();
    mPath.addRoundRect(new RectF(0, 0, w, h), mCornerRadius, mCornerRadius, Path.Direction.CW);
    mForeground.setBounds(-paddingRect.left, -paddingRect.top, w + paddingRect.right, h +
            paddingRect.bottom);
    if (shimmerViewHelper != null) {
        shimmerViewHelper.onSizeChanged();
    }
}

public void setCornerRadius(float cornerRadius) {
    mCornerRadius = cornerRadius;
}

public void setResImage(int resId) {
    setImageURI(new Uri.Builder().scheme(UriUtil.LOCAL_RESOURCE_SCHEME).path(String.valueOf
            (resId)).build());
}

private boolean showFocusForced = false;

public void forceShowFocus() {
    showFocusForced = true;
    invalidate();
}

public void hideFocus() {
    showFocusForced = false;
    invalidate();
}

@Override
public float getGradientX() {
    return shimmerViewHelper.getGradientX();
}

@Override
public void setGradientX(float gradientX) {
    shimmerViewHelper.setGradientX(gradientX);
}

@Override
public boolean isShimmering() {
    return shimmerViewHelper.isShimmering();
}

@Override
public void setShimmering(boolean isShimmering) {
    shimmerViewHelper.setShimmering(isShimmering);
}

@Override
public boolean isSetUp() {
    return shimmerViewHelper.isSetUp();
}

@Override
public void setAnimationSetupCallback(ShimmerViewHelper.AnimationSetupCallback callback) {
    shimmerViewHelper.setAnimationSetupCallback(callback);
}

@Override
public int getPrimaryColor() {
    return shimmerViewHelper.getPrimaryColor();
}

@Override
public void setPrimaryColor(int primaryColor) {
    shimmerViewHelper.setPrimaryColor(primaryColor);
}

@Override
public int getReflectionColor() {
    return shimmerViewHelper.getReflectionColor();
}

@Override
public void setReflectionColor(int reflectionColor) {
    shimmerViewHelper.setReflectionColor(reflectionColor);
}

private Shimmer mShimmer;

@Override
protected void onFocusChanged(boolean gainFocus, int direction, @Nullable Rect
        previouslyFocusedRect) {
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
    if (gainFocus) {
        showShimmer();
    } else {
        hideShimmer();
    }
}

public void hideShimmer() {
    if (null != mShimmer && mShimmer.isAnimating()) {
        mShimmer.cancel();
    }
}

public void showShimmer() {
    hideShimmer();
    mShimmer = new Shimmer();
    mShimmer.setRepeatCount(0);
    mShimmer.setDuration(1000);
    mShimmer.start(this);
}

}

public class ColumnBoxView1 extends TvSimpleDraweeView implements ColumnBoxView,
View.OnClickListener {

public ColumnBoxView1(Context context) {
    super(context);
    init();
}

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

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

ColumnBoxInfo mColumnBoxInfo;

private void init() {
    setCornerRadius(6f);
    getHierarchy().setPlaceholderImage(R.drawable.ic_holder, ScalingUtils.ScaleType.FIT_XY);
    setOnClickListener(this);
}


@Override
public void setColumnBoxInfo(ColumnBoxInfo columnBoxInfo) {
    mColumnBoxInfo = columnBoxInfo;
    setImageURI(columnBoxInfo.getCoverPath());

// SimpleDraweeViewUtils.setBlurImage(this,columnBoxInfo.getCoverPath());
}

@Override
protected void onFocusChanged(boolean gainFocus, int direction, @Nullable Rect previouslyFocusedRect) {
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
    if (gainFocus) {
        bringToFront();
    }
}

@Override
public void onClick(View v) {
    ColumnBoxUtils.onClick(mColumnBoxInfo, getContext());
}

}

尝试把setImageURI方法替换为
public static void setBlurImage(SimpleDraweeView draweeView, String path) {
ImageRequest request = ImageRequestBuilder.
newBuilderWithSource(Uri.parse(path))
.setPostprocessor(new BlurPostprocessor(draweeView.getContext()))
.build();
PipelineDraweeController controller = (PipelineDraweeController) Fresco
.newDraweeControllerBuilder()
.setImageRequest(request)
.setOldController(draweeView.getController())
.build();
draweeView.setController(controller);
}
后,原本正常显示的图片变得模糊,且还是部分图片显示黑色。
图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    l419070777 l419070777 2020-04-15 18:29

    内存溢出oom 了。后台配置的图片清晰度过高,服务器裁剪成需要显示的大小就可以了

    点赞 评论 复制链接分享

相关推荐