OSDN Git Service

Adding demo for xml animator loading
authorChet Haase <chet@google.com>
Wed, 18 Aug 2010 17:28:06 +0000 (10:28 -0700)
committerChet Haase <chet@google.com>
Wed, 25 Aug 2010 02:50:12 +0000 (19:50 -0700)
Change-Id: I77d4e4f3b26c47e7973dd64282c12a9d6c3b5dd1

samples/ApiDemos/AndroidManifest.xml
samples/ApiDemos/res/anim/animator.xml [new file with mode: 0644]
samples/ApiDemos/res/anim/property_animator.xml [new file with mode: 0644]
samples/ApiDemos/res/anim/sequencer.xml [new file with mode: 0644]
samples/ApiDemos/res/layout/animation_custom_evaluator.xml [new file with mode: 0644]
samples/ApiDemos/res/layout/animation_loading.xml [new file with mode: 0644]
samples/ApiDemos/src/com/example/android/apis/animation/AnimationLoading.java [new file with mode: 0644]
samples/ApiDemos/src/com/example/android/apis/animation/CustomEvaluator.java [new file with mode: 0644]

index a8d5563..fe8d84a 100644 (file)
         <!--     ANDROID.ANIMATION PACKAGE SAMPLES         -->
         <!-- ************************************* -->
 
+        <activity android:name=".animation.AnimationLoading" android:label="Animation/Loading">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.SAMPLE_CODE" />
+            </intent-filter>
+        </activity>
+
         <activity android:name=".animation.AnimationSeeking" android:label="Animation/Seeking">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
             </intent-filter>
         </activity>
 
+        <activity android:name=".animation.CustomEvaluator"
+                  android:label="Animation/Custom Evaluator">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.SAMPLE_CODE" />
+            </intent-filter>
+        </activity>
+
         <activity android:name=".animation.ListFlipper" android:label="Animation/View Flip">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
diff --git a/samples/ApiDemos/res/anim/animator.xml b/samples/ApiDemos/res/anim/animator.xml
new file mode 100644 (file)
index 0000000..2432f19
--- /dev/null
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+
+<animator xmlns:android="http://schemas.android.com/apk/res/android"
+    android:duration="1000"
+    android:valueFrom="1"
+    android:valueTo="0"
+    android:valueType="floatType"
+    android:repeatCount="1"
+    android:repeatMode="reverse"/>
diff --git a/samples/ApiDemos/res/anim/property_animator.xml b/samples/ApiDemos/res/anim/property_animator.xml
new file mode 100644 (file)
index 0000000..8c0f006
--- /dev/null
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+
+<property xmlns:android="http://schemas.android.com/apk/res/android"
+    android:duration="1000"
+    android:valueFrom="0"
+    android:valueTo="200"
+    android:valueType="floatType"
+    android:propertyName="y"
+    android:repeatCount="1"
+    android:repeatMode="reverse"/>
diff --git a/samples/ApiDemos/res/anim/sequencer.xml b/samples/ApiDemos/res/anim/sequencer.xml
new file mode 100644 (file)
index 0000000..8da606e
--- /dev/null
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+
+<sequencer>
+    <property xmlns:android="http://schemas.android.com/apk/res/android"
+        android:duration="1000"
+        android:valueTo="200"
+        android:valueType="floatType"
+        android:propertyName="x"
+        android:repeatCount="1"
+        android:repeatMode="reverse"/>
+    <property xmlns:android="http://schemas.android.com/apk/res/android"
+        android:duration="1000"
+        android:valueFrom="0"
+        android:valueTo="400"
+        android:valueType="floatType"
+        android:propertyName="y"
+        android:repeatCount="1"
+        android:repeatMode="reverse"/>
+</sequencer>
diff --git a/samples/ApiDemos/res/layout/animation_custom_evaluator.xml b/samples/ApiDemos/res/layout/animation_custom_evaluator.xml
new file mode 100644 (file)
index 0000000..fd033f6
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical"
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent"
+    android:id="@+id/container"
+    >
+    <Button
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="Play"
+        android:id="@+id/startButton"
+        />
+</LinearLayout>
diff --git a/samples/ApiDemos/res/layout/animation_loading.xml b/samples/ApiDemos/res/layout/animation_loading.xml
new file mode 100644 (file)
index 0000000..f49c5b0
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical"
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent"
+    android:id="@+id/container"
+    >
+    <Button
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="Run"
+        android:id="@+id/startButton"
+        />
+</LinearLayout>
diff --git a/samples/ApiDemos/src/com/example/android/apis/animation/AnimationLoading.java b/samples/ApiDemos/src/com/example/android/apis/animation/AnimationLoading.java
new file mode 100644 (file)
index 0000000..68b9fdf
--- /dev/null
@@ -0,0 +1,153 @@
+/*
+ * 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 android.animation.AnimatableInflater;
+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.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;
+
+/**
+ * This application demonstrates loading Animatable objects from XML resources.
+ */
+public class AnimationLoading extends Activity {
+
+    private static final int DURATION = 1500;
+
+    /** Called when the activity is first created. */
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.animation_loading);
+        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 Animator.AnimatorUpdateListener {
+
+        private static final float BALL_SIZE = 100f;
+
+        public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
+        Animatable animation = null;
+
+        public MyAnimationView(Context context) {
+            super(context);
+            addBall(100, 0);
+            addBall(250, 0);
+            addBall(400, 0);
+        }
+
+        private void createAnimation() {
+            if (animation == null) {
+                PropertyAnimator anim =
+                        (PropertyAnimator) AnimatableInflater.
+                                loadAnimatable(getApplicationContext(), R.anim.property_animator);
+                anim.addUpdateListener(this);
+                anim.setTarget(balls.get(0));
+
+                Animator fader =
+                        (Animator) AnimatableInflater.loadAnimatable(getApplicationContext(),
+                        R.anim.animator);
+                fader.addUpdateListener(new Animator.AnimatorUpdateListener() {
+                    public void onAnimationUpdate(Animator animation) {
+                        balls.get(1).setAlpha((Float) animation.getAnimatedValue());
+                    }
+                });
+
+                Sequencer seq =
+                        (Sequencer) AnimatableInflater.loadAnimatable(getApplicationContext(),
+                        R.anim.sequencer);
+                seq.setTarget(balls.get(2));
+
+                animation = new Sequencer();
+                ((Sequencer) animation).playTogether(anim, fader, seq);
+            }
+        }
+
+        public void startAnimation() {
+            createAnimation();
+            animation.start();
+        }
+
+        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) {
+            for (ShapeHolder ball : balls) {
+                canvas.translate(ball.getX(), ball.getY());
+                ball.getShape().draw(canvas);
+                canvas.translate(-ball.getX(), -ball.getY());
+            }
+        }
+
+        public void onAnimationUpdate(Animator animation) {
+
+            invalidate();
+            ShapeHolder ball = balls.get(0);
+            ball.setY((Float)animation.getAnimatedValue());
+        }
+    }
+}
\ No newline at end of file
diff --git a/samples/ApiDemos/src/com/example/android/apis/animation/CustomEvaluator.java b/samples/ApiDemos/src/com/example/android/apis/animation/CustomEvaluator.java
new file mode 100644 (file)
index 0000000..69326f8
--- /dev/null
@@ -0,0 +1,182 @@
+/*
+ * 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 android.animation.TypeEvaluator;
+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.AccelerateInterpolator;
+import android.widget.Button;
+import android.widget.LinearLayout;
+
+public class CustomEvaluator extends Activity {
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.animation_reversing);
+        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 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 {
+        @Override
+        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 Animator.AnimatorUpdateListener {
+
+        public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
+        Animator 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, 500f);
+                bounceAnim = new PropertyAnimator(1500, ballHolder, "xY",
+                        endXY);
+                bounceAnim.setEvaluator(new XYEvaluator());
+                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(Animator animation) {
+            invalidate();
+        }
+
+    }
+}
\ No newline at end of file