【自定义view+属性动画实现】实现思路:继承view,实现有参构造方法,重写onDraw方法绘制用的,在onDraw方法里创建画笔,可在在构造方法里实现初始画笔.
这里有实现思路
1.自定义view
public class Circle extends View {//重写3个构造方法
public Circle(Context context) {
super(context);
}public Circle(Context context, AttributeSet attrs) {
super(context, attrs);
}public Circle(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//开始绘制小圆
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//创建画笔
Paint p=new Paint();
//设置颜色
p.setColor(getResources().getColor(R.color.colorPrimary));
//设置空心线宽
p.setStrokeWidth(20);
//绘制
canvas.drawCircle(canvas.getWidth()/2,canvas.getHeight()/2,50,p);
}
}
2.布局文件添加自定控件
3.实现属性动画和自定义动画的结合效果
public class MainActivity extends AppCompatActivity {@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//自定义的view
Circle c= (Circle) findViewById(R.id.cicle);
//设置属性动画效果
ObjectAnimator transkationY = ObjectAnimator.ofFloat(c,"translationY", 0, 600f);
ObjectAnimator transkationX = ObjectAnimator.ofFloat(c,"translationX", 0, 600f);
//控制动画效果
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(transkationY).with(transkationX);
//持续时间
animatorSet.setDuration(3000);
//开始
animatorSet.start();
//动画的监听
animatorSet.addListener(new Animator.AnimatorListener() {
//开始
@Override
public void onAnimationStart(Animator animator) {}
//结束
@Override
public void onAnimationEnd(Animator animator) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
//退出
@Override
public void onAnimationCancel(Animator animator) {}
//重复
@Override
public void onAnimationRepeat(Animator animator) {}
});
}
}
最后的效果图
文章图片