软件编程
位置:首页>> 软件编程>> Android编程>> Android自定义控件实现UC浏览器语音搜索效果

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

作者:ludi  发布时间:2022-06-01 06:59:40 

标签:Android,语音,搜索

 最近项目上要实现语音搜索功能,界面样式要模仿一下UC浏览器的样式,UC浏览器中有一个控件,会随着声音大小浮动,然后寻思偷个懒,百度一下,结果也没有找到类似的,只能自己动手了。

先上图看我实现的效果:

Android自定义控件实现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));
}
0
投稿

猜你喜欢

  • 一、C#语言的简单介绍根据微软官网的介绍,C#是为.NET平台量身订做的一种面向对象的语言,它与Java类似都属于从C++演变(提取了C++
  • 众所周知,面向对象编程的特点为:封装、继承、多态。C#是一门完全面向对象的语言,由于比Java推出的时间还要晚,所以对面向对象的思想的体现比
  • 前言在H5火热的时代,许多框架都出了底部弹窗的控件,在H5被称为弹出菜单ActionSheet,今天我们也来模仿一个ios的底部弹窗,取材于
  • 导语本章根据百度地图API,实现仿钉钉打卡功能。用到了基础地图、覆盖物、定位图层、陀螺仪方法、悬浮信息弹框。百度地图API地址  
  • 在.NET 4.0(当然也包括4.0以前的版本)下,用反射判断某个方法是否运用了自定义Attribute时,可以通过调用MethodInfo
  • 简介在移动开发中,如果我们要实现一些图像处理相关的功能,难免要用到OpenCV。而OpenCV是用c++开发的。我们在Android中,需要
  • java的接口解耦方式我只想把抽象的东西说的具体,或者说,听起来简单些,明白些。。。学过java的人都知道,java是单继承的,也就是说一个
  • 1:@Qualifier@Qualifier 注释指定注入 Bean 的名称,这样歧义就消除了。所以@Auto
  • 简介项目需要...直接展示效果吧:原理使用UGUI提供的ScrollRect和ScrollBar组件实现基本滑动以及自己控制每次移动一页来达
  • 实现的效果图:自定义Fragment继承BottomSheetDialogFragment重写它的三个方法:onCreateDialog()
  • 本文实例为大家分享了java实现猜数字游戏的具体代码,供大家参考,具体内容如下随机生成0~100的数字,通过控制台输入猜测数字,输出进行提示
  • 在 C# 中,程序中在运行时出现的错误,会不断在程序中进行传播,这种机制称为“异常”。异常通常由错误的代码引发,并由能够更正错误的代码进行
  • C++编写的一个图书管理系统,供大家参考,具体内容如下2018大一的课设,搬到这纪念一下,共1200多行代码为图书管理人员编写一个图书管理系
  • 前言最近公司在重构广告系统,其中核心的打包功由广告系统调用,即对apk打包的调用和打包完成之后的回调,需要提供相应的接口给广告系统。因此,为
  • 一、添加pom.xml依赖<parent>    <groupId>org.springfram
  • JWT简介Json Web Token(JWT):JSON网络令牌,是为了在网络应用环境间传递声明而制定的一种基于JSON的开放标准((RF
  • 一、整体设计1、需求分析池化技术是计算机中的一种设计模式,内存池是常见的池化技术之一,它能够有效的提高内存的申请和释放效率以及内存碎片等问题
  • 很久以来都想研究一下利用java操作Excel的方法,今天没事,就稍微了解了一下,特总结一下。利用java操作Excel,有个开源的东东-j
  • 引言综合应用Java的GUI编程和网络编程,实现一个能够支持多组用户同时使用的聊天室软件。该聊天室具有比较友好的GUI界面,并使用C/S模式
  • 一、可变参数方法的定义首先看下可变参数方法在代码上是如何定义的,如下所示:public static void method1(Intege
手机版 软件编程 asp之家 www.aspxhome.com