歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 4應用動畫案列一

Android 4應用動畫案列一

日期:2017/3/1 10:38:22   编辑:Linux編程

這段時間一直都在看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的文件很簡單,自己稍微看下就可以了,好了,一次一個案列吧

Copyright © Linux教程網 All Rights Reserved