
最近项目上要实现语音搜索功能,界面样式要模仿一下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,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,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));}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android自定义控件实现UC浏览器语音搜索效果全部内容,希望文章能够帮你解决Android自定义控件实现UC浏览器语音搜索效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)