Html.HtmlFrom中的Fresco lib

时间:2015-10-06 13:56:07

标签: android fresco

我已经在我的项目中使用Fresco库了。 我想知道我们是否也可以在ImageGetter Html.HtmlFrom中使用它。 这是我的代码作为示例。

public class ItemActivity extends AppCompatActivity implements Html.ImageGetter {

    private TextView contentTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        contentTextView = (TextView) this.findViewById(R.id.itemContentTextView);

        String content = getIntent().getStringExtra("content");

        contentTextView.setText(Html.fromHtml(content, this, null));
    }

    @Override
    public Drawable getDrawable(String source) {
        return null;
    }
}

我尝试使用如下:

public Drawable getDrawable(String source) {
        SimpleDraweeView draweeView = new SimpleDraweeView(getBaseContext());
        draweeView.setImageURI(Uri.parse(source));
        return draweeView.getDrawable();
    }

但没有加载任何图片。 我有什么方法可以做到吗?

2 个答案:

答案 0 :(得分:0)

您需要使用getTopLevelDrawable(),而不是getDrawable()

答案 1 :(得分:0)

public Drawable getDrawable(String source) {
        if (Strings.isNullOrEmpty(source)) {
            return null;
        }
        SimpleDraweeView simpleDraweeView = new SimpleDraweeView(mContext);
        ImageRequestBuilder imageRequestBuilder = ImageRequestBuilder.newBuilderWithSource(Uri.parse(source));
        DraweeController draweeController = Fresco.newDraweeControllerBuilder()
                .setImageRequest(imageRequestBuilder.build())
                .setOldController(simpleDraweeView.getController())
                .setAutoPlayAnimations(true)
                .build();
        simpleDraweeView.setController(draweeController);
        Drawable drawable = simpleDraweeView.getDrawable();
        drawable.setBounds(0, 0, 100, 100);
        return drawable;
    }