您好,登錄后才能下訂單哦!
這篇文章主要講解了“Android怎么實現圖片設置圓角形式”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Android怎么實現圖片設置圓角形式”吧!
1.自定義的圖片圓角形式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(); /** 保存layer */ int layer = canvas.saveLayer(0.0F, 0.0F, width, height, null, 31); /** 設置drawable的大小 */ localDraw.setBounds(0, 0, width, height); /** 將drawable綁定到bitmap(this.mask)上面(drawable只能通過bitmap顯示出來) */ localDraw.draw(canvas); if ((this.mask == null) || (this.mask.isRecycled())) { this.mask = createOvalBitmap(width, height); } /** 將bitmap畫到canvas上面 */ canvas.drawBitmap(this.mask, 0.0F, 0.0F, this.paint); /** 將畫布復制到layer上 */ 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; /** * 設置橢圓的大小(因為橢圓的最外邊會和border的最外邊重合的,如果圖片最外邊的顏色很深,有看出有棱邊的效果,所以為了讓體驗更加好, * 讓其縮進padding px) */ RectF localRectF = new RectF(padding, padding, width - padding, height - padding); localCanvas.drawOval(localRectF, localPaint); return localBitmap; } }
1.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>
如下圖:
2.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>
3.MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
運行后結果:
感謝各位的閱讀,以上就是“Android怎么實現圖片設置圓角形式”的內容了,經過本文的學習后,相信大家對Android怎么實現圖片設置圓角形式這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。