Add ApiDemo to show the new seeking behavior of Animator
Change-Id: I4ad4e322af9c1d8419e1d7f8bea6b20951a1dc4f
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
* Copyright (C) 2010 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.apis.animation;
|
||||
|
||||
// Need the following import to get access to the app resources, since this
|
||||
// class is in a sub-package.
|
||||
import com.example.android.apis.R;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.animation.Animatable;
|
||||
import android.animation.Animator;
|
||||
import android.animation.PropertyAnimator;
|
||||
import android.animation.Sequencer;
|
||||
import android.animation.Animatable.AnimatableListener;
|
||||
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.BounceInterpolator;
|
||||
import android.widget.Button;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.SeekBar;
|
||||
|
||||
/**
|
||||
* This application demonstrates the seeking capability of Animator. The SeekBar in the
|
||||
* UI allows you to set the position of the animation. Pressing the Run button will play from
|
||||
* the current position of the animation.
|
||||
*/
|
||||
public class AnimationSeeking extends Activity {
|
||||
|
||||
private static final int DURATION = 1500;
|
||||
private SeekBar mSeekBar;
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.animation_seeking);
|
||||
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();
|
||||
}
|
||||
});
|
||||
|
||||
mSeekBar = (SeekBar) findViewById(R.id.seekBar);
|
||||
mSeekBar.setMax(DURATION);
|
||||
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
|
||||
|
||||
@Override
|
||||
public void onStopTrackingTouch(SeekBar seekBar) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartTrackingTouch(SeekBar seekBar) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProgressChanged(SeekBar seekBar, int progress,
|
||||
boolean fromUser) {
|
||||
// prevent seeking on app creation
|
||||
if (animView.getHeight() != 0) {
|
||||
animView.seek(progress);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public class MyAnimationView extends View implements Animator.AnimatorUpdateListener, AnimatableListener {
|
||||
|
||||
private static final int RED = 0xffFF8080;
|
||||
private static final int BLUE = 0xff8080FF;
|
||||
private static final int CYAN = 0xff80ffff;
|
||||
private static final int GREEN = 0xff80ff80;
|
||||
private static final float BALL_SIZE = 100f;
|
||||
|
||||
public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
|
||||
Sequencer animation = null;
|
||||
Animator bounceAnim = null;
|
||||
ShapeHolder ball = null;
|
||||
|
||||
public MyAnimationView(Context context) {
|
||||
super(context);
|
||||
ball = addBall(200, 0);
|
||||
}
|
||||
|
||||
private void createAnimation() {
|
||||
if (bounceAnim == null) {
|
||||
bounceAnim = new PropertyAnimator(1500, ball, "y",
|
||||
ball.getY(), getHeight() - BALL_SIZE);
|
||||
bounceAnim.setInterpolator(new BounceInterpolator());
|
||||
bounceAnim.addUpdateListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void startAnimation() {
|
||||
createAnimation();
|
||||
bounceAnim.start();
|
||||
}
|
||||
|
||||
public void seek(long seekTime) {
|
||||
createAnimation();
|
||||
bounceAnim.setCurrentPlayTime(seekTime);
|
||||
}
|
||||
|
||||
private ShapeHolder addBall(float x, float y) {
|
||||
OvalShape circle = new OvalShape();
|
||||
circle.resize(BALL_SIZE, BALL_SIZE);
|
||||
ShapeDrawable drawable = new ShapeDrawable(circle);
|
||||
ShapeHolder shapeHolder = new ShapeHolder(drawable);
|
||||
shapeHolder.setX(x);
|
||||
shapeHolder.setY(y);
|
||||
int red = (int)(100 + Math.random() * 155);
|
||||
int green = (int)(100 + 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) {
|
||||
canvas.translate(ball.getX(), ball.getY());
|
||||
ball.getShape().draw(canvas);
|
||||
}
|
||||
|
||||
public void onAnimationUpdate(Animator animation) {
|
||||
invalidate();
|
||||
long playtime = bounceAnim.getCurrentPlayTime();
|
||||
//mSeekBar.setProgress((int)playtime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAnimationCancel(Animatable animation) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAnimationEnd(Animatable animation) {
|
||||
balls.remove(((PropertyAnimator)animation).getTarget());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAnimationRepeat(Animatable animation) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAnimationStart(Animatable animation) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright (C) 2010 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.apis.animation;
|
||||
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.RadialGradient;
|
||||
import android.graphics.drawable.ShapeDrawable;
|
||||
import android.graphics.drawable.shapes.Shape;
|
||||
import android.view.View;
|
||||
|
||||
/**
|
||||
* A data structure that holds a Shape and various properties that can be used to define
|
||||
* how the shape is drawn.
|
||||
*/
|
||||
public class ShapeHolder {
|
||||
private float x = 0, y = 0;
|
||||
private ShapeDrawable shape;
|
||||
private int color;
|
||||
private RadialGradient gradient;
|
||||
private float alpha = 1f;
|
||||
private Paint paint;
|
||||
|
||||
public void setPaint(Paint value) {
|
||||
paint = value;
|
||||
}
|
||||
public Paint getPaint() {
|
||||
return paint;
|
||||
}
|
||||
|
||||
public void setX(float value) {
|
||||
x = value;
|
||||
}
|
||||
public float getX() {
|
||||
return x;
|
||||
}
|
||||
public void setY(float value) {
|
||||
y = value;
|
||||
}
|
||||
public float getY() {
|
||||
return y;
|
||||
}
|
||||
public void setShape(ShapeDrawable value) {
|
||||
shape = value;
|
||||
}
|
||||
public ShapeDrawable getShape() {
|
||||
return shape;
|
||||
}
|
||||
public int getColor() {
|
||||
return color;
|
||||
}
|
||||
public void setColor(int value) {
|
||||
color = value;
|
||||
}
|
||||
public void setGradient(RadialGradient value) {
|
||||
gradient = value;
|
||||
}
|
||||
public RadialGradient getGradient() {
|
||||
return gradient;
|
||||
}
|
||||
|
||||
public void setAlpha(float alpha) {
|
||||
this.alpha = alpha;
|
||||
shape.setAlpha((int)((alpha * 255f) + .5f));
|
||||
}
|
||||
|
||||
public float getWidth() {
|
||||
//return shape.getIntrinsicWidth();
|
||||
return shape.getShape().getWidth();
|
||||
}
|
||||
public void setWidth(float width) {
|
||||
//shape.setIntrinsicWidth(width);
|
||||
Shape s = shape.getShape();
|
||||
s.resize(width, s.getHeight());
|
||||
}
|
||||
|
||||
public float getHeight() {
|
||||
return shape.getShape().getHeight();
|
||||
}
|
||||
public void setHeight(float height) {
|
||||
Shape s = shape.getShape();
|
||||
s.resize(s.getWidth(), height);
|
||||
//shape.setIntrinsicHeight(height);
|
||||
}
|
||||
|
||||
public ShapeHolder(ShapeDrawable s) {
|
||||
shape = s;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user