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); } } }
Android 方块移动Loading动画
最新推荐文章于 2021-05-28 02:49:11 发布