最近家里的宽带业务到期,客服打电话说之前的套餐没了,新套餐普遍涨价,就选了一个+20元绑定一个摄像头的套餐,前两天师傅上门安装了摄像头,我手机上下载一个“小翼管家”就能在手机上实时看家里情况。话说,这种收费服务就是抢劫呀!
回到正题,“小翼管家”App做的一般,不过打开摄像头一刻,loading动画倒是让我挺有兴趣,于是决定在摸鱼的时间动手仿一个......画六边形实在有点麻烦,还要计算角度,没那么多时间,我还要打王者......就用圆角矩形代替了......
package com.example.myapplication;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class LoadingView extends View {
private Paint paint;
private int width;
private int height;
private RectF r1, r2, r3, r4, r5, r6, r7, r8, r9;
private RectF R1, R2, R3, R4, R5, R6, R7, R8, R9;
private int count;
private ValueAnimator animator;
private boolean fading;
public LoadingView(Context context) {
super(context);
init();
}
public LoadingView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.parseColor("#f9cb9c"));
reset();
}
private void reset() {
r1 = new RectF();
r2 = new RectF();
r3 = new RectF();
r4 = new RectF();
r5 = new RectF();
r6 = new RectF();
r7 = new RectF();
r8 = new RectF();
r9 = new RectF();
}
private ValueAnimator.AnimatorUpdateListener listener = new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float fraction = (float) animation.getAnimatedValue();
float temp = fraction;
if (count > 9) {
count = 1;
fading = !fading;
}
if (fading) {
temp = 1 - fraction;
}
switch (count) {
case 1:
r1 = getTemp(temp);
break;
case 2:
r2 = getTemp(temp);
break;
case 3:
r3 = getTemp(temp);
break;
case 4:
r4 = getTemp(temp);
break;
case 5:
r5 = getTemp(temp);
break;
case 6:
r6 = getTemp(temp);
break;
case 7:
r7 = getTemp(temp);
break;
case 8:
r8 = getTemp(temp);
break;
case 9:
r9 = getTemp(temp);
break;
}
invalidate();
if (fraction == 0) {
start();
}
}
};
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
if (hasWindowFocus) {
start();
} else {
stop();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int wmode = MeasureSpec.getMode(widthMeasureSpec);
int wsize = MeasureSpec.getSize(widthMeasureSpec);
int hmode = MeasureSpec.getMode(heightMeasureSpec);
int hsize = MeasureSpec.getSize(heightMeasureSpec);
if (wmode == MeasureSpec.AT_MOST && hmode == MeasureSpec.AT_MOST) {
setMeasuredDimension(64, 64);
} else if (wmode == MeasureSpec.AT_MOST) {
setMeasuredDimension(64, hsize);
} else if (hmode == MeasureSpec.AT_MOST) {
setMeasuredDimension(wsize, 64);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = (w - 4) / 3;
height = (h - 4) / 3;
R1 = new RectF(1, 1, 1 + width, 1 + height);
R2 = new RectF(1 + R1.right, R1.top, 1 + R1.right + width, R1.bottom);
R3 = new RectF(1 + R2.right, R1.top, 1 + R2.right + width, R1.bottom);
R8 = new RectF(1, 1 + R1.bottom, R1.right, 1 + R1.bottom + height);
R9 = new RectF(1 + R8.right, R8.top, 1 + R8.right + width, R8.bottom);
R4 = new RectF(1 + R9.right, R8.top, 1 + R9.right + width, R8.bottom);
R7 = new RectF(1, 1 + R8.bottom, R1.right, 1 + R8.bottom + height);
R6 = new RectF(1 + R7.right, R7.top, 1 + R7.right + width, R7.bottom);
R5 = new RectF(1 + R6.right, R7.top, 1 + R6.right + width, R7.bottom);
}
private void start() {
count++;
animator = ValueAnimator.ofFloat(1, 0);
animator.setDuration(500);
animator.addUpdateListener(listener);
animator.start();
}
private void stop() {
animator.cancel();
animator.removeUpdateListener(listener);
animator = null;
fading = false;
count = 0;
reset();
invalidate();
}
private RectF getTemp(float fraction) {
RectF temp = new RectF();
RectF rectF = getRect();
temp.left = rectF.left + width / 2 * fraction;
temp.top = rectF.top + height / 2 * fraction;
temp.right = rectF.right - width / 2 * fraction;
temp.bottom = rectF.bottom - height / 2 * fraction;
return temp;
}
private RectF getRect() {
switch (count) {
case 1:
return R1;
case 2:
return R2;
case 3:
return R3;
case 4:
return R4;
case 5:
return R5;
case 6:
return R6;
case 7:
return R7;
case 8:
return R8;
case 9:
return R9;
}
return R1;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float r = width / 4;
canvas.drawRoundRect(r1, r, r, paint);
canvas.drawRoundRect(r2, r, r, paint);
canvas.drawRoundRect(r3, r, r, paint);
canvas.drawRoundRect(r4, r, r, paint);
canvas.drawRoundRect(r5, r, r, paint);
canvas.drawRoundRect(r6, r, r, paint);
canvas.drawRoundRect(r7, r, r, paint);
canvas.drawRoundRect(r8, r, r, paint);
canvas.drawRoundRect(r9, r, r, paint);
}
}
|