国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義控件實現UC瀏覽器語音搜索效果

2019-12-12 03:01:25
字體:
來源:轉載
供稿:網友

 最近項目上要實現語音搜索功能,界面樣式要模仿一下UC瀏覽器的樣式,UC瀏覽器中有一個控件,會隨著聲音大小浮動,然后尋思偷個懶,百度一下,結果也沒有找到類似的,只能自己動手了。

先上圖看我實現的效果:

這是自定義控件的代碼,里面注釋也很明白,就不費話了

public class CustomCircleView extends View{  private Paint mPaint;  private int strokeWidth = 0;   //圓環的寬度  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();    //如果布局中沒有設置bigCircleColor和floatCircleColor的時候給他一個默認值    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的高度和寬度 這個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);    //計算圓的半徑稍微麻煩點,但是在圖上畫一下應該能明白 (height/2-nBitmapHeight/2)/2+nBitmapHeight/2    canvas.drawCircle(width/2, height/2, (height/2-nBitmapHeight/2)/2+nBitmapHeight/2, mPaint);    //繪制浮動的圓    mPaint.setColor(floatCircleColor);    mPaint.setStyle(Paint.Style.STROKE);    mPaint.setStrokeWidth(strokeWidth);    canvas.drawCircle(width/2, height/2, strokeWidth/2+nBitmapHeight/2, mPaint);    //繪制中間圖標    canvas.drawBitmap(bitmap, width/2-nBitmapWidth/2, height/2-nBitmapHeight/2, mPaint);  }  //根據傳入的寬度重新繪制  public void setStrokeWidth(int with){    this.strokeWidth=with;    invalidate();  }}

在res/values 下建一個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"  />

高度寬度一定要給精確值,切記啊!!!顏色值可以不設定,默認就是我上面圖的效果。
然后根據音量大小直接傳入數值就可以了,很簡單的使用方法,這里我用隨機數代替了。

customView = ((CustomCircleView) findViewById(R.id.customView));  button = ((Button) findViewById(R.id.button));  button.setOnClickListener(this);}@Overridepublic void onClick(View v) {  Random random=new Random();  customView.setStrokeWidth(random.nextInt(100));}

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平凉市| 乌拉特前旗| 满城县| 岢岚县| 革吉县| 绵阳市| 于田县| 永宁县| 堆龙德庆县| 余姚市| 孟州市| 禹州市| 华阴市| 新田县| 阿巴嘎旗| 塔河县| 淮滨县| 桑日县| 德昌县| 阿图什市| 邢台市| 桃江县| 瓦房店市| 芦山县| 湾仔区| 汪清县| 胶南市| 四会市| 琼中| 天等县| 兰西县| 广宗县| 广河县| 启东市| 台前县| 亳州市| 蕉岭县| 德格县| 牟定县| 亳州市| 西青区|