android4动画应用二

之前本想把ANDROID4的动画应用都写下来的,后来觉得自己想讲一套视频,以共编程爱好者更好的学习,但是由于时间问题一直没能如愿,前段时间录了些视频,自己感觉录制下来的效果并不理想,个人感觉自己的表达能力急需有待提高,在录制好后,自己听了遍,效果真是,那个差啊,不过没关系,我有信心把它录制好,并觉得录制一套免费的ANDROOID4完全开发视频,从底层分析到APP上线,说实话,之前一直给公司做,自己闲时抽了些时间给自己写了几个,比如说针对很多人面试难的面试宝典,还有人生宝典,个性展示等APP,处于自己设计的问题,总感觉不如人意,所以一直没上MARKET市场,哎,可能我真的还辨别不了商机与偏好的区别,好了,不多说了,这段时间自己也在针对设计这块进行严谨的充电中,不知道等我把这写都搞明白了,IT行业又变成什么样了,别的就不说了,先把之前的动画二与三一起贴出来吧,只贴核心代码,我会把演示的视频上传的YOUKU里:http://u.youku.com/user_show/uid_jiangshide1,还有之前的动画一的演示视频地址:http://v.youku.com/v_show/id_XMzQ1MDg4MTY0.html,各位偏好者可以去观看下,在后面我会陆续推出一些视频,比如针对初学者与中级之类的视频方便其学习,不管我做得好与不好,希望都能得到各位网友的支持,毕竟我所作的都是免费的,可能在后期我会推出自己门户网站,已经在设计当中,这可能需要些时间,希望到时候能得到大家的支持,好了,直接贴源码吧:

这是CLONING的核心代码:

package com.jsd.android4.cloning;


import java.util.ArrayList;


import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
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.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;


import com.jsd.android4.R;
import com.jsd.android4.bouncing.ShapeHolder;


/**
 * 
 * @author jankey
 *
 */
public class AnimCloningActivity extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.anim_cloning);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
final MyAnimationView animView = new MyAnimationView(this);
container.addView(animView);
Button starter = (Button) findViewById(R.id.startButton);
starter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animView.startAnimation();
}
});
}

public class MyAnimationView extends View implements AnimatorUpdateListener{


public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
AnimatorSet animation = null;
private float mDensity;

public MyAnimationView(Context context) {
super(context);
mDensity = getContext().getResources().getDisplayMetrics().density;

ShapeHolder ball0 = addBall(50f,25f);
ShapeHolder ball1 = addBall(150f,25f);
ShapeHolder ball2 = addBall(250f,25f);
ShapeHolder ball3 = addBall(350f,25f);
ShapeHolder ball4 = addBall(450f,25f);
}


private void createAnimation(){
if(animation == null){
ObjectAnimator animl = ObjectAnimator.ofFloat(balls.get(0),"y",0f,getHeight() - balls.get(0).getHeight()).setDuration(500);
ObjectAnimator anim2 = animl.clone();
anim2.setTarget(balls.get(1));
animl.addUpdateListener(this);

ShapeHolder ball2 = balls.get(2);
ObjectAnimator animDown = ObjectAnimator.ofFloat(ball2, "y",0f,getHeight() - ball2.getHeight()).setDuration(500);
animDown.setInterpolator(new AccelerateInterpolator());
ObjectAnimator animUp = ObjectAnimator.ofFloat(ball2, "y",0f,getHeight()-ball2.getHeight(),0f).setDuration(500);
animUp.setInterpolator(new DecelerateInterpolator());
AnimatorSet s1 = new AnimatorSet();
s1.playSequentially(animDown,animUp);
animDown.addUpdateListener(this);
animUp.addUpdateListener(this);
AnimatorSet s2 = (AnimatorSet)s1.clone();
s2.setTarget(balls.get(3));

animation = new AnimatorSet();
animation.playTogether(animl,anim2,s1);
animation.playSequentially(s1,s2);
}
}

private ShapeHolder addBall(float x,float y){
OvalShape circle = new OvalShape();
circle.resize(50f*mDensity,50f*mDensity);
ShapeDrawable drawable = new ShapeDrawable(circle);
ShapeHolder shapeHolder = new ShapeHolder(drawable);
shapeHolder.setX(x - 25f);
shapeHolder.setY(y - 25f);
int red  = (int)(100 + Math.random() * 155);
int green = (int)(10 + Math.random() * 155);
int blue = (int)(100 + Math.random() * 155);
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();
}
}

public void startAnimation(){
createAnimation();
animation.start();
}

@Override
public void onAnimationUpdate(ValueAnimator animation) {
invalidate();
}
}
}


这是EVALUTOR的核心代码:

package com.jsd.android4.evaluator;


import java.util.ArrayList;


import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
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.View;
import android.widget.Button;
import android.widget.LinearLayout;


import com.jsd.android4.R;
import com.jsd.android4.bouncing.ShapeHolder;


/**
 * 
 * @author jankey
 *
 */
public class CustomEvaluatorActivity extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.evaluator);
initialized();
}

private void initialized(){
LinearLayout container = (LinearLayout) findViewById(R.id.container);
final MyAnimationView animView = new MyAnimationView(this);
container.addView(animView);
Button starter = (Button) findViewById(R.id.startTo);
starter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animView.startAnimation();
}
});
}

public class XYHolder {
       private float mX;
       private float mY;


       public XYHolder(float x, float y) {
           mX = x;
           mY = y;
       }


       public float getX() {
           return mX;
       }


       public void setX(float x) {
           mX = x;
       }


       public float getY() {
           return mY;
       }


       public void setY(float y) {
           mY = y;
       }
   }


   public class XYEvaluator implements TypeEvaluator {
       public Object evaluate(float fraction, Object startValue, Object endValue) {
           XYHolder startXY = (XYHolder) startValue;
           XYHolder endXY = (XYHolder) endValue;
           return new XYHolder(startXY.getX() + fraction * (endXY.getX() - startXY.getX()),
                   startXY.getY() + fraction * (endXY.getY() - startXY.getY()));
       }
   }


   public class BallXYHolder {


       private ShapeHolder mBall;


       public BallXYHolder(ShapeHolder ball) {
           mBall = ball;
       }


       public void setXY(XYHolder xyHolder) {
           mBall.setX(xyHolder.getX());
           mBall.setY(xyHolder.getY());
       }


       public XYHolder getXY() {
           return new XYHolder(mBall.getX(), mBall.getY());
       }
   }


   public class MyAnimationView extends View implements ValueAnimator.AnimatorUpdateListener {


       public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
       ValueAnimator bounceAnim = null;
       ShapeHolder ball = null;
       BallXYHolder ballHolder = null;


       public MyAnimationView(Context context) {
           super(context);
           ball = createBall(25, 25);
           ballHolder = new BallXYHolder(ball);
       }


       private void createAnimation() {
           if (bounceAnim == null) {
               XYHolder startXY = new XYHolder(0f, 0f);
               XYHolder endXY = new XYHolder(300f, 800f);
               bounceAnim = ObjectAnimator.ofObject(ballHolder, "xY",
                       new XYEvaluator(), endXY);
               bounceAnim.setDuration(2000);
               bounceAnim.addUpdateListener(this);
           }
       }


       public void startAnimation() {
           createAnimation();
           bounceAnim.start();
       }


       private ShapeHolder createBall(float x, float y) {
           OvalShape circle = new OvalShape();
           circle.resize(50f, 50f);
           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(); //new Paint(Paint.ANTI_ALIAS_FLAG);
           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);
           return shapeHolder;
       }


       @Override
       protected void onDraw(Canvas canvas) {
           canvas.save();
           canvas.translate(ball.getX(), ball.getY());
           ball.getShape().draw(canvas);
           canvas.restore();
       }


       public void onAnimationUpdate(ValueAnimator animation) {
           invalidate();
       }


   }
}

RES文件我就不贴了,很简单,之前的差不多,在下面我就把运行的图贴上来吧:






好了以上就是其截图,如果自己有兴趣的话,可以自己对其源码进行修改与在整合,以便做出更多优美的效果来.....

posted @ 2012-02-17 22:18  移动应用开发  阅读(280)  评论(0编辑  收藏  举报