属性动画|属性动画 小球从左上角到右下角运动

【属性动画|属性动画 小球从左上角到右下角运动】activity.xml中写



创建一个类ViewClass继承View

import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class ViewClass extends View { public ViewClass(Context context) { super(context); }public ViewClass(Context context, AttributeSet attrs) { super(context, attrs); }public ViewClass(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }public ViewClass(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); }// 测量: @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } // 绘制: @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 圆 Paint pa1 = new Paint(); pa1.setColor(Color.BLUE); pa1.setAntiAlias(true); canvas.drawCircle(50,50,50,pa1); } // 定位: @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); } // 监听 @Override public boolean onTouchEvent(MotionEvent event) { return super.onTouchEvent(event); } }



在activity.java中写



import android.animation.Animator; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.TypeEvaluator; import android.animation.ValueAnimator; import android.content.Intent; import android.graphics.PointF; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.animation.LinearInterpolator; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { ViewClass MyView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); MyView = (ViewClass) findViewById(R.id.Bull); ObjectAnimator move = ObjectAnimator.ofFloat(MyView, "translationY", 0, 1100f); ObjectAnimator scale2 = ObjectAnimator.ofFloat(MyView, "translationX",0,600f); AnimatorSet animset = new AnimatorSet(); animset.play(move).with(scale2); animset.setDuration(3000); animset.start(); // 动画监听 animset.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animator) {}@Override public void onAnimationEnd(Animator animator) { Intent intent = new Intent(HelloActivity.this, MainActivity.class); startActivity(intent); }@Override public void onAnimationCancel(Animator animator) {}@Override public void onAnimationRepeat(Animator animator) {} }); }}


    推荐阅读