/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.android.apis.animation; // Need the following import to get access to the app resources, since this // class is in a sub-package. import android.graphics.drawable.ColorDrawable; import com.example.android.apis.R; import android.animation.*; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RadialGradient; import android.graphics.Shader; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.OvalShape; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.animation.AccelerateInterpolator; import android.view.animation.DecelerateInterpolator; import android.widget.LinearLayout; import java.util.ArrayList; public class BouncingBalls extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.bouncing_balls); LinearLayout container = (LinearLayout) findViewById(R.id.container); container.addView(new MyAnimationView(this)); } public class MyAnimationView extends View { private static final int RED = 0xffFF8080; private static final int BLUE = 0xff8080FF; private static final int CYAN = 0xff80ffff; private static final int GREEN = 0xff80ff80; public final ArrayList balls = new ArrayList(); AnimatorSet animation = null; public MyAnimationView(Context context) { super(context); //start{ ValueAnimator colorAnim = ObjectAnimator.ofInt(this, "backgroundColor", RED, BLUE);//创建一个动画器;"backgroundColor"是属性的名称;[RED, BLUE]是属性取值区间 colorAnim.setDuration(3000);//设置动画持续时间 colorAnim.seteva luator(new Argbeva luator());//类型评估器,有了和前面指定的属性相对应的类型的评估器,动画器才能理解这个属性; colorAnim.setRepeatCount(ValueAnimator.INFINITE);//设置重复次数 colorAnim.setRepeatMode(ValueAnimator.REVERSE);//设置重复模式 colorAnim.start();//启动动画 //end} //这断代码是绘制属性动画的基本动作//还有一个setInterpolator()函数设置时间插值,默认没有设置的话是线性插值器 } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() != MotionEvent.ACTION_DOWN && event.getAction() != MotionEvent.ACTION_MOVE) { return false; } ShapeHolder newBall = addBall(event.getX(), event.getY()); // Bouncing animation with squash and stretch float startY = newBall.getY(); float endY = getHeight() - 50f; float h = (float)getHeight(); float eventY = event.getY(); int duration = (int)(500 * ((h - eventY)/h)); ValueAnimator bounceAnim = ObjectAnimator.ofFloat(newBall, "y", startY, endY); bounceAnim.setDuration(duration); bounceAnim.setInterpolator(new AccelerateInterpolator()); ValueAnimator squashAnim1 = Object