首先创建一个View类,并继承View
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
/**
* <p>文件描述:<p>
* <p>作者:JYB<p>
* <p>创建时间:2019/2/22<p>
* <p>更改时间:2019/2/22<p>
* <p>版本号:1<p>
*/
public class MyView extends View {
private Paint paint;
private int width;
private int height;
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
paint=new Paint();
//设置颜色
paint.setColor(Color.RED);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//绘制圆形
canvas.drawCircle(width/2,height/2,100,paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//设置宽
width=getMeasuredWidth();
//设置高
height=getMeasuredHeight();
}
}
MainActivity页面的动画效果
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private MyView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
getSupportActionBar().hide();
//取得的宽高维度存放于DisplayMetrics 对象中
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int height = displayMetrics.heightPixels;
//缩放
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(lv,"scaleY",0,3,1);
//透明度
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(lv,"alpha",0,1);
//平移
ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(lv,"translationY",0,360);
//旋转度数
ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(lv,"rotation",0,height/2-lv.getHeight()/2);
//设置动画
AnimatorSet set = new AnimatorSet();
//组合多个Animator
set.play(objectAnimator1).with(objectAnimator2).with(objectAnimator3).with(objectAnimator4);
//设置动画的持续时间
set.setDuration(3000);
//开始动画
set.start();
//监听器中添加相关的操作
set.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
//跳转页面
startActivity(new Intent(MainActivity.this,ShowActivity.class));
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
private void initView() {
lv = findViewById(R.id.lv);
}
}
动画页面的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<jiaoyibo.bawei.com.fenleizhanshi.MyView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>