简体   繁体   English

Kotlin Android Studio-从newsapi加载缩略图

[英]Kotlin Android Studio - load thumbnails from newsapi

I'm using this news api to pull the json data into my app and render it in my articlerecycler adapter class. 我正在使用此新闻api将json数据提取到我的应用中,并在我的articlerecycler适配器类中进行呈现。 From the api there is a json value urlToImage that contains the thumbnail that I need to render. 从api中有一个json值urlToImage ,其中包含我需要渲染的缩略图。 Currently I have a working list of 20 or so articles displaying the author, title, description, etc. 目前,我有20篇左右的文章的工作清单,其中显示了作者,标题,描述等。

What is the best way to show the thumbnails as well? 显示缩略图的最佳方法是什么? Is using ImageView widget the best way to handle this? 使用ImageView小部件是处理此问题的最佳方法吗?

For example, 例如,

    <ImageView
    android:id="@+id/thumbnailIv"
    android:layout_width="94dp"
    android:layout_height="93dp"
    android:layout_weight="1"
    android:contentDescription="@string/urlToImage"
    android:scaleType="fitCenter"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/placeholder_image" />

I've found through other posts that picasso is a good way to lazy load images, specifically thumbnails. 通过其他帖子,我发现毕加索是延迟加载图像(特别是缩略图)的好方法。

    // load the image with Picasso
    Picasso.get()
            .load("") // load the image
            .into(thumbnailIv) // select the ImageVi

  }

Yes use ImageView and use library like Glide or picasso to load image into ImageView from the url 是的,请使用ImageView并使用Glidepicasso类的库将图像从url加载到ImageView

 GlideApp.with(context)
            .load(url)
            .diskCacheStrategy(DiskCacheStrategy.DATA) //optional
            .placeholder(R.drawable.default_image)  //optional
            .error(R.drawable.default_image)  //optional
            .into(holder.imageNews);

With ImageView you can control ScaleType another useful property is android:adjustViewBounds="true" which will maintain aspect ratio . 使用ImageView可以控制ScaleType另一个有用的属性是android:adjustViewBounds="true" ,它将保持宽高比。 If you use use linear/relative and set image as background then you cannot control these properties 如果使用线性/相对并且将图像设置为背景,则无法控制这些属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM