public class LodDingView extends View {
private Paint mPaint;
private RectF mRectf;
private int redius = 90;
private int count = 0;
public LodDingView(Context context) {
super(context);
init();
}
public LodDingView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public LodDingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
mPaint = new Paint();
mPaint.setColor(Color.GRAY);
mPaint.setAntiAlias(true);
mRectf = new RectF(redius - 8, 30, redius + 8, 46);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(redius * 2, redius * 2);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 画布旋转的
canvas.rotate(count * 36, redius, redius);
for (int i = 0; i < 10; i++){
mPaint.setAlpha(255 - (10 - i) * 20);
if (i >= 0 && i <= 3) {
mRectf.set(redius - 5, 30, redius + 5, 40);
} else if (i >= 4 && i < 6) {
mRectf.set(redius - 6, 30, redius + 6, 42);
} else if (i >= 6 && i < 8){
mRectf.set(redius - 7, 30, redius + 7, 44);
} else {
mRectf.set(redius - 8, 30, redius + 8, 46);
}
canvas.drawRoundRect(mRectf, 16, 16, mPaint);
canvas.rotate(36, redius, redius);
}
count ++;
if (count > 9) {
count = 0;
}
postInvalidateDelayed(100);
}
public class MainActivity extends AppCompatActivity {
private LodingView mLodingView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLodingView = findViewById(R.id.loding);
ObjectAnimator scaleAnimationX = ObjectAnimator.ofFloat(mLodingView,"scaleX",0f, 1f);
scaleAnimationX.setDuration(3000);
ObjectAnimator scaleAnimationY = ObjectAnimator.ofFloat(mLodingView,"scaleY",0f, 1f);
scaleAnimationY.setDuration(3000);
ObjectAnimator alphaAnimation = ObjectAnimator.ofFloat(mLodingView, "alpha", 0f, 1f);
alphaAnimation.setDuration(3000);
scaleAnimationX.start();
scaleAnimationY.start();
alphaAnimation.start();
}
}
ObjectAnimator.ofFloat括号中的参数:
第一个参数,要实现动画的控件id
第二个参数,要实现的动画属性,以下列出6种:
propertyName | 作用 |
---|---|
alpha | 实现渐变效果 |
rotation | 实现旋转旋转效果 |
translationX | 实现水平移动效果(左或右移动) |
translationY | 实现纵向移动效果(向上或者向下移动) |
scaleX | 实现轴X缩放效果(放大或者缩小) |
scaleY | 实现轴Y缩放效果(放大或者缩小) |
以上两个参数是所有的实现效果都是这样写的,接下来介绍渐变动画的参数
第三个参数,开始时的透明状态( 1f 为不透明,0f 为完全透明,取值 0f ~ 1f )
第四个参数,结束时的透明状态( 1f 为不透明,0f 为完全透明,取值 0f ~ 1f )
还有第五个参数,表示为,渐变后再回到某一状态(1f为不透明,0f为完全透明,取值0f~1f)
ObjectAnimator alphaAnimation = ObjectAnimator.ofFloat(mLodingView, "alpha", 0f, 1f, 0f);