仿抖音的音乐旋转

这次是实现一个仿抖音的音乐旋转自定义View,先看一下效果

img_7c4f1aeb0026a2d08774d4971e1a14a0.gif
效果图

实现这个效果主要是采用的拼凑的方法,即先实现音符动画再实现图片旋转动画然后将两个效果合并到一起。

img_eab640f6d4dd773af8bf8b957695a72d.png
image

先看下概念图

img_e24befe93cd0c02837d2674cb83fa80e.png
概念图

●音符动画

音符动画这里是利用贝塞尔曲线+PathMeasure+ValueAnimator来实现的
img_710d4c2f1db4038be8bde4fe4ff543ba.png
音符动画概念
1.贝塞尔曲线的绘制:因为音符的运动轨迹是自下而上的,因此我们在添加Path路径的时候需要先将起点移到右下角,然后再绘制贝塞尔曲线。
path = new Path();
//将起点移到右下角
path.moveTo(getWidth(),getHeight()-getWidth()/6);
//绘制自下而上的贝塞尔曲线
path.quadTo(0,getHeight(),getWidth()/4,0);
img_5a16b57427b68baf75c94e09ab1ae415.gif
image
2.PathMeasure+ValueAnimator实现音符沿轨迹运动
private void initPath() {
    //新建两个float数组pos用来存储每个轨迹点的坐标,tan用来存储正切值
    pos = new float[2];
    tan = new float[2];
    path = new Path();
    path.moveTo(getWidth(),getHeight()-getWidth()/6);
    path.quadTo(0,getHeight(),getWidth()/4,0);
    pathMeasure = new PathMeasure(path,false);
    length = pathMeasure.getLength();
    valueAnimator = ValueAnimator.ofFloat(0,2f);
    valueAnimator.setDuration(3000);
    //设置重复执行动画
    valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
    //设置为匀速运动
    valueAnimator.setInterpolator(new LinearInterpolator());
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float temp=(float) animation.getAnimatedValue();
            val= temp/2;
            //这里实现音符的透明度从0~1~0的效果
            if(temp>1){
                Music3.this.setAlpha(Math.abs(temp-2f));
            }else {
                Music3.this.setAlpha(temp);
            }
            //更新界面
            invalidate();
        }
    });
    valueAnimator.start();
}
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //获取每个点对应的坐标
    pathMeasure.getPosTan(length*val,pos,tan);
    //创建音符BitMap宽高是逐渐放大的
    scaledBitmap = Bitmap.createScaledBitmap(bitmap, (int)(getWidth()/5*val)+4, (int)(getWidth()/5*val)+4, true);
    canvas.drawPath(path,paint);
    canvas.drawBitmap(scaledBitmap,pos[0],pos[1],paint);
}
img_05d55bad531ab3e18b483c7f76d31985.gif
音符动画

●图片旋转

这里我引用的一个第三方的圆形图片库
implementation 'de.hdodenhof:circleimageview:2.2.0'
实现图片旋转
circleImageView = findViewById(R.id.mm);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration(4000);
rotateAnimation.setRepeatCount(Animation.INFINITE);
circleImageView.startAnimation(rotateAnimation);
img_2e741cabfee91696969a0e685c0d5a79.gif
图片旋转

最后附上源码https://gitee.com/itfittnesss/DouYinMusic

个人博客https://myml666.github.io

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值