美文网首页
自定义简易声音波纹VIEW

自定义简易声音波纹VIEW

作者: 猿ape | 来源:发表于2018-10-23 10:15 被阅读0次

已经有好久没有写我的文章了,想自己已经快脱离这个行业了,现在想起来也是挺逗的,也是自己偷懒吧。

最近项目中有一些自定义的view需要自己做了,所以我就揽下了一个任务。就是声音采集的时候的动画。当然这种动画在网上有好多种,我就想记录一下思路,以免自己给忘记了。

效果图

没有声音 有声音的时候的跳动

初始化代码

参数设置

核心代码

kotlin代码:

private fun drawSegment(canvas: Canvas?) {

for (item in -(size /2)..(size /2)) {

var actionX =interval * item +centerX

        var moveY = moveY()

canvas!!.save()

canvas!!.drawLine(

actionX.toFloat(),

                (centerY - moveY).toFloat(),

                actionX.toFloat(),

                (centerY + moveY).toFloat(),

                paint

        )

canvas!!.restore()

}

if (range !=0)

postInvalidateDelayed(100L)

}

java代码:

private void drawSegment(Canvas canvas) {

for (int i = -size /2; i

int actionX =interval * i +centerX;

        int moveY = moveY();

        canvas.save();

        canvas.drawLine(

actionX,

                (centerY - moveY),

                actionX,

                (centerY + moveY),

                paint

        );

        canvas.restore();

    }

if (range !=0)

postInvalidateDelayed(100L);

}

画指定的条目线,size是初始定义的条目数量,从中心扩散条目,这样布局会平均一点。效果可以看效果图。

波纹跳动核心代码:

kotlin代码:

private fun moveY(): Int {

if (range ==0) {

range =1

    }

var rand = Random().nextInt(range)

if (rand ==0) {

rand =1

    }

var moveY = Random().nextInt(rand)

if (moveY ==0) {

moveY =1

    }

return moveY

}

java代码:

private int moveY() {

if (range ==0) {

range =1;

    }

int rand =new Random().nextInt(range);

    if (rand ==0) {

rand =1;

    }

int moveY =new Random().nextInt(rand);

    if (moveY ==0) {

moveY =1;

    }

return moveY;

}

通过幅度range来计算变化的大小moveY。这个是条目的高度,通过变化高度来实现跳动的视觉效果。而moveY就是通过range的两次随机数来获取的。代码很清楚。

这样就完成了非常简单的语音波动VIEW,代码非常简单。当然也留了一个坑,哈哈!

完整的代码

kotlin代码:

class RecordingKtView : View {

private var paint: Paint? = Paint(Paint.ANTI_ALIAS_FLAG)

/**

* 跳动的条目熟练

*/

    private var size =36

    /**

* 中心x

*/

    private var centerX =0

    /**

* 中心y

*/

    private var centerY =0

    /**

* 间隔长度

*/

    private var interval =0

    /**

* 跳动幅度

*/

    var range: Int =0

    //初始化数据

    constructor(context: Context?) :super(context)

constructor(context: Context?, attrs: AttributeSet?) :super(context, attrs)

constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) :super(context, attrs, defStyleAttr)

init {

paint!!.color = Color.GREEN

        paint!!.strokeWidth =5f

        paint!!.strokeCap = Paint.Cap.ROUND

    }

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {

super.onSizeChanged(w, h, oldw, oldh)

centerX = w /2

        centerY = h /2

        interval = w /size

    }

override fun onDraw(canvas: Canvas?) {

super.onDraw(canvas)

drawSegment(canvas)

}

private fun drawSegment(canvas: Canvas?) {

for (itemin -(size /2)..(size /2)) {

var actionX =interval * item +centerX

            var moveY = moveY()

canvas!!.save()

canvas!!.drawLine(

actionX.toFloat(),

                    (centerY - moveY).toFloat(),

                    actionX.toFloat(),

                    (centerY + moveY).toFloat(),

                    paint

            )

canvas!!.restore()

}

if (range !=0)

postInvalidateDelayed(100L)

}

private fun moveY(): Int {

if (range ==0) {

range =1

        }

var rand = Random().nextInt(range)

if (rand ==0) {

rand =1

        }

var moveY = Random().nextInt(rand)

if (moveY ==0) {

moveY =1

        }

return moveY

}

}

java代码:

public class RecordingView extends View {

private Paint paint;

    /**

* 条目数量

*/

    private int size =36;

    /**

* 屏幕中心x

*/

    private int centerX =0;

    /**

* 屏幕中心y

*/

    private int centerY =0;

    /**

* 间隔长度

*/

    private int interval =0;

    /**

* 跳动幅度

*/

    private int range =0;

    public RecordingView(Context context) {

super(context);

        init();

    }

public RecordingView(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

        init();

    }

public RecordingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

        init();

    }

private void init() {

paint =new Paint(Paint.ANTI_ALIAS_FLAG);

        paint.setColor(Color.GREEN);

    }

/**

* 改变跳的幅度

    * @param sing

    */

    public void setRange(int sing) {

this.range = sing;

    }

@Override

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

        centerX = w /2;

        centerY = h /2;

        interval = w /size;

    }

@Override

    protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

        drawSegment(canvas);

    }

private void drawSegment(Canvas canvas) {

for (int i = -size /2; i

int actionX =interval * i +centerX;

            int moveY = moveY();

            canvas.save();

            canvas.drawLine(

actionX,

                    (centerY - moveY),

                    actionX,

                    (centerY + moveY),

                    paint

            );

            canvas.restore();

        }

if (range !=0)

postInvalidateDelayed(100L);

    }

private int moveY() {

if (range ==0) {

range =1;

        }

int rand =new Random().nextInt(range);

        if (rand ==0) {

rand =1;

        }

int moveY =new Random().nextInt(rand);

        if (moveY ==0) {

moveY =1;

        }

return moveY;

    }

}

相关文章

网友评论

      本文标题:自定义简易声音波纹VIEW

      本文链接:https://www.haomeiwen.com/subject/tvrozftx.html