1. 程式人生 > >動畫模擬一個手機充電

動畫模擬一個手機充電

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>