0

应用程序将 JSON API 插件与 Glide 库一起用于图像。

例如,这样的高质量图片:

http://previews.123rf.com/images/wavebreakmediamicro/wavebreakmediamicro1403/wavebreakmediamicro140342957/28409409-Group-of-people-running-together-for-fitness-in-the-park-Stock-Photo.jpg

在卡片视图中出现非常模糊: 在此处输入图像描述 在应用程序的其他区域,图像加载正常。

我下载了禁用 JPEG 压缩并重新生成缩略图。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="150dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:layout_margin="5dp"
app:cardBackgroundColor="@color/cardBackground"
app:cardCornerRadius="10dp"
app:cardElevation="5dp"


android:id="@+id/card_view">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Thumbnail image -->
    <ImageView
        android:id="@+id/thumbnail"

        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_gravity="center_horizontal|top"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"/>

    <!-- Article title -->
    <TextView
        android:id="@+id/title"

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:layout_gravity="bottom"
        android:background="@drawable/listgrad"
        android:textColor="@android:color/white"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <!-- Article comment count -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:id="@+id/comment_count"
        android:layout_alignBottom="@+id/thumbnail"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <!-- Article view count -->
    <!--<TextView-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:textAppearance="?android:attr/textAppearanceSmall"-->
    <!--android:text="Small Text"-->
    <!--android:id="@+id/view_count"-->
    <!--android:layout_alignTop="@+id/comment_count"-->
    <!--android:layout_toLeftOf="@+id/comment_count"-->
    <!--android:layout_toStartOf="@+id/comment_count" />-->

</FrameLayout>

适配器

    public class MyRecyclerViewAdaptor extends RecyclerView.Adapter<MyRecyclerViewAdaptor.ViewHolder> {
    // A list of posts
    private List<Post> posts;
    private Context mContext;

    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onItemClick(Post post);
    }

    public MyRecyclerViewAdaptor(ArrayList<Post> posts, OnItemClickListener listener) {
        this.posts = posts;
        mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup viewGroup, final int i) {
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.card_view_item, viewGroup, false);
        mContext = viewGroup.getContext();
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int i) {
        Glide.with(mContext)
                .load(posts.get(i).getThumbnailUrl())
                .centerCrop()
                .into(viewHolder.thumbnailImageView);

        viewHolder.title.setText(posts.get(i).getTitle());

        int count = posts.get(i).getCommentCount();
        String countText = (count == 1 || count == 0) ? count + " Comment" : count + " Comments";
        viewHolder.commentCount.setText(countText);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onItemClick(posts.get(i));
            }
        });
    }

    @Override
    public int getItemCount() {
        return posts.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView thumbnailImageView;
        TextView title;
        TextView commentCount;

        public ViewHolder(View itemView) {
            super(itemView);

            thumbnailImageView = (ImageView) itemView.findViewById(R.id.thumbnail);
            title = (TextView) itemView.findViewById(R.id.title);
            commentCount = (TextView) itemView.findViewById(R.id.comment_count);
        }

    }
  }
4

2 回答 2

0

将您的 ImageView 替换为以下内容:

android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center_horizontal|top"
android:scaleType="fitStart"
android:adjustViewBounds="true"/>
于 2016-09-06T01:58:45.373 回答
0

在您的ImageView情况下,您必须给出修复宽度和高度。像这样

<ImageView
    android:id="@+id/thumbnail"
    android:layout_width="300dp"
    android:layout_height="150dp"
    android:layout_gravity="center_horizontal|top"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop" />

在 Glide 中,您应该像这样加载图像

 Glide.with(mContext)
            .load(posts.get(i).getThumbnailUrl())
            .centerCrop()
            .into(viewHolder.thumbnailImageView);
于 2016-09-06T05:48:37.820 回答