【问题标题】:Appcompat CardView and Picasso no rounded CornersAppcompat CardView 和 Picasso 没有圆角
【发布时间】:2014-10-24 16:37:48
【问题描述】:

我不知道我应该在哪里解决这个问题,如果是我的错,Picasso Lib 或 Cardview 库中存在某些问题。

基本上我有一个 CardView 包含一个图像(全卡覆盖)和一个 TextView 覆盖。

Android 5.0 设备上运行代码时,一切正常,并且图像得到了 圆角

但是,如果我在 5.0 之前的设备上运行它,图像会与Cardlayout 重叠并且没有圆角。

您可以在此图片上查看比较:

这里有一些代码片段:

layout_row.xml

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

    <ImageView
        android:id="@+id/pandaImage"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/pandaName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/pandaImage"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@color/photo_tint"
        android:clickable="true"
        android:focusable="true"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:textSize="24sp" />

</RelativeLayout>

以及加载图像的回收器适配器:

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    Photo p = photos.get(i);
    Picasso.with(mContext).load(p.getUrl()).fit().into(viewHolder.mImage);
    viewHolder.mPandaName.setText(p.getTitle());
}

【问题讨论】:

  • 如果将图片设置为卡片的背景会怎样?

标签: android android-appcompat picasso cardlayout


【解决方案1】:

根据文档,这是设计的:

由于圆角剪裁成本高昂,在 L 之前的平台上,CardView 不会剪裁与圆角相交的子项。相反,它添加了填充以避免这种交叉(参见 setPreventCornerOverlap(boolean) 来改变这种行为)。

请参阅the CardView docs 了解更多信息。

【讨论】:

  • 在 pre L 设备上设置 setPreventCornerOverlap(false) 时,图像视图超过了CardView 并与阴影对齐。对此有什么想法吗?
  • @mike.b93 我相信你引用了这个错误code.google.com/p/android/issues/detail?id=77821 我的评论似乎解决了 Pre-L 的问题。至少在我测试过的一台设备上。
【解决方案2】:

正如@kcoppock 所说,这是设计使然。

这是我在这种情况下会做的事情。

1) 您可以使用 Picasso Transformation 接口为您的图像指定自定义转换(在我们的例子中 - 带有圆角的图像)

2) 将此转换应用于 pre-L 设备上的 Picasso 请求

3) 由于 CardView 为图像添加了一些边距 - 在 pre-L 设备上通过调用 setPreventOverlap(false) 删除它

回到代码:

自定义转换:

public class RoundedTransformation implements com.squareup.picasso.Transformation {
    private final int radius;
    private final int margin;

    public RoundedTransformation(final int radius, final int margin) {
        this.radius = radius;
        this.margin = margin;
    }

    @Override
    public Bitmap transform(final Bitmap source) {
        final Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));

        Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        canvas.drawRoundRect(new RectF(margin, margin, source.getWidth() - margin, source.getHeight() - margin), radius, radius, paint);

        if (source != output) {
            source.recycle();
        }

        return output;
    }

    @Override
    public String key() {
        return "rounded(radius=" + radius + ", margin=" + margin + ")";
    }
}

毕加索:

//feel free to play with radius to match your CardView
Picasso.with(mContext).load(p.getUrl()).transform(new RoundedTransformation(12, 0)).fit().into(viewHolder.mImage);

【讨论】:

  • setPreventOverlap(false) 在处理卡片背景时是一个非常重要的调用。很高兴在这里提到它。
  • 很好的解决方案!如何只使顶角变圆而底角保持方形?
  • @jiawen 不幸的是,实现一个圆角效果比仅仅调用一个 API 需要更多的工作。通常它需要两次绘制。可以参考SO上的类似问题
  • @PavelDudka。我在这里找到了一个很好的解决方案:github.com/wasabeef/picasso-transformations/blob/master/… 它允许您自定义要圆角的角。
【解决方案3】:

这样对我有用:

  1. ImageView 替换为RoundedImageView (https://github.com/vinc3m1/RoundedImageView)。
  2. RoundedImageView 上的riv_corner_radius 属性设置为与CardView 的角相同。
  3. CardView (app:cardPreventCornerOverlap="false") 上将cardPreventCornerOverlap 设置为false。
  4. 现在 L 和 pre-L 看起来一样。

【讨论】:

    【解决方案4】:

    如果您希望获得针对该问题的全局解决方案,您可以使用Carbon's CardView。它正确地将其内容剪辑到所有设备上的圆角返回 Froyo。看图:

    【讨论】:

      【解决方案5】:

      使用下面的代码。

      重要提示:不要在 XML 中为 ImageView 设置背景。

      <android.support.v7.widget.CardView 
                                          android:layout_width="match_parent"
                                          android:layout_height="130dp"
                                          app:cardCornerRadius="5dp"
                                          app:cardElevation="0dp"
                                          app:cardUseCompatPadding="true">
      
          <RelativeLayout
              android:id="@+id/rl_target_marry"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_alignParentLeft="true"
              android:layout_alignParentStart="true"
              android:layout_below="@+id/textView2"
              >
      
              <ImageView
                  android:id="@+id/img_target_marry"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:scaleType="fitXY"
                  />
              <FrameLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:foreground="?attr/selectableItemBackground">
      
                  <TextView
                      android:id="@+id/textView"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignParentEnd="true"
                      android:layout_alignParentRight="true"
                      android:layout_centerVertical="true"
                      android:layout_gravity="right|center_vertical"
                      android:layout_marginRight="16dp"
                      android:text="Marry"
                      android:textColor="@color/colorWhite"
                      android:textSize="28sp"/>
      
              </FrameLayout>
      
          </RelativeLayout>
      </android.support.v7.widget.CardView>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-10
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多