这篇文章主要讲解了“Android怎么实现图片设置圆角形式”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android怎么实现图片设置圆角形式”吧!自定义的图片圆角形式CircleImage
这篇文章主要讲解了“Android怎么实现图片设置圆角形式”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android怎么实现图片设置圆角形式”吧!
自定义的图片圆角形式CircleImageView类
public class CircleImageView extends ImageView { private static final Xfermode MASK_XFERMODE; private Bitmap mask; private Paint paint; private int mBorderWidth = 10; private int mBorderColor = Color.parseColor("#f2f2f2"); private boolean useDefaultStyle = false; static { PorterDuff.Mode localMode = PorterDuff.Mode.DST_IN; MASK_XFERMODE = new PorterDuffXfermode(localMode); } public CircleImageView(Context context) { super(context); } public CircleImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircleImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircularImage); mBorderColor = a.getColor(R.styleable.CircularImage_border_color, mBorderColor); final int def = (int) (2 * context.getResources().getDisplayMetrics().density + 0.5f); mBorderWidth = a.getDimensionPixelOffset(R.styleable.CircularImage_border_width, def); a.recycle(); } private void useDefaultStyle(boolean useDefaultStyle) { this.useDefaultStyle = useDefaultStyle; } @Override protected void onDraw(canvas canvas) { if (useDefaultStyle) { super.onDraw(canvas); return; } final Drawable localDraw = getDrawable(); if (localDraw == null) { return; } if (localDraw instanceof NinePatchDrawable) { return; } if (this.paint == null) { final Paint localPaint = new Paint(); localPaint.setFilterBitmap(false); localPaint.setAntiAlias(true); localPaint.setXfermode(MASK_XFERMODE); this.paint = localPaint; } final int width = getWidth(); final int height = getHeight(); int layer = canvas.saveLayer(0.0F, 0.0F, width, height, null, 31); localDraw.setBounds(0, 0, width, height); localDraw.draw(canvas); if ((this.mask == null) || (this.mask.isRecycled())) { this.mask = createOvalBitmap(width, height); } canvas.drawBitmap(this.mask, 0.0F, 0.0F, this.paint); canvas.restoreToCount(layer); drawBorder(canvas, width, height); } private void drawBorder(Canvas canvas, final int width, final int height) { if (mBorderWidth == 0) { return; } final Paint mBorderPaint = new Paint(); mBorderPaint.setStyle(Paint.Style.STROKE); mBorderPaint.setAntiAlias(true); mBorderPaint.setColor(mBorderColor); mBorderPaint.setStrokeWidth(mBorderWidth); canvas.drawCircle(width / 2, height / 2, (width - mBorderWidth) / 2, mBorderPaint); canvas = null; } public Bitmap createOvalBitmap(final int width, final int height) { Bitmap.Config localConfig = Bitmap.Config.ARGB_8888; Bitmap localBitmap = Bitmap.createBitmap(width, height, localConfig); Canvas localCanvas = new Canvas(localBitmap); Paint localPaint = new Paint(); final int padding = (mBorderWidth - 3) > 0 ? mBorderWidth - 3 : 1; RectF localRectF = new RectF(padding, padding, width - padding, height - padding); localCanvas.drawOval(localRectF, localPaint); return localBitmap; }}
1 在values目录下创建一个circle_attr.xml,文件内容:
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="CircularImage"> <attr name="border_width" fORMat="dimension" /> <attr name="border_color" format="color" /> </declare-styleable></resources>
如下图:
activity_main.xml
<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/ic_sp" /> <com.demo.test.bitmap.CircleImageView android:layout_width="100dp" android:layout_height="100dp" android:layout_marginLeft="50dp" android:src="@drawable/ic_sp" /> </LinearLayout></LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}
运行后结果:
感谢各位的阅读,以上就是“Android怎么实现图片设置圆角形式”的内容了,经过本文的学习后,相信大家对Android怎么实现图片设置圆角形式这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!
--结束END--
本文标题: Android怎么实现图片设置圆角形式
本文链接: https://lsjlt.com/news/300549.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0