public class MyView extends View{ private int color; private float radius; private Paint paint; public MyView(Context context) { this(context,null); } public MyView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context,attrs); } private void init(Context context,AttributeSet attrs){ TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView); color = typedArray.getColor(R.styleable.MyView_MyViewColor, Color.BLUE); radius = typedArray.getDimension(R.styleable.MyView_MyViewRadius,30); typedArray.recycle(); paint = new Paint(); paint.setAntiAlias(true); paint.setStrokeWidth(2); paint.setColor(color); paint.setStyle(Paint.Style.FILL); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawCircle(30f,30f,radius,paint); } }public class WelcomeActivity extends AppCompatActivity { private MyView mv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); initView(); startAnim(); } private void initView(){ mv = (MyView)findViewById(R.id.mv); } private void startAnim(){ ObjectAnimator translationX = new ObjectAnimator().ofFloat(mv,"translationX",0,650f); ObjectAnimator translationY = new ObjectAnimator().ofFloat(mv,"translationY",0,1000f); AnimatorSet animatorSet = new AnimatorSet(); animatorSet.playTogether(translationX,translationY); animatorSet.setDuration(3000); animatorSet.start(); Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { Intent intent = new Intent(WelcomeActivity.this,MainActivity.class); startActivity(intent); } },3000); } }<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyView"> <attr name="MyViewColor" format="color"></attr> <attr name="MyViewRadius" format="dimension"></attr> </declare-styleable> </resources>
属性动画(蓝球下移)
最新推荐文章于 2025-03-24 20:39:53 发布