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

本文介绍如何在Android中创建一个自定义视图,该视图模仿UC浏览器的语音搜索功能,随着音量大小改变而浮动的圆环效果。通过自定义CircleView类,设置圆环和浮动圆的颜色、宽度,并在onDraw方法中绘制图形。在布局文件中使用自定义属性来配置图标、颜色等。通过调整传入的宽度值,可以实现圆环宽度的变化,模拟音量浮动效果。
摘要由CSDN通过智能技术生成

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

先上图看我实现的效果:

65bb2d802eaae62701861366ad233b72.png

这是自定义控件的代码,里面注释也很明白,就不费话了

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文件 代码:

在布局中的使用:

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值