任何两张图片进度条 cpu扫描效果

package com.example.liuan.scananim;

import android.graphics.drawable.ClipDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    private ImageView ivAccCpu1;
    private ImageView ivAccCpu2;

    boolean isOpen;
    private ClipDrawable drawableIvAccCpu2;
    private ClipDrawable drawableIvAccCpu1;
    int animDuration = 5000;
    private final int MSG_STARTANIM_OPEN = 1024;
    private final int MSG_STARTANIM_CLOSE = 1022;
    int progress = 0;
    private Handler handler = new Handler() {


        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_STARTANIM_OPEN:
                    if (progress > 100) {
                        removeMessages(MSG_STARTANIM_OPEN);
                    }
                    progress += 1;
                    showImagePergress(progress, true);
                    sendEmptyMessageDelayed(MSG_STARTANIM_OPEN, animDuration / 100);
                    break;
                case MSG_STARTANIM_CLOSE:
                    if (progress > 100) {
                        removeMessages(MSG_STARTANIM_CLOSE);
                    }
                    progress += 1;
                    showImagePergress(progress, false);
                    sendEmptyMessageDelayed(MSG_STARTANIM_CLOSE, animDuration / 100);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ivAccCpu1 = findViewById(R.id.iv_acc_cpu1);
        ivAccCpu2 = findViewById(R.id.iv_acc_cpu2);
        //获取图片所显示的ClipDrawble对象
        //从10000 到 1  到这 着显示
        drawableIvAccCpu2 = (ClipDrawable) ivAccCpu2.getDrawable();
        //从1 到10000   正着显示
        drawableIvAccCpu1 = (ClipDrawable) ivAccCpu1.getDrawable();
        showImageFirstPic(true);
        RelativeLayout relativeLayout = findViewById(R.id.rl_acp_acc);
        relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.removeCallbacksAndMessages(null);
                if (isOpen) {
                    close();
                } else {
                    openAnim();
                }


                isOpen = !isOpen;
            }
        });

    }

    //开的时候从上到下
    private void openAnim() {
        progress = 0;
        handler.sendEmptyMessage(MSG_STARTANIM_OPEN);
    }

    //关的时候从下到上
    private void close() {
        progress = 0;
        handler.sendEmptyMessage(MSG_STARTANIM_CLOSE);
    }


    /**
     * 从0-100
     */
    private void showImagePergress(int progress, boolean isFormTop) {
        //Log.e(TAG, "showImagePergress: progress" + progress);
        int num = (int) (10000 * progress / 100);
        drawableIvAccCpu2.setLevel(isFormTop ? num : 10000 - num);
        drawableIvAccCpu1.setLevel(isFormTop ? 10000 - num : num);
    }


    private void showImageFirstPic(boolean isShowFirst) {
        drawableIvAccCpu2.setLevel(isShowFirst ? 0 : 10000);
        drawableIvAccCpu1.setLevel(isShowFirst ? 10000 : 0);
    }


}
 

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.liuan.scananim.MainActivity">

    <RelativeLayout

        android:id="@+id/rl_acp_acc"
        android:layout_width="match_parent"
        android:layout_height="match_parent">



        <ImageView
            android:id="@+id/iv_acc_cpu1"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="162dp"
            android:src="@drawable/clip_cpu_scan1" />

        <ImageView
            android:id="@+id/iv_acc_cpu2"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="162dp"
            android:src="@drawable/clip_cpu_scan2" />

    </RelativeLayout>

</android.support.constraint.ConstraintLayout>
clip_cpu_scan2.xml
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="vertical"
    android:drawable="@mipmap/hou_open"
    android:gravity="top"></clip>
clip_cpu_scan1.xml
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="vertical"
    android:drawable="@mipmap/hou_close"
    android:gravity="bottom"></clip>
下载地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值