Android 方块移动Loading动画

Android 方块移动Loading动画


import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by huangqiyu on 2016/12/28.
 */
public class CommonLoadingDialog extends RelativeLayout {

    private final int TIME = 300;
    private boolean isRunning = true;
    private Handler handler;
    private TextView mTextViewContent;

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

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

    public CommonLoadingDialog(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setMinimumWidth(110);
        this.setMinimumHeight(110);
        init(context);
    }

    private void init(Context context) {

        RelativeLayout loadingLayout = new RelativeLayout(context);
        loadingLayout.setMinimumWidth(110);
        loadingLayout.setMinimumHeight(110);

        MyRect rectRed = new MyRect(context);
        LayoutParams params1 = new LayoutParams(50, 50);
        rectRed.setLayoutParams(params1);
        loadingLayout.addView(rectRed);

        MyRect rectBlue = new MyRect(context);
        rectBlue.setColor(Color.BLUE);
        LayoutParams params2 = new LayoutParams(50, 50);
        rectBlue.setLayoutParams(params2);
        rectBlue.setX(60f);
        rectBlue.setY(0);
        loadingLayout.addView(rectBlue);

        MyRect rectYellow = new MyRect(context);
        rectYellow.setColor(Color.YELLOW);
        LayoutParams params3 = new LayoutParams(50, 50);
        rectYellow.setLayoutParams(params3);
        rectYellow.setX(0);
        rectYellow.setY(60f);
        loadingLayout.addView(rectYellow);

        RelativeLayout.LayoutParams rParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        this.addView(loadingLayout, rParams);

        initText(context);

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                MyRect view = (MyRect) msg.obj;
                switch (msg.what) {
                    case 1:
                        ObjectAnimator.ofFloat(view, "translationX", 0, 60f).setDuration(TIME).start();
                        break;
                    case 2:
                        ObjectAnimator.ofFloat(view, "translationY", 60, 0f).setDuration(TIME).start();
                        break;
                    case 3:
                        ObjectAnimator.ofFloat(view, "translationX", 60, 0f).setDuration(TIME).start();
                        break;
                    case 4:
                        ObjectAnimator.ofFloat(view, "translationY", 0, 60f).setDuration(TIME).start();
                        break;
                }
                return true;
            }
        });

        startLoadingAnim(rectYellow, 1, 0);
        startLoadingAnim(rectRed, 4, 300);
        startLoadingAnim(rectBlue, 3, 600);
    }

    private void initText(Context context) {
        mTextViewContent = new TextView(context);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 150, 0, 0);
        mTextViewContent.setText("加载中...");
        mTextViewContent.setLayoutParams(params);
        this.addView(mTextViewContent);

    }

    public void setText(String text) {
        mTextViewContent.setText(text);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        isRunning = false;
    }

    /**
     * 运动位置:
     * 4 <- 3
     * 1 -> 2
     *
     * @param view      目标view
     * @param start     开始位置
     * @param delayTime 延迟时间
     */
    private void startLoadingAnim(final View view, final int start, final int delayTime) {

        new Thread() {
            @Override
            public void run() {

                try {
                    if (delayTime > 0)
                        Thread.sleep(delayTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                int position = start;
                while (isRunning) {
                    Message.obtain(handler, position, view).sendToTarget();
                    position++;
                    if (position > 4) {
                        position = 1;
                    }

                    try {
                        Thread.sleep(900);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

    }

    class MyRect extends View {

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        public MyRect(Context context) {
            super(context);
            paint.setColor(Color.RED);
        }

        public void setColor(int color) {
            paint.setColor(color);
            invalidate();
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawRect(0, 0, 50, 50, paint);
        }
    }

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值