效果
创建一个类
ClaView
//继承View 得到前三个方法
public class ClaView extends View {
Paint paint;
Context context;
//定义宽高
public int width = 0,height = 0;
public ClaView(Context context) {
super(context);
}
public ClaView(Context context,AttributeSet attrs) {
super(context, attrs);
//画笔
paint = new Paint();
//设置画笔颜色
paint.setColor(Color.RED);
}
public ClaView(Context context,AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//绘制图形
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//画布,画一个圆
canvas.drawCircle(width/2,100,100,paint);
}
//测量
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 获取测量视图度
width=getMeasuredWidth();
// 获取测量视图高度
height=getMeasuredHeight();
}
}
XML布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<com.**.ClaView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view"/>
</android.support.constraint.ConstraintLayout>
ManiActivity主页面
public class MainActivity extends AppCompatActivity {
private ClaView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//得到id控件
view = findViewById(R.id.view);
//属性动画
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view,"translationY",0,500);
//设置动画维持时间3秒
objectAnimator.setDuration(3000).start();
//添加监听器
objectAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
//动画执行完 跳转Main2Activity
startActivity(new Intent(MainActivity.this,Main2Activity.class));
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
}