cardview的圆角我想制作如上所示的界面。图像不固定,这意味着我可以将图像传输给其他人。我想使用xml文件中的打包。类似于,
<com.example.widgets.RoundedImageView android: layout_width = "39dp" android: layout_height = "39dp" android: src = "@ drawable / your_drawable" />
qcbq4gxm1#
你可以使用这个组件。https://github.com/pungrue26/SelectableRoundedImageView
<com.utility.SelectableRoundedImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:sriv_left_top_corner_radius="55dp" app:sriv_right_top_corner_radius="0dp" android:scaleType="fitXY" />
0ve6wy6x2#
如果您不想使用任何第三方库,则可以自定义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" />
e5nqia273#
将imageView放入CardView中并设置CardView的角半径
I already answered in the link below :
在ImageView中设置圆角图像
l2osamch4#
你的可绘制代码应该像这样..
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- <solid android:color="add your color here"/>--> <solid android:color="#00000"/> <stroke android:color="add your color here" android:width="1dp"/> <corners android:radius="5dp"/> </shape>
XML格式
<com.example.widgets.RoundedImageView android: layout_width = "39dp" android: layout_height = "39dp" android:background="here is your background source" android: src = "@ drawable / your_drawable" />
4条答案
按热度按时间qcbq4gxm1#
你可以使用这个组件。https://github.com/pungrue26/SelectableRoundedImageView
0ve6wy6x2#
如果您不想使用任何第三方库,则可以自定义ImageView。
在layout.xml中
e5nqia273#
将imageView放入CardView中并设置CardView的角半径
在ImageView中设置圆角图像
l2osamch4#
你的可绘制代码应该像这样..
XML格式