使ImageView适合CardView的宽度

时间:2014-12-10 06:07:35

标签: android imageview android-appcompat android-cardview

我有一个带圆角的CardView,我希望在顶部有一个ImageView,如下面的材料设计指南所示的示例所示。

components_cards8.png

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
     android:id="@+id/card_view"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     card_view:cardCornerRadius="4dp">

     <!-- ... --> 
 </android.support.v7.widget.CardView>

然后在CardView内,我有ImageView

<ImageView
    android:id="@+id/imageView"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:scaleType="centerCrop"
    android:src="@drawable/default_cover" />

如果我将card_view:cardCornerRadius设置为0dp,那么ImageView就会像我想要的那样适合卡片。

image_1

然而,material design guidelines表示卡片应该有圆角,而不是方角。

我遇到的问题是我将card_view:cardCornerRadius设置为0dp以外的其他内容,例如4dp,然后发生以下情况:

image_2

可以看出,ImageView不适合CardView

我的问题是,当ImageView有圆角时,如何使CardView符合{{1}}的布局。

10 个答案:

答案 0 :(得分:54)

你需要做两件事:

1)在您的CardView上拨打setPreventCornerOverlap(false)

2)在CardView中放置 舍入 Imageview

关于舍入您的imageview,我遇到了同样的问题,因此我创建了一个库,您可以在每个角落设置 不同的半径 。有一个很好的库(vinc3m1的RoundedImageView)支持ImageView上的圆角,但它只支持每个角上相同的半径。但我希望它是圆形 左上角和右上角。

最后我得到了我想要的结果。

https://github.com/pungrue26/SelectableRoundedImageView

Rounded ImageView inside CardView

答案 1 :(得分:21)

如果您的图片尺寸(宽度)固定为ImageView宽度,则只需将ImageView属性更改为:

android:scaleType="fitXY"

就是这样。没有额外的图像转角,没有繁忙的工作。除了它有效的应用程序性能。

注意:我的建议可能不适合大尺寸ImageView的小图片。

答案 2 :(得分:15)

CardView with ImageView

我通过将RoundedImageView置于CardView内来实现此目的。您还需要设置适当的CardView属性。

https://medium.com/@etiennelawlor/layout-tips-for-pre-and-post-lollipop-bcb2e4cdd6b2#.kmb24wtkk

答案 3 :(得分:7)

编辑2015/09/29

https://github.com/vinc3m1/RoundedImageView增加了对所选角落舍入的支持

你也可以使用makeramen RoundedImageView https://github.com/vinc3m1/RoundedImageView, 并删除CardView中的自动填充以用于预先使用LolliPop

yourCardView.setPreventCornerOverlap(假);

然后设置你需要的填充以显示cardview的阴影

答案 4 :(得分:4)

bck_rounded.xml文件夹中设置drawable。给它一个与card_view相同的半径。

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <corners android:radius="4dp" />
    </shape>

在图片中应用视图:android:background="@drawable/bck_rounded"

答案 5 :(得分:1)

我解决了设置
问题 1)app:cardUseCompatPadding="false"
2)设置舍入的imageView背景

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:radius="4dp" />
</shape>

答案 6 :(得分:0)

您必须自定义ImageView。

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

public class RoundedImageView extends android.support.v7.widget.AppCompatImageView {
private Paint mPaint;
private Path mPath;
private Bitmap mBitmap;
private Matrix mMatrix;
private int mRadius = convertDpToPixel(10);
private int mWidth;
private int mHeight;
private Drawable mDrawable;

 public RoundedImageView(Context context) {
    super(context);
    init();
}

public RoundedImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public RoundedImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init() {
    mPaint = new Paint();
    mPaint.setColor(Color.WHITE);

    mPath = new Path();
}

public int convertDpToPixel(int dp) {
    DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics);
}

@Override
public void setImageDrawable(Drawable drawable) {
    mDrawable = drawable;
    if (drawable == null) {
        return;
    }
    mBitmap = drawableToBitmap(drawable);
    int bDIWidth = mBitmap.getWidth();
    int bDIHeight = mBitmap.getHeight();
    //Fit to screen.
    float scale;
    if ((mHeight / (float) bDIHeight) >= (mWidth / (float) bDIWidth)) {
        scale = mHeight / (float) bDIHeight;
    } else {
        scale = mWidth / (float) bDIWidth;
    }
    float borderLeft = (mWidth - (bDIWidth * scale)) / 2;
    float borderTop = (mHeight - (bDIHeight * scale)) / 2;
    mMatrix = getImageMatrix();
    RectF drawableRect = new RectF(0, 0, bDIWidth, bDIHeight);
    RectF viewRect = new RectF(borderLeft, borderTop, (bDIWidth * scale) + borderLeft, (bDIHeight * scale) + borderTop);
    mMatrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
    invalidate();
}

private Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap;
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if (bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }
    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    mWidth = MeasureSpec.getSize(widthMeasureSpec);
    mHeight = MeasureSpec.getSize(heightMeasureSpec);
    if ((mDrawable != null) && (mHeight > 0) && (mWidth > 0)) {
        setImageDrawable(mDrawable);
    }
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mBitmap == null) {
        return;
    }
    canvas.drawColor(Color.TRANSPARENT);
    mPath.reset();
    mPath.moveTo(0, mRadius);
    mPath.lineTo(0, canvas.getHeight());
    mPath.lineTo(canvas.getWidth(), canvas.getHeight());
    mPath.lineTo(canvas.getWidth(), mRadius);
    mPath.quadTo(canvas.getWidth(), 0, canvas.getWidth() - mRadius, 0);
    mPath.lineTo(mRadius, 0);
    mPath.quadTo(0, 0, 0, mRadius);
    canvas.drawPath(mPath, mPaint);
    canvas.clipPath(mPath);
    canvas.drawBitmap(mBitmap, mMatrix, mPaint);
}
}

在layout.xml中

<com.example.widget.RoundedImageViewmageView
            android:id="@+id/ivProductImg"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:scaleType="fitXY"
            />

答案 7 :(得分:0)

有时使用Glide而非Picasso加载图像也有帮助。

答案 8 :(得分:0)

我尝试将cardview和imageview结合使用,所以会像这样:

    android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            app:cardCornerRadius="8dp"
            android:elevation="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/kmp_warna1"
                android:scaleType="centerCrop"/>

  </android.support.v7.widget.CardView>

我只是在cardview上添加了转角半径和高程属性,并且在图像视图上添加了scaletype = centerCrop

希望这会有所帮助

答案 9 :(得分:0)

我尝试使用MaskedCardView为我工作

3.0
NaN
1.0
NaN

并与之一起使用属性app:cardPreventCornerOverlap =“ false”

相关问题