//主方法
public class SplashActivity extends AppCompatActivity {
private ProgressBarView pbv;
private int progress = 120;
private int time = 3;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//动画运行时间为3秒钟,动画结束后跳转到商品详情页面。
time--;
if (time == 0) {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
} else {
//设置动画播放进程
progress += 120;
pbv.setProgress(progress);
handler.sendEmptyMessageDelayed(0, 1000);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//查找控件
ImageView imageView = (ImageView) findViewById(R.id.logo_img);
pbv = (ProgressBarView) findViewById(R.id.my_progess);
setAnimation(imageView);
handler.sendEmptyMessage(0);
pbv.setProgress(progress);
}
//执行动画的方法
private void setAnimation(ImageView imageView) {
//应用图标从屏幕最上方平移到屏幕中间
ObjectAnimator trans = ObjectAnimator.ofFloat(imageView, "translationY", 0f, 500f).setDuration(1000);
//缩放由2倍到1倍
ObjectAnimator scalX = ObjectAnimator.ofFloat(imageView, "scaleX", 2f, 1f).setDuration(1000);
ObjectAnimator scalY = ObjectAnimator.ofFloat(imageView, "scaleY", 2f, 1f).setDuration(1000);
//渐变从完全透明到完全不透明
ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 0.0f, 1f).setDuration(1000);
// 旋转为旋转一圈
ObjectAnimator rotate = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f).setDuration(1000);
//动画组合开始执行
AnimatorSet setAnimatior = new AnimatorSet();
setAnimatior.play(trans).before(scalX).before(scalY).before(alpha).before(rotate);
setAnimatior.start();
}
}
//自定义VIEW
public class ProgressBarView extends View {
private Paint paint;
private int currentX = 100;
private int currentY = 100;
private int count;
private PointF pointF = new PointF(currentX,currentY);
private int mProgress;
public ProgressBarView(Context context) {
super(context);
initpaint(context);
}
private void initpaint(Context context) {
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
}
public ProgressBarView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initpaint(context);
}
public ProgressBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initpaint(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setStrokeWidth(0);
paint.setColor(Color.BLACK);
canvas.drawCircle(pointF.x,pointF.y,20,paint);
canvas.drawCircle(pointF.x,pointF.y,30,paint);
paint.setStrokeWidth(10);
paint.setColor(Color.RED);
RectF recyF = new RectF(75,75,125,125);
canvas.drawArc(recyF,-90,mProgress,false,paint);
paint.setStrokeWidth(1);
paint.setColor(Color.BLUE);
canvas.drawText(count+"",98,102,paint);
}
public void setProgress(int progress){
this.mProgress = progress;
if (mProgress == 120){
count = 2;
}
if (mProgress == 240){
count = 1;
}
if (mProgress == 360){
count = 0;
}
invalidate();
}
}
//布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/logo_img"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"
android:layout_centerHorizontal="true" />
<com.bawei.chenkai.day14rikao.ProgressBarView
android:visibility="gone"
android:id="@+id/my_progess"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>