Created
November 19, 2012 16:20
-
-
Save roman-mazur/4111584 to your computer and use it in GitHub Desktop.
A simple example of an Android activity for explaining concepts of property animations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.testtest; | |
import android.app.Activity; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.ColorFilter; | |
import android.graphics.Paint; | |
import android.graphics.Rect; | |
import android.graphics.drawable.Drawable; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.os.Message; | |
import android.os.SystemClock; | |
import android.view.View; | |
public class AnimationActivity extends Activity { | |
@SuppressWarnings("deprecation") | |
@Override | |
public void onCreate(final Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
final View view = new View(this); | |
final AnimatedCrossDrawable animDrawable = new AnimatedCrossDrawable(); | |
view.setBackgroundDrawable(animDrawable); | |
setContentView(view); | |
// Start very simple animation | |
// Video: http://www.youtube.com/watch?v=I5C6cNZjIYY | |
new LevelAnimator(animDrawable) | |
// uncomment the following lines for interpolation example: | |
// Video: http://www.youtube.com/watch?v=jBLPEUpdUps | |
// { | |
// | |
// private float line(float x1, float y1, float x2, float y2, float x) { | |
// return (x - x1) / (x2 - x1) * (y2 - y1) + y1; | |
// } | |
// | |
// @Override | |
// protected float interpolate(final float fraction) { | |
// final float[] x = {0f, 0.2f, 0.4f, 0.6f, 0.8f, 1f}; | |
// final float[] y = {0f, 0.2f, 0.1f, 0.6f, 0.5f, 1f}; | |
// | |
// for (int i = 0; i < x.length; i++) { | |
// if (fraction < x[i]) { | |
// return line(x[i - 1], y[i - 1], x[i], y[i], fraction); | |
// } | |
// } | |
// | |
// return fraction; | |
// } | |
// | |
// } | |
.setDuration(3000) | |
.start(0, AnimatedCrossDrawable.MAX_LEVEL); | |
// use this code instead to utilize property animations API | |
// ObjectAnimator.ofInt(animDrawable, "level", 0, AnimatedCrossDrawable.MAX_LEVEL) | |
// .setDuration(3000) | |
// .start(); | |
} | |
public static class LevelAnimator extends Handler { | |
private static final int MSG_ANIMATE = 1; | |
private long startTime, duration; | |
private int begin, end; | |
private final Drawable drawable; | |
public LevelAnimator(final Drawable drawable) { | |
this.drawable = drawable; | |
} | |
@Override | |
public void handleMessage(Message msg) { | |
switch (msg.what) { | |
case MSG_ANIMATE: | |
final int curLevel = getLevelValue(interpolate(getFraction(SystemClock.uptimeMillis()))); | |
drawable.setLevel(curLevel); | |
if (SystemClock.uptimeMillis() < startTime + duration) { | |
sendEmptyMessage(MSG_ANIMATE); | |
} | |
break; | |
default: | |
} | |
} | |
private float getFraction(final long curTime){ | |
float passed = curTime - startTime; | |
final float fraction = passed /duration; | |
return Math.min(fraction, 1.0f); | |
} | |
protected float interpolate(final float fraction) { | |
return fraction; | |
} | |
private int getLevelValue(final float part) { | |
final int interval = end - begin; | |
return begin + (int) (part * interval); | |
} | |
public LevelAnimator setDuration(long duration) { | |
this.duration = duration; | |
return this; | |
} | |
public LevelAnimator start(final int begin, final int end){ | |
startTime = SystemClock.uptimeMillis(); | |
this.begin = begin; | |
this.end = end; | |
sendEmptyMessage(MSG_ANIMATE); | |
return this; | |
} | |
} | |
public static class AnimatedCrossDrawable extends Drawable { | |
private static final int MAX_LEVEL = 100000; | |
final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
{ | |
paint.setColor(Color.BLUE); | |
} | |
@Override | |
protected boolean onLevelChange(int level) { | |
super.onLevelChange(level); | |
invalidateSelf(); | |
return true; | |
} | |
@Override | |
public void draw(final Canvas canvas) { | |
Rect bounds = getBounds(); | |
float part = (float)getLevel() / MAX_LEVEL; | |
float x = bounds.left + part * bounds.width(); | |
float y = bounds.top + part * bounds.height(); | |
canvas.drawLine(0f, 0f, x, y, paint); | |
canvas.drawLine(0f, bounds.bottom, x, bounds.bottom - y, paint); | |
} | |
@Override | |
public void setAlpha(final int alpha) { } | |
@Override | |
public void setColorFilter(final ColorFilter cf) { } | |
@Override | |
public int getOpacity() { return 0; } | |
@Override | |
public int getIntrinsicHeight() { return 0; } | |
@Override | |
public int getIntrinsicWidth() { return 0; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment