Android自定義控件實(shí)現(xiàn)UC瀏覽器語(yǔ)音搜索效果
最近項(xiàng)目上要實(shí)現(xiàn)語(yǔ)音搜索功能,界面樣式要模仿一下UC瀏覽器的樣式,UC瀏覽器中有一個(gè)控件,會(huì)隨著聲音大小浮動(dòng),然后尋思偷個(gè)懶,百度一下,結(jié)果也沒有找到類似的,只能自己動(dòng)手了。
先上圖看我實(shí)現(xiàn)的效果:
這是自定義控件的代碼,里面注釋也很明白,就不費(fèi)話了
public class CustomCircleView extends View{ private Paint mPaint; private int strokeWidth = 0; //圓環(huán)的寬度 private Bitmap bitmap = null; // 圖片位圖 private int nBitmapWidth = 0; // 圖片的寬度 private int nBitmapHeight = 0; // 圖片的高度 private int width; //view的寬度 private int height ; //view的高度 private int bigCircleColor =0; //view的高度 private int floatCircleColor =0; //view的高度 public CustomCircleView(Context context) { this(context, null); } public CustomCircleView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomCircleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomCircleView, defStyleAttr, 0); int n = a.getIndexCount(); for (int i = 0; i < n; i++) { int attr = a.getIndex(i); switch (attr) { case R.styleable.CustomCircleView_icon: bitmap = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, 0)); break; case R.styleable.CustomCircleView_bigCircleColor: bigCircleColor = a.getColor(attr, Color.GRAY); break; case R.styleable.CustomCircleView_floatCircleColor: floatCircleColor = a.getColor(attr,Color.GREEN); break; } } a.recycle(); mPaint = new Paint(); //如果布局中沒有設(shè)置bigCircleColor和floatCircleColor的時(shí)候給他一個(gè)默認(rèn)值 if (bigCircleColor==0){ bigCircleColor=Color.parseColor("#FFEEF0F1"); } if (floatCircleColor==0){ floatCircleColor=Color.parseColor("#25c1f5"); } // 獲取圖片高度和寬度 nBitmapWidth = bitmap.getWidth(); nBitmapHeight = bitmap.getHeight(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); //獲取view的高度和寬度 這個(gè)view必須給精確值?。。。。。。?! if (widthMode == MeasureSpec.EXACTLY) { width = widthSize; } if (heightMode == MeasureSpec.EXACTLY) { height = heightSize; } setMeasuredDimension(width, height); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setAntiAlias(true); // 消除鋸齒 //繪制最外層灰色大圓 mPaint.setColor(bigCircleColor); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(height/2-nBitmapHeight/2); //計(jì)算圓的半徑稍微麻煩點(diǎn),但是在圖上畫一下應(yīng)該能明白 (height/2-nBitmapHeight/2)/2+nBitmapHeight/2 canvas.drawCircle(width/2, height/2, (height/2-nBitmapHeight/2)/2+nBitmapHeight/2, mPaint); //繪制浮動(dòng)的圓 mPaint.setColor(floatCircleColor); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(strokeWidth); canvas.drawCircle(width/2, height/2, strokeWidth/2+nBitmapHeight/2, mPaint); //繪制中間圖標(biāo) canvas.drawBitmap(bitmap, width/2-nBitmapWidth/2, height/2-nBitmapHeight/2, mPaint); } //根據(jù)傳入的寬度重新繪制 public void setStrokeWidth(int with){ this.strokeWidth=with; invalidate(); } }
在res/values 下建一個(gè)attrs文件 代碼:
<resources> <declare-styleable name="CustomCircleView"> <attr name="bigCircleColor" format="color" /> <attr name="floatCircleColor" format="color" /> <attr name="icon" format="reference" /> </declare-styleable> </resources>
在布局中的使用:
<com.example.administrator.mycustomcircleview.CustomCircleView android:id="@+id/customView" android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" app:icon="@mipmap/voice_icon" app:floatCircleColor="@color/colorAccent" app:bigCircleColor="@color/colorPrimaryDark" />
高度寬度一定要給精確值,切記?。。?!顏色值可以不設(shè)定,默認(rèn)就是我上面圖的效果。
然后根據(jù)音量大小直接傳入數(shù)值就可以了,很簡(jiǎn)單的使用方法,這里我用隨機(jī)數(shù)代替了。
customView = ((CustomCircleView) findViewById(R.id.customView)); button = ((Button) findViewById(R.id.button)); button.setOnClickListener(this); } @Override public void onClick(View v) { Random random=new Random(); customView.setStrokeWidth(random.nextInt(100)); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解決Android SearchView不顯示搜索icon的問(wèn)題
- Android開發(fā)之搜索框SearchView用法示例
- Android自定義View實(shí)現(xiàn)搜索框(SearchView)功能
- Android仿簡(jiǎn)書動(dòng)態(tài)searchview搜索欄效果
- Android搜索框(SearchView)的功能和用法詳解
- Android搜索框SearchView屬性和用法詳解
- Android SearchView搜索框組件的使用方法
- Android搜索框組件SearchView的基本使用方法
- 可支持快速搜索篩選的Android自定義選擇控件
- Android?SearchView搜索控件使用方法詳解
相關(guān)文章
Android Studio3.0升級(jí)后使用注意事項(xiàng)及解決方法
這篇文章主要介紹了Android Studio3.0升級(jí)后使用注意事項(xiàng)及解決方法,需要的朋友參考下吧2017-12-12Android操作系統(tǒng)的架構(gòu)設(shè)計(jì)分析
這篇文章主要介紹了Android操作系統(tǒng)的架構(gòu)設(shè)計(jì)分析,Android系統(tǒng)架構(gòu)分為L(zhǎng)inux內(nèi)核驅(qū)動(dòng)、C/C ++框架、Java框架、Java應(yīng)用程序,本文分別講解了它的作用,需要的朋友可以參考下2015-06-06Android數(shù)據(jù)持久化之File機(jī)制分析
這篇文章主要介紹了Android數(shù)據(jù)持久化之File機(jī)制,較為詳細(xì)的分析了File機(jī)制的原理及Android使用File實(shí)現(xiàn)數(shù)據(jù)持久化的相關(guān)操作技巧,需要的朋友可以參考下2017-05-05android手機(jī)獲取唯一標(biāo)識(shí)的方法
這篇文章主要 為大家詳細(xì)介紹了android手機(jī)獲取唯一標(biāo)識(shí)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Android實(shí)現(xiàn)短信加密功能(發(fā)送加密短信、解密本地短信)
這篇文章主要介紹了android實(shí)現(xiàn)短信加密功能的相關(guān)資料,功能包括發(fā)送加密短信、解密本地短信,感興趣的小伙伴們可以參考一下2016-01-01Android TouchListener實(shí)現(xiàn)拖拽刪實(shí)例代碼
這篇文章主要介紹了Android TouchListener實(shí)現(xiàn)拖拽刪實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02