Android——自定义View(五)

1.概述

  最近看到网易云音乐的听歌识曲的页面,这次仿网易云音乐听歌识曲效果。

2.效果

1.水波纹效果

水波纹效果.gif

3.实现思路

  1.我们通过自定义一个容器,以及自定义水波纹的圆。
  2.自定义的容器添加几个自定义属性,水波纹颜色,半径,边宽等。
  3.在我们自定义容器中获取自定义属性,并创建添加水波纹的圆。我这里设置为4个。
  4.处理水波纹的圆是有X/Y轴的缩放动画。以及透明度的动画。

4.代码实现

4.1自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--
     ripple_anim_color : 水波纹的颜色
     ripple_anim_type  :水波纹的类型,填充,描边
     radius            :半径
     strokWidth        :描边宽度 -->
    <declare-styleable name="RippleAnimationView">
        <attr name="ripple_anim_color" format="color"/>
        <attr name="ripple_anim_type" format="enum">
            <enum name="fillRipple" value="0"/>
            <enum name="strokeRipple" value="1"/>
        </attr>
        <attr name="radius" format="integer"/>
        <attr name="strokWidth" format="integer"/>
    </declare-styleable>
</resources>

4.2自定义容器

/**
 * TODO:自定义容器,动态添加水波纹的圆。并通过动画实现
 *   实现步骤
 *      1.通过自定义属性来设置背景,以及水波纹颜色等
 *      2.动态添加水波纹个数
 *      3.通过动画实现水波纹的XY轴缩放,以及透明度的变化
 */
public class RippleAnimationView extends RelativeLayout {
    //画笔
    public Paint paint;
    //水波纹半径
    private int radius;
    //水波纹颜色
    private int rippleColor;
    //水波纹描边宽度
    private int strokWidth;
    //水波纹的类型,0-实心充满 1-空心描边
    private int rippleType;
    //动画集合
    private AnimatorSet animatorSet;
    //水波纹圆的集合
    private ArrayList<RippleCircleView> viewList = new ArrayList<>();
    //动画执行的标志位
    private boolean animationRunning = false;

    public RippleAnimationView(Context context) {
        this(context,null);
    }

    public RippleAnimationView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RippleAnimationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        //解析自定义属性
        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.RippleAnimationView);
        radius = array.getInteger(R.styleable.RippleAnimationView_radius, 54);
        rippleColor = array.getColor(R.styleable.RippleAnimationView_ripple_anim_color, ContextCompat.getColor(context, R.color.rippleColor));
        strokWidth = array.getInteger(R.styleable.RippleAnimationView_strokWidth,2);
        rippleType = array.getInt(R.styleable.RippleAnimationView_ripple_anim_type, 0);

        //初始化画笔
        paint = new Paint();
        paint.setAntiAlias(true);            //设置抗锯齿
        paint.setStrokeWidth(UIUtils.getInstance().getWidth(strokWidth));    //设置描边宽度
        if (rippleType == 0){
            paint.setStyle(Paint.Style.FILL);//设置样式
        }else {
            paint.setStyle(Paint.Style.STROKE);
        }
        paint.setColor(rippleColor);         //设置颜色

        //动态添加水波纹,设置它的大小
        LayoutParams rippleParams = new LayoutParams(UIUtils.getInstance().getWidth(radius + strokWidth),UIUtils.getInstance().getWidth(radius + strokWidth));
        rippleParams.addRule(CENTER_IN_PARENT, TRUE);
        //设置最大缩放系数
        float maxScale = 10;//UIUtils.getInstance().displayMetricsWidth / (float) ( (UIUtils.getInstance().getWidth(radius + strokWidth)));
        //动画执行时间
        int rippleDuration = 3500;
        //间隔时间 (上一个波纹  和下一个波纹的)
        int singleDelay = rippleDuration / 4;
        //动画的集合,使用AnimatorSet
        ArrayList<Animator> animatorList = new ArrayList<>();
        //实例化一个波纹=view
        for (int i = 0; i < 4; i++) {
            //创建一个水波纹的View,添加到当前容器
            RippleCircleView rippleCircleView = new RippleCircleView(this);
            viewList.add(rippleCircleView);
            //设置属性动画X轴,Y轴缩放动画,以及透明度动画
            ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(rippleCircleView, View.SCALE_X,maxScale,0);
            scaleXAnimator.setRepeatCount(ObjectAnimator.INFINITE);  //无线重复
            scaleXAnimator.setRepeatMode(ObjectAnimator.RESTART);
            scaleXAnimator.setStartDelay(i * singleDelay);
            scaleXAnimator.setDuration(rippleDuration);
            animatorList.add(scaleXAnimator);

            //Y轴缩放
            ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(rippleCircleView, View.SCALE_Y,maxScale,0);
            scaleYAnimator.setRepeatCount(ObjectAnimator.INFINITE);
            scaleYAnimator.setRepeatMode(ObjectAnimator.RESTART);
            scaleYAnimator.setStartDelay(i * singleDelay);
            scaleYAnimator.setDuration(rippleDuration);
            animatorList.add(scaleYAnimator);

            //透明度
            ObjectAnimator alphaYAnimator = ObjectAnimator.ofFloat(rippleCircleView, View.ALPHA,0f,1f);
            alphaYAnimator.setRepeatCount(ObjectAnimator.INFINITE);
            alphaYAnimator.setRepeatMode(ObjectAnimator.RESTART);
            alphaYAnimator.setStartDelay(i * singleDelay);
            alphaYAnimator.setDuration(rippleDuration);
            animatorList.add(alphaYAnimator);
            addView(rippleCircleView,rippleParams);
        }
        animatorSet = new AnimatorSet();
        animatorSet.setInterpolator(new AccelerateDecelerateInterpolator()); //先加速后减速
        //同时执行动画
        animatorSet.playTogether(animatorList);
        array.recycle();
    }

    public int getStrokWidth() {
        return strokWidth;
    }

    /**
     * TODO:开始动画
     */
    public void startRippleAnimation(){
        if (!animationRunning){ //如果没有执行动画的时候需要隐藏水波纹
            for (RippleCircleView rippleView : viewList) {
                rippleView.setVisibility(VISIBLE);
            }
            animatorSet.start();
            animationRunning = true;
        }
    }

    /**
     * TODO:开始动画
     */
    public void stopRippleAnimation(){
        if (animationRunning){
            Collections.reverse(viewList); //将它反序
            for (RippleCircleView rippleView : viewList) {
                rippleView.setVisibility(INVISIBLE);
            }
            animatorSet.end();
            animationRunning = false;
        }
    }

    public boolean isAnimationRunning() {
        return animationRunning;
    }

    public void setAnimationRunning(boolean animationRunning) {
        this.animationRunning = animationRunning;
    }
}

4.3自定义水波纹的圆

/**
 * TODO:自定义水波纹的圆
 */
public class RippleCircleView extends View {
    //持有父容器的RippleAnimationView的引用
    private RippleAnimationView rippleAnimationView;

    public RippleCircleView(RippleAnimationView rippleAnimationView) {
        this(rippleAnimationView.getContext(),null);
        this.rippleAnimationView = rippleAnimationView;
        this.setVisibility(View.INVISIBLE); //默认隐藏
    }

    public RippleCircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RippleCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 画圆
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        int radius = (Math.min(getWidth(), getHeight())) / 2;
        canvas.drawCircle(radius,radius,radius - rippleAnimationView.getStrokWidth(),rippleAnimationView.paint);
    }
}

5.使用

5.1.布局文件

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#000000"
        tools:context="com.ych.ripple.RippleActivity">

        <com.ych.ripple.view.RippleAnimationView
            android:id="@+id/layout_RippleAnimation"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:radius="150"
            app:ripple_anim_type="fillRipple"
            app:strokWidth="18"
            app:ripple_anim_color="#FF0000">

            <ImageView
                android:id="@+id/ImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:src="@drawable/music" />
        </com.ych.ripple.view.RippleAnimationView>
    </LinearLayout>
</layout>

5.2.Activity中使用

/**
 * TODO:仿网易云音乐听歌识曲水波纹效果
 */
public class RippleActivity extends AppCompatActivity {

    private ActivityRippleBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this,R.layout.activity_ripple);
        //这个是用于做屏幕适配的,设置中间图片的大小
        ViewCalculateUtil.setViewLayoutParam(binding.ImageView, 300,300,0,0,0,0);
        //设置点击事件
        binding.ImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果动画正在执行,那么就暂停,否则开启
                if (binding.layoutRippleAnimation.isAnimationRunning()){
                    binding.layoutRippleAnimation.stopRippleAnimation();
                }else {
                    binding.layoutRippleAnimation.startRippleAnimation();
                }
            }
        });
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,907评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,987评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,298评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,586评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,633评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,488评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,275评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,176评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,619评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,819评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,932评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,655评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,265评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,871评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,994评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,095评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,884评论 2 354

推荐阅读更多精彩内容