Android自定义控件实现UC浏览器语音搜索效果

所属分类: 软件编程 / Android 阅读数: 1641
收藏 0 赞 0 分享

 最近项目上要实现语音搜索功能,界面样式要模仿一下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);
}

@Override
public void onClick(View v) {
  Random random=new Random();
  customView.setStrokeWidth(random.nextInt(100));
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android框架学习之Volley和Glide详解

这篇文章主要给大家介绍了关于Android框架学习之Volley和Glide的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android中Fragment的基本用法示例总结

Fragment是activity的界面中的一部分或一种行为,下面这篇文章主要给大家介绍了关于Android中Fragment的基本用法的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
收藏 0 赞 0 分享

Android.mk引入第三方jar包和so库文件的方法

这篇文章主要介绍了Android.mk引入第三方jar包和so库文件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android仿微信录制小视频

这篇文章主要为大家详细介绍了Android仿微信录制小视频,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

android实现一键锁屏和一键卸载的方法实例

这篇文章主要给大家介绍了关于android如何实现一键锁屏和一键卸载的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

Android手势密码--设置和校验功能的实现代码

这篇文章主要介绍了Android手势密码--设置和校验功能的实现代码,非常不错,具有一定的参考校验价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Kotlin学习笔记之const val与val

这篇文章主要给大家介绍了关于Kotlin学习笔记之const val与val的相关资料,并给大家介绍了const val和val区别以及Kotlin中var和val的区别,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android实现调用系统分享功能示例的总结

这篇文章主要介绍了通过Android调用系统分享文本信息、单张图片、多个文件和指定分享到微信、QQ,同时分享图片和文字的功能示例,小编觉得挺不错,一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android自定义view实现输入控件

这篇文章主要为大家详细介绍了Android自定义view实现输入控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码

这篇文章主要介绍了Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码的实例代码,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多