成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁技術文章
文章詳情頁

Android自定義View圓形圖片控件代碼詳解

瀏覽:7日期:2022-09-21 14:29:48

前言

在日常開發中,圓形的圖片效果還是很常見的。可以通過給Paint設置Xfermode來實現,這里簡單記錄如下。

實現

實現圓形效果的核心是PorterDuffXfermode,對于PorterDuffXfermode,這里不展開,可以查詢相關資料。

核心代碼

//繪制背景canvas.drawCircle(mSize / 2, mSize / 2, mSize / 2, mPaint);//設置模式為:顯示背景層和上層的交集,且顯示上層圖像mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//繪制要顯示的圖像canvas.drawBitmap(mSrcBitmap, 0, 0, mPaint);//重置XfermodemPaint.setXfermode(null);

自定義屬性

<?xml version='1.0' encoding='utf-8'?><resources> <declare-styleable name='CircleView'> <!--定義資源--> <attr name='src' format='reference' /> <!--定義類型--> <attr name='type' format='enum'> <!--圓形--> <enum name='round' value='1' /> <!--矩形--> <enum name='rect' value='2' /> </attr> </declare-styleable></resources>

自定義控件

public class CircleView extends View { private static final int DEFAULT_SIZE = 200; private static final int DEFAULT_RADIUS = 20; private static final int TYPE_ROUND = 1; private static final int TYPE_RECT = 2; private int mSize; private int mResourceId; private int mType; private Paint mPaint; private Bitmap mSrcBitmap; public CircleView(Context context) { this(context, null); } public CircleView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CircleView); mResourceId = ta.getResourceId(R.styleable.CircleView_src, R.mipmap.ic_launcher); mType = ta.getInt(R.styleable.CircleView_type, TYPE_ROUND); ta.recycle(); init(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getMeasureSize(widthMeasureSpec); int height = getMeasureSize(heightMeasureSpec); mSize = Math.min(width, height); setMeasuredDimension(mSize, mSize); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //繪制背景 if (mSrcBitmap == null) { mSrcBitmap = getScaleBitmap(); } if (mType == TYPE_ROUND) { canvas.drawCircle(mSize / 2, mSize / 2, mSize / 2, mPaint); } else if (mType == TYPE_RECT) { canvas.drawRoundRect(0, 0, mSize, mSize, DEFAULT_RADIUS, DEFAULT_RADIUS, mPaint); } //設置模式為:顯示背景層和上層的交集,且顯示上層圖像 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); //繪制要顯示的圖像 canvas.drawBitmap(mSrcBitmap, 0, 0, mPaint); //重置Xfermode mPaint.setXfermode(null); } private void init() { //禁用硬件加速,否則可能無法繪制圓形 setLayerType(LAYER_TYPE_HARDWARE, null); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.FILL); } private int getMeasureSize(int measureSpec) { int mode = MeasureSpec.getMode(measureSpec); int size = MeasureSpec.getSize(measureSpec); return mode == MeasureSpec.EXACTLY ? size : DEFAULT_SIZE; } /** * 獲取縮放后的Bitmap * * @return */ private Bitmap getScaleBitmap() { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), mResourceId, options); options.inSampleSize = calcSampleSize(options, mSize, mSize); options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(getResources(), mResourceId, options); } /** * 計算縮放比例 * * @param option * @param width * @param height * @return */ private int calcSampleSize(BitmapFactory.Options option, int width, int height) { int originWidth = option.outWidth; int originHeight = option.outHeight; int sampleSize = 1; while ((originWidth = originWidth >> 1) > width && (originHeight = originHeight >> 1) > height) { sampleSize = sampleSize << 1; } return sampleSize; }}

注意:如果沒有圓形的效果,那么可能需要禁用硬件加速:setLayerType(LAYER_TYPE_HARDWARE, null)

布局

<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' android:gravity='center_horizontal' android:orientation='vertical' tools:context='.MainActivity'> <com.wangyz.custom.CircleView android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_margin='10dp' app:src='http://m.cgvv.com.cn/bcjs/@drawable/image' /> <com.wangyz.custom.CircleView android:layout_width='100dp' android:layout_height='100dp' android:layout_margin='10dp' app:src='http://m.cgvv.com.cn/bcjs/@drawable/image' /> <com.wangyz.custom.CircleView android:layout_width='100dp' android:layout_height='100dp' android:layout_margin='10dp' app:src='http://m.cgvv.com.cn/bcjs/@drawable/image' app:type='rect' /></LinearLayout>

效果

Android自定義View圓形圖片控件代碼詳解

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 成年美女黄网站色视频大全免费 | 国产乱码精品一区二区三区四川人 | 精品国产一区二区三区不卡蜜臂 | 极品色在线精品视频 | 波多野结衣免费视频观看 | 亚洲 欧美 精品 中文第三 | 欧美三级在线观看不卡视频 | 精品国产一区二区三区成人 | 亚洲欧美日韩综合久久久久 | 中美日韩在线网免费毛片视频 | 欧美在线一级毛片观看 | 在线精品亚洲 | 久久久久久久久久免观看 | 久久99精品久久久久久秒播 | 精品国产综合成人亚洲区 | 特黄特a级特别特级特毛片 特黄特黄 | 成人a毛片免费全部播放 | 视频一区免费 | 国产欧美亚洲精品a | 久久精品网站免费观看 | 欧美午夜毛片a级在线 | 亚洲精品日韩专区在线观看 | 欧美日韩一区二区三区视频在线观看 | 国产美女一区二区三区 | 中国女警察一级毛片视频 | 日韩在线视屏 | 久久精品视频免费观看 | 国产精品福利视频萌白酱 | 亚洲第一区视频 | 亚洲人成在线播放网站 | 色欧美在线 | 男女一级爽爽快视频 | 欧美激情综合亚洲五月蜜桃 | 欧美videos娇小 | avav男人天堂 | 成人亚洲精品一区 | 91精品国产免费 | 久久精品国产在爱久久 | 色天使影院 | 国产特黄特色的大片观看免费视频 | 精品三级视频 |