android4動畫應用二

來源:互聯網
上載者:User

之前本想把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檔案我就不貼了,很簡單,之前的差不多,在下面我就把啟動並執行圖貼上來吧:

好了以上就是其,如果自己有興趣的話,可以自己對其源碼進行修改與在整合,以便做出更多優美的效果來.....

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.