首页 / 操作系统 / Linux / Android 4应用动画案列一
这段时间一直都在看Android源码,当然在这里我就不说关于源码的问题,自我对其ANDROID4.0的观后感就是~比之前的2.2与2.3的都改进了好多,可能是我学疏才潜,在里面还有太多需要我去用时间来征服的节点,所以在这里就不敢自笔为是,所以就把简单的直接把API里的一些例子给搬出来,看看效果吧,一个一个的来,也顺便把里面的源码给直接贴出来,2011年虽然对我们来说是一个不可多得的一年,但是同样2012年更是让我们期待着跳越前栏的感觉,让我们共同展望2012年吧:这个例子很简单,先看我截的几张图吧: 以上就是简单的动画截图,废话不多是了,看下源吗就知道,这里的源代码相对来说都很简单,我就不对齐解释了:java代码:package com.jsd.android4; import com.jsd.android4.bouncing.BouncingBalls; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class Android4_DemosActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ Button mToBuncingBalls = (Button) findViewById(R.id.bouncingBalls); mToBuncingBalls.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent mIt = new Intent(); mIt.setClass(Android4_DemosActivity.this,BouncingBalls.class); startActivity(mIt); } }); } } package com.jsd.android4.bouncing; import java.util.ArrayList; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.AnimatorSet; import android.animation.ArgbEvaluator; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; 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 com.jsd.android4.R; /** * * @author jankey * */ public class BouncingBalls extends Activity { @Override protected 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 BLUE = 0xff8080FF; private static final int RED = 0xffFF8080; private static final int CYAN = 0xff80ffff; private static final int GREEN = 0xff80ff80; private static final int DURATION = 3000; private static final float WIDTH = 50f; private static final float HEIGHT = 50f; public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>(); AnimatorSet animation = null; public MyAnimationView(Context context) { super(context); ValueAnimator colorAnim = ObjectAnimator.ofInt(this, "backgroundColor", BLUE, RED); colorAnim.setDuration(DURATION); colorAnim.setEvaluator(new ArgbEvaluator()); colorAnim.setRepeatCount(ValueAnimator.INFINITE); colorAnim.setRepeatMode(ValueAnimator.REVERSE); colorAnim.start(); } @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()); 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 = ObjectAnimator.ofFloat(newBall, "x", newBall.getX(), newBall.getX() - 25f); squashAnim1.setDuration(duration / 4); squashAnim1.setRepeatCount(1); squashAnim1.setRepeatMode(ValueAnimator.REVERSE); squashAnim1.setInterpolator(new DecelerateInterpolator()); ValueAnimator squashAnim2 = ObjectAnimator.ofFloat(newBall, "width", newBall.getWidth() + 50); squashAnim2.setDuration(duration / 4); squashAnim2.setRepeatCount(1); squashAnim2.setRepeatMode(ValueAnimator.REVERSE); squashAnim2.setInterpolator(new DecelerateInterpolator()); ValueAnimator stretchAnim1 = ObjectAnimator.ofFloat(newBall, "y", endY, endY + 25f); stretchAnim1.setDuration(duration / 4); stretchAnim1.setRepeatCount(1); stretchAnim1.setInterpolator(new DecelerateInterpolator()); stretchAnim1.setRepeatMode(ValueAnimator.REVERSE); ValueAnimator stretchAnim2 = ObjectAnimator.ofFloat(newBall, "height", newBall.getHeight(), newBall.getHeight() - 25); stretchAnim2.setDuration(duration/4); stretchAnim2.setRepeatCount(1); stretchAnim2.setInterpolator(new DecelerateInterpolator()); stretchAnim2.setRepeatMode(ValueAnimator.REVERSE); ValueAnimator bounceBackAnim = ObjectAnimator.ofFloat(newBall,"y",endY,startY); bounceBackAnim.setDuration(duration); bounceBackAnim.setInterpolator(new DecelerateInterpolator()); AnimatorSet bouncer = new AnimatorSet(); bouncer.play(bounceAnim).before(squashAnim1); bouncer.play(squashAnim1).with(squashAnim2); bouncer.play(squashAnim1).with(stretchAnim1); bouncer.play(squashAnim1).with(stretchAnim2); bouncer.play(bounceBackAnim).after(stretchAnim2); ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall,"alpha",1f,0f); fadeAnim.setDuration(250); fadeAnim.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); balls.remove(((ObjectAnimator)animation).getTarget()); } }); AnimatorSet animatorSet = new AnimatorSet(); animatorSet.play(bouncer).before(fadeAnim); animatorSet.start(); return true; } private ShapeHolder addBall(float x, float y) { OvalShape circle = new OvalShape(); circle.resize(WIDTH, HEIGHT); ShapeDrawable drawable = new ShapeDrawable(circle); ShapeHolder shapeHolder = new ShapeHolder(drawable); shapeHolder.setX(x - 25f); shapeHolder.setY(y - 25f); int red = (int) (Math.random() * 255); int green = (int) (Math.random() * 255); int blue = (int) (Math.random() * 255); int color = 0xff000000 | red << 16 | green << 8 | blue; Paint paint = drawable.getPaint(); int darkColor = 0xff000000 | red / 4 << 16 | green / 4 << 8 | blue / 4; RadialGradient gradient = new RadialGradient(37.5f, 12.5f, 50f, color, darkColor, Shader.TileMode.CLAMP); paint.setShader(gradient); shapeHolder.setPaint(paint); balls.add(shapeHolder); return shapeHolder; } @Override protected void onDraw(Canvas canvas) { for(int i = 0;i < balls.size();++i){ ShapeHolder shapeHolder = balls.get(i); canvas.save(); canvas.translate(shapeHolder.getX(),shapeHolder.getY()); shapeHolder.getShape().draw(canvas); canvas.restore(); } } } } 以上是主要的代码,基本复制就可以用了,RES的文件很简单,自己稍微看下就可以了,好了,一次一个案列吧
收藏该网址