Corners ImgeView适合在CardView内部没有半径像CardView在Android中的角落

Corners ImgeView适合在CardView内部没有半径像CardView在Android中的角落,第1张

概述我正在开发一个 Android应用程序.在我的应用程序中,我一起使用CardView和ImageView.但我在设计CardView内部的ImageView时遇到了问题.问题在于ImageView的圆角半径半径.请参阅下面的我的场景. I have xml layout for adapter item like this. <?xml version="1.0" encoding="utf-8" 我正在开发一个 Android应用程序.在我的应用程序中,我一起使用CardVIEw和ImageVIEw.但我在设计CardVIEw内部的ImageVIEw时遇到了问题.问题在于ImageVIEw的圆角半径半径.请参阅下面的我的场景.

I have xml layout for adapter item like this.

<?xml version="1.0" enCoding="utf-8"?><androID.support.v7.Widget.CardVIEw xmlns:card_vIEw="http://schemas.androID.com/apk/res-auto"    xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_margintop="5dp"    androID:layout_marginleft="5dp"    androID:layout_marginRight="5dp"    card_vIEw:cardCornerRadius="5dp"    androID:layout_wIDth="match_parent"    androID:ID="@+ID/di_card_container"    androID:layout_height="wrap_content">    <relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent">        <ImageVIEw            androID:ID="@+ID/di_iv_image"            androID:scaleType="centerCrop"            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content" />        <relativeLayout            androID:padding="10dp"            androID:layout_below="@ID/di_iv_image"            androID:layout_alignParentleft="true"            androID:layout_alignParentBottom="true"            androID:ID="@+ID/di_name_container"            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content">            <TextVIEw                androID:textSize="15dp"                androID:textcolor="@color/textcolorPrimary"                androID:ID="@+ID/di_tv_name"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content" />        </relativeLayout>    </relativeLayout></androID.support.v7.Widget.CardVIEw>

正如您所看到的那样,我将CardVIEw的圆角半径设置为5dp,而ImageVIEw with则适合其父CardVIEw的宽度.问题是ImageVIEw的顶角不像其父CardVIEw的角落那样弯曲.

This is the screenshot

通常,CardVIEw的子视图角会自动弯曲,如果它适合父CardVIEw.对?那么为什么我的ImageVIEw不工作?请问我的代码有什么问题?我该怎么办呢?

解决方法 所以这是前棒棒糖的常见行为.以下是修复它的步骤:

第1步:将以下属性添加到您的cardVIEw

card_vIEw:cardUseCompatpadding="true"card_vIEw:cardPreventCornerOverlap="false"card_vIEw:cardCornerRadius="10dp"

第2步:使用围绕其顶部边框的自定义ImageVIEw:

public class RoundedtopImageVIEw extends ImageVIEw {private Paint                       mPaint;private Path                        mPath;private Bitmap                      mBitmap;private Matrix                      mMatrix;private int                         mRadius = displayUtils.convertDptopixel(10);private int                         mWIDth;private int                         mHeight;private Drawable                    mDrawable;public RoundedtopImageVIEw(Context context) {    super(context);    init();}public RoundedtopImageVIEw(Context context,AttributeSet attrs) {    super(context,attrs);    init();}public RoundedtopImageVIEw(Context context,AttributeSet attrs,int defStyleAttr) {    super(context,attrs,defStyleAttr);    init();}private voID init() {    mPaint = new Paint();    mPaint.setcolor(color.WHITE);    mPath = new Path();}@OverrIDepublic 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,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,canvas.getWIDth(),canvas.getHeight());    drawable.draw(canvas);    return bitmap;}@OverrIDeprotected 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);    }}@OverrIDeprotected 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(),mRadius);    mPath.quadTo(canvas.getWIDth(),canvas.getWIDth() - mRadius,0);    mPath.lineto(mRadius,0);    mPath.quadTo(0,mRadius);    canvas.drawPath(mPath,mPaint);    canvas.clipPath(mPath);    canvas.drawBitmap(mBitmap,mMatrix,mPaint);}}

第3步:只需使用RoundedtopImageVIEw替换xml中的ImageVIEw

第4步:在代码中将其用作常规imageVIEw,例如使用Picasso:

RoundedtopImageVIEw image = (RoundedtopImageVIEw) findVIEwByID(R.ID.di_iv_image);Picasso.with(context)                    .load("Some cool Url")                    .into(image);

编辑:添加了convertDptopixel函数

抱歉,我忘了添加这个,这是一个Util类的一部分,你可以在任何你想要的地方添加(在我的displayUtils类中):

public static int convertDptopixel(int dp) {    displayMetrics displayMetrics = Resources.getSystem().getdisplayMetrics();    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dp,displayMetrics);}
总结

以上是内存溢出为你收集整理的Corners ImgeView适合在CardView内部没有半径像CardView在Android中的角落全部内容,希望文章能够帮你解决Corners ImgeView适合在CardView内部没有半径像CardView在Android中的角落所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1138670.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存