国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Android實(shí)現(xiàn)帶圓環(huán)的圓形頭像

瀏覽:82日期:2022-09-23 08:14:43

在最近寫的一個天氣APP中用到了圓形頭像這樣的一個樣式,中間是圓形的頭像(被圓形切割的圖片),周圍是一個帶顏色的圓環(huán)。如下圖所示,今天就來說一所它的實(shí)現(xiàn)過程。

Android實(shí)現(xiàn)帶圓環(huán)的圓形頭像

它的實(shí)現(xiàn)也不是特別困難,其實(shí)就是用到了BitmapShader這個用法,然后包裝成一個paint,最后畫出一個圓。

1>實(shí)現(xiàn)一個Paint畫出以圓形背景的圓。

2>以同樣的圓形畫出一個稍微小一點(diǎn)的圓,作為它的有色圓環(huán)。(此圓和上一個圓顏色不同)。

3>用BitmapShader實(shí)現(xiàn)一個新的圓,和第二個圓的大小圓心一致。

(BitmapShader只能在onDraw中實(shí)現(xiàn),在其他外部無法實(shí)現(xiàn))

具體代碼如下:

1、界面代碼

<?xml version='1.0' encoding='utf-8'?><RelativeLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' android: android:layout_width='match_parent' android:layout_height='match_parent' android:paddingBottom='@dimen/activity_vertical_margin' android:paddingLeft='@dimen/activity_horizontal_margin' android:paddingRight='@dimen/activity_horizontal_margin' android:paddingTop='@dimen/activity_vertical_margin' tools:context='com.example.app_switchbutton.CircleWeatherActivity'> <com.example.app_switchbutton.CircleWeather android:layout_width='250dp' android:layout_height='wrap_content' android: android:layout_centerHorizontal='true'/> </RelativeLayout>

2、邏輯java代碼:

package com.example.app_switchbutton; import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.BitmapShader;import android.graphics.Canvas;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.Shader;import android.util.AttributeSet;import android.view.View; /** * Created by 盡途 on 2017/5/12. */ public class CircleWeather extends View { private int widthSize; private int heightSize; private Paint mpaint1,mpaint2,mpaint3; private Bitmap mbitmap; private BitmapShader mbitmapshader; public CircleWeather(Context context){ super(context); initView(); } public CircleWeather(Context context, AttributeSet attributeSet){ super(context,attributeSet); initView(); } private void initView(){ mpaint1=new Paint(); mpaint2=new Paint(); mpaint3=new Paint(); mpaint2.setStyle(Paint.Style.FILL); mpaint3.setStyle(Paint.Style.FILL); mpaint2.setAntiAlias(true); mpaint3.setAntiAlias(true); mpaint2.setColor(getResources().getColor(R.color.colorPrimary)); mpaint3.setColor(getResources().getColor(R.color.colorGray)); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { widthSize=MeasureSpec.getSize(widthMeasureSpec); heightSize=widthSize; setMeasuredDimension(widthSize,heightSize); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); } @Override protected void onDraw(Canvas canvas) { mbitmap= BitmapFactory.decodeResource(getResources(),R.drawable.hehua);//bitmapshader只能在onDraw中實(shí)現(xiàn)在外部不可以 int BitmapWidthSize=mbitmap.getWidth(); int BitmapHeightSize=mbitmap.getHeight(); float scale=(float)widthSize/Math.min(BitmapHeightSize,BitmapWidthSize);//獲取最為合適的尺寸 Matrix matrix=new Matrix(); matrix.setScale(scale,scale); Bitmap bitmap=Bitmap.createBitmap(mbitmap,0,0,BitmapWidthSize,BitmapHeightSize,matrix,true); mbitmapshader=new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP); mpaint1.setShader(mbitmapshader); canvas.drawCircle((float)widthSize*0.5f,(float)heightSize*0.5f,(float)heightSize*0.5f,mpaint2); canvas.drawCircle((float)widthSize*0.5f,(float)heightSize*0.5f,(float)heightSize*0.47f,mpaint3); canvas.drawCircle((float)widthSize*0.5f,(float)heightSize*0.5f,(float)heightSize*0.47f,mpaint1); super.onDraw(canvas); }}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 无棣县| 黑水县| 运城市| 资中县| 札达县| 巫山县| 汕尾市| 宜兰市| 新平| 建湖县| 射阳县| 锦州市| 崇文区| 镇巴县| 湟中县| 扶沟县| 淮安市| 宜兰市| 万载县| 和顺县| 岳西县| 邵阳县| 巩留县| 北川| 木兰县| 鹤山市| 云浮市| 监利县| 康平县| 阿荣旗| 张家口市| 鲁山县| 随州市| 龙江县| 麻江县| 武强县| 湘乡市| 景洪市| 阿鲁科尔沁旗| 彭州市| 合川市|