您好,登錄后才能下訂單哦!
本文實例為大家分享了自定義Drawable實現圓形和圓角的具體代碼,供大家參考,具體內容如下
圓形
package com.customview.widget; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.Shader; import android.graphics.drawable.Drawable; import android.support.annotation.IntRange; import android.support.annotation.NonNull; import android.support.annotation.Nullable; /** * 自定義Drawable 實現圓形圖片 * Created by hust_twj on 2017/9/26. */ public class CircleDrawable extends Drawable { private Paint mPaint; private int mWidth; //寬/高,直徑 private Bitmap mBitmap; public CircleDrawable(Bitmap bitmap) { this.mBitmap = bitmap; BitmapShader bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);//著色器 水平和豎直都需要填充滿 mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setShader(bitmapShader); mWidth = Math.min(mBitmap.getWidth(), mBitmap.getHeight()); //寬高的最小值作為直徑 } @Override public void draw(@NonNull Canvas canvas) { canvas.drawCircle(mWidth/2, mWidth/2, mWidth/2, mPaint); //繪制圓形 } @Override public void setAlpha(@IntRange(from = 0, to = 255) int i) { mPaint.setAlpha(i); } @Override public void setColorFilter(@Nullable ColorFilter colorFilter) { mPaint.setColorFilter(colorFilter); } @Override public int getOpacity() { return PixelFormat.TRANSLUCENT; //設置系統默認,讓drawable支持和窗口一樣的透明度 } //還需要從重寫以下2個方法,返回drawable實際寬高 @Override public int getIntrinsicWidth() { return mWidth; } @Override public int getIntrinsicHeight() { return mWidth; } }
圓角:
package com.customview.widget; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.RectF; import android.graphics.Shader; import android.graphics.drawable.Drawable; import android.support.annotation.IntRange; import android.support.annotation.NonNull; import android.support.annotation.Nullable; /** * 自定義Drawable 實現圓角圖片 * Created by hust_twj on 2017/9/26. */ public class RoundCornerDrawable extends Drawable { private Paint mPaint; private RectF rectF;//定義矩形區域 private Bitmap mBitmap; public RoundCornerDrawable(Bitmap bitmap) { this.mBitmap = bitmap; BitmapShader bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);//著色器 水平和豎直都需要填充滿 mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setShader(bitmapShader); //rectF = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()); } @Override public void draw(@NonNull Canvas canvas) { //RectF:圓角矩形區域 canvas.drawRoundRect(new RectF(0, 0, mBitmap.getWidth(), mBitmap.getHeight()),100, 100, mPaint);//rx:x方向上的圓角半徑; ry:y方向上的圓角半徑。 } /* @Override public void setBounds(int left, int top, int right, int bottom) { super.setBounds(left, top, right, bottom); rectF = new RectF(left, top, right, bottom); }*/ @Override public void setAlpha(@IntRange(from = 0, to = 255) int i) { mPaint.setAlpha(i); } @Override public void setColorFilter(@Nullable ColorFilter colorFilter) { mPaint.setColorFilter(colorFilter); } @Override public int getOpacity() { return PixelFormat.TRANSLUCENT; //設置系統默認,讓drawable支持和窗口一樣的透明度 } //還需要從重寫以下2個方法,返回drawable實際寬高 @Override public int getIntrinsicWidth() { return mBitmap.getWidth(); } @Override public int getIntrinsicHeight() { return mBitmap.getHeight(); } }
MainActivity中:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.XXX); imageView.setImageDrawable(new RoundCornerDrawable(bitmap));
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。