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=".DonghuaActivity">
<com.example.z2.view.Vieeew
android:id="@+id/circle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
package com.example.z2.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;
public class Vieeew extends View {
private Paint paint;
private int width;
private int hight;
public Vieeew(Context context) {
super(context);
}
public Vieeew(Context context,AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setColor(Color.RED);
}
public Vieeew(Context context,AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(width/2,hight/2,50,paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getMeasuredWidth();
hight = getMeasuredHeight();
}
}
属性动画
package com.example.z2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import com.example.z2.view.Vieeew;
public class DonghuaActivity extends AppCompatActivity {
private Vieeew circle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donghua);
// handler.sendEmptyMessageDelayed(0,1000);
circle = (Vieeew) findViewById(R.id.circle);
ScaleAnimation scaleAni = new ScaleAnimation(0.2f, 3.0f, 0.2f, 3.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
//设置动画执行的时间,单位是毫秒
scaleAni.setDuration(100);
// 0表示从0度开始,360表示旋转360度
// Animation.RELATIVE_TO_SELF, 0.5f表示绕着自己的中心点进行动画
RotateAnimation rotateAni = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
//淡出
AlphaAnimation alphaAni = new AlphaAnimation(0.2f, 1);
//设置动画执行的时间,单位是毫秒
alphaAni.setDuration(3000);
alphaAni.setRepeatCount(0);
//平移
TranslateAnimation translateAni = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT,
0, Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 1);
//设置动画执行的时间,单位是毫秒
translateAni.setDuration(3000);
// 设置动画重复次数
// -1或者Animation.INFINITE表示无限重复,正数表示重复次数,0表示不重复只播放一次
translateAni.setRepeatCount(0);
// 设置动画完成的时间(速度),单位是毫秒,1000是1秒内完成动画
rotateAni.setDuration(3000);
// 将淡出动画和平移和缩放动画和旋转动画放到动画插值器
AnimationSet as = new AnimationSet(false);
as.addAnimation(scaleAni);
as.addAnimation(rotateAni);
as.addAnimation(translateAni);
as.addAnimation(alphaAni);
Animation.AnimationListener listener = new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent( DonghuaActivity.this,MainActivity.class));
}
@Override
public void onAnimationRepeat(Animation animation) {
}
};
translateAni.setAnimationListener(listener);
// 启动动画
circle.startAnimation(as);
}
}