0

我正在尝试将远程 URL 中的图像加载到 Litho Image 小部件中,但 Litho 小部件具有“可绘制”作为设置图像的唯一道具。有没有人尝试在 Litho Image 小部件中从远程 URL 设置图像?

4

3 回答 3

2

您可以使用将使用 Fresco 图像加载库来下载和渲染图像的 FrescoComponent。或者,您可以使用与 Glide 的集成: https ://github.com/pavlospt/litho-glide/tree/master/litho-glide

于 2017-11-23T05:25:06.883 回答
0

Litho-Fresco 不支持远程图像。你必须在你的gradle中implementation 'com.facebook.fresco:fresco:1.13.0'添加implementation 'com.facebook.litho:litho-fresco:0.31.0'。然后您可以通过以下方式加载远程图像:

@LayoutSpec
object MovieComponentSpec {

fun getImageController(imageUrl : String) = Fresco.newDraweeControllerBuilder()
    .setUri(imageUrl)
    .build()


@JvmStatic
@OnCreateLayout
internal fun onCreateLayout(
    c: ComponentContext,
    @Prop title: String,
    @Prop imageUrl: String
): Component {

    return Column.create(c)
        .paddingDip(ALL, 16f)
        .backgroundColor(Color.WHITE)
        .child(
            FrescoImage.create(c).controller(getImageController(imageUrl))
        )
        .child(
            Text.create(c)
                .text(title)
                .textSizeSp(10f)
        )
        .build()
    }

}
于 2019-11-22T06:16:16.013 回答
-2

如果你真的想使用Litho,你可以下载图像,并将其转换为Drawable对象。

public static Drawable drawableFromUrl(String url) throws IOException {
    Bitmap b = null;

    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.connect();
    InputStream input = connection.getInputStream();

    b = BitmapFactory.decodeStream(input);

    return new BitmapDrawable(b);
}

请注意,您需要单独调用此方法,Thread或者AsyncTask因为这是网络操作。

于 2017-10-25T18:02:25.243 回答