欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

动画模拟一个手机充电

程序员文章站 2022-05-02 22:14:29
...

MainActivity

package com.zjh.administrat.zhaojunhui1213;

import android.animation.Animator;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<CustomLinView> list;
    private ValueAnimator animator;
    private int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CustomLinView image1 = findViewById(R.id.image1);
        CustomLinView image2 = findViewById(R.id.image2);
        CustomLinView image3 = findViewById(R.id.image3);
        CustomLinView image4 = findViewById(R.id.image4);
        CustomLinView image5 = findViewById(R.id.image5);
        CustomLinView image6 = findViewById(R.id.image6);
        //把图片放入集合中
        list = new ArrayList<>();
        list.add(image1);
        list.add(image2);
        list.add(image3);
        list.add(image4);
        list.add(image5);
        list.add(image6);
        getdata(i);
    }

    private void getdata(final int i) {
        //属性动画 渐变色 从绿到黄
        animator = ValueAnimator.ofInt(Color.parseColor("#ffcc66"), Color.parseColor("#ffcc33"));
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int color = (int) animation.getAnimatedValue();
                //这里是个view,设置背景色
                list.get(i).setBackgroundColor(color);
            }
        });
        //持续时间
        animator.setDuration(1500);
        animator.setEvaluator(new ArgbEvaluator());
        animator.start();
        //当下标为最后一个时停止运行
        if (i == 5) {
            return;
        }
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                //当第一个图片完成动画时开启下一个动画
                getdata(i + 1);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
    }
}


CustomLinView自定义view

package com.zjh.administrat.zhaojunhui1213;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

@SuppressLint("AppCompatCustomView")
public class CustomLinView extends ImageView {


    public CustomLinView(Context context) {
        super(context);
    }

    public CustomLinView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <com.zjh.administrat.zhaojunhui1213.CustomLinView
        android:id="@+id/image7"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:background="#999999" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#999999"
        android:orientation="vertical"
        android:padding="5dp">

        <com.zjh.administrat.zhaojunhui1213.CustomLinView
            android:id="@+id/image6"
            android:layout_width="200dp"
            android:layout_height="30dp"
            android:layout_margin="5dp"
            android:background="#66cc66" />

        <com.zjh.administrat.zhaojunhui1213.CustomLinView
            android:id="@+id/image5"
            android:layout_width="200dp"
            android:layout_height="30dp"
            android:layout_margin="5dp"
            android:background="#66cc66" />

        <com.zjh.administrat.zhaojunhui1213.CustomLinView
            android:id="@+id/image4"
            android:layout_width="200dp"
            android:layout_height="30dp"
            android:layout_margin="5dp"
            android:background="#66cc66" />

        <com.zjh.administrat.zhaojunhui1213.CustomLinView
            android:id="@+id/image3"
            android:layout_width="200dp"
            android:layout_height="30dp"
            android:layout_margin="5dp"
            android:background="#66cc66" />

        <com.zjh.administrat.zhaojunhui1213.CustomLinView
            android:id="@+id/image2"
            android:layout_width="200dp"
            android:layout_height="30dp"
            android:layout_margin="5dp"
            android:background="#66cc66" />

        <com.zjh.administrat.zhaojunhui1213.CustomLinView
            android:id="@+id/image1"
            android:layout_width="200dp"
            android:layout_height="30dp"
            android:layout_margin="5dp"
            android:background="#66cc66" />
    </LinearLayout>

</LinearLayout>

相关标签: 属性动画