Android Tutorial - AnimationSet
Using AnimationSet
//package jp.mrshiromi.net.onamaenaani.util; import android.content.Context; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.OvershootInterpolator; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; class AnimUtil { public static final int DURATION = 10000; public static final int ALPHA_DURATION = 500; public static Animation createAnimation() { AnimationSet set = new AnimationSet(false); AlphaAnimation aa = new AlphaAnimation(0f, 1f); aa.setDuration(ALPHA_DURATION * 4); TranslateAnimation ta1 = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, -0.1f, Animation.RELATIVE_TO_PARENT, 0.1f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); ta1.setDuration(DURATION / 5); ta1.setRepeatMode(Animation.REVERSE); ta1.setRepeatCount(DURATION / (DURATION / 5) - 1); ScaleAnimation sa = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(DURATION - ALPHA_DURATION); TranslateAnimation ta2 = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, 0.8f, Animation.RELATIVE_TO_PARENT, 0f); ta2.setDuration(DURATION + ALPHA_DURATION); TranslateAnimation ta3 = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, -0.1f, Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, 0f); ta3.setDuration(DURATION / 5); ta3.setStartOffset(DURATION); set.addAnimation(aa); set.addAnimation(ta1); set.addAnimation(sa); set.addAnimation(ta2); set.addAnimation(ta3); set.setFillAfter(true); return set; } }
Change center and LinearInterpolator
package com.ai.android.ExerciseSystemIntents; import android.util.Log; import android.view.animation.AccelerateInterpolator; import android.view.animation.Animation; import android.view.animation.LinearInterpolator; import android.view.animation.Transformation; import android.graphics.Camera; import android.graphics.Matrix; public class ViewAnimation3 extends Animation { float centerX, centerY; public ViewAnimation3(){} @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); centerX = width/2.0f; centerY = height/2.0f; setDuration(2500); setFillAfter(true); setInterpolator(new LinearInterpolator()); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { final Matrix matrix = t.getMatrix(); matrix.setScale(interpolatedTime, interpolatedTime); matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); } }
Create ScaleAnimation
//package jp.mrshiromi.net.onamaenaani.util; import android.content.Context; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.OvershootInterpolator; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; class AnimUtil { public static Animation createSplashLogoAnimation() { AnimationSet set = new AnimationSet(false); ScaleAnimation sa = new ScaleAnimation(1.0f, 1.1f, 1.0f, 1.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(1000); sa.setRepeatCount(5); sa.setRepeatMode(Animation.REVERSE); set.addAnimation(sa); set.setInterpolator(new OvershootInterpolator(1.5f)); return set; } }
Scale
package com.ai.android.ExerciseSystemIntents; import android.util.Log; import android.view.animation.AccelerateInterpolator; import android.view.animation.Animation; import android.view.animation.LinearInterpolator; import android.view.animation.Transformation; import android.graphics.Camera; import android.graphics.Matrix; public class ViewAnimation2 extends Animation { public ViewAnimation2(){} @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); setDuration(2500); setFillAfter(true); setInterpolator(new LinearInterpolator()); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { final Matrix matrix = t.getMatrix(); matrix.setScale(interpolatedTime, interpolatedTime); } }
How the AnimatorListener events work.
package app.test; // Need the following import to get access to the app resources, since this // class is in a sub-package. import android.animation.Animator; import android.animation.ObjectAnimator; import android.widget.CheckBox; import android.widget.TextView; import java.util.ArrayList; import android.animation.ValueAnimator; import android.animation.AnimatorSet; 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; 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. */ 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) { shape.getPaint().setColor(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.getShape().getWidth(); } public void setWidth(float 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); } public ShapeHolder(ShapeDrawable s) { shape = s; } } /** * This demo shows how the AnimatorListener events work. */ public class Test extends Activity { TextView startText, repeatText, cancelText, endText; TextView startTextAnimator, repeatTextAnimator, cancelTextAnimator, endTextAnimator; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout container = (LinearLayout) findViewById(R.id.container); final MyAnimationView animView = new MyAnimationView(this); container.addView(animView); startText = (TextView) findViewById(R.id.startText); startText.setAlpha(.5f); repeatText = (TextView) findViewById(R.id.repeatText); repeatText.setAlpha(.5f); cancelText = (TextView) findViewById(R.id.cancelText); cancelText.setAlpha(.5f); endText = (TextView) findViewById(R.id.endText); endText.setAlpha(.5f); startTextAnimator = (TextView) findViewById(R.id.startTextAnimator); startTextAnimator.setAlpha(.5f); repeatTextAnimator = (TextView) findViewById(R.id.repeatTextAnimator); repeatTextAnimator.setAlpha(.5f); cancelTextAnimator = (TextView) findViewById(R.id.cancelTextAnimator); cancelTextAnimator.setAlpha(.5f); endTextAnimator = (TextView) findViewById(R.id.endTextAnimator); endTextAnimator.setAlpha(.5f); final CheckBox endCB = (CheckBox) findViewById(R.id.endCB); Button starter = (Button) findViewById(R.id.startButton); starter.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { animView.startAnimation(endCB.isChecked()); } }); Button canceler = (Button) findViewById(R.id.cancelButton); canceler.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { animView.cancelAnimation(); } }); Button ender = (Button) findViewById(R.id.endButton); ender.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { animView.endAnimation(); } }); } public class MyAnimationView extends View implements Animator.AnimatorListener, ValueAnimator.AnimatorUpdateListener { public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>(); Animator animation; ShapeHolder ball = null; boolean endImmediately = false; public MyAnimationView(Context context) { super(context); ball = createBall(25, 25); } private void createAnimation() { if (animation == null) { ObjectAnimator yAnim = ObjectAnimator.ofFloat(ball, "y", ball.getY(), getHeight() - 50f).setDuration(1500); yAnim.setRepeatCount(0); yAnim.setRepeatMode(ValueAnimator.REVERSE); yAnim.setInterpolator(new AccelerateInterpolator(2f)); yAnim.addUpdateListener(this); yAnim.addListener(this); ObjectAnimator xAnim = ObjectAnimator.ofFloat(ball, "x", ball.getX(), ball.getX() + 300).setDuration(1000); xAnim.setStartDelay(0); xAnim.setRepeatCount(0); xAnim.setRepeatMode(ValueAnimator.REVERSE); xAnim.setInterpolator(new AccelerateInterpolator(2f)); ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(ball, "alpha", 1f, .5f). setDuration(1000); AnimatorSet alphaSeq = new AnimatorSet(); alphaSeq.play(alphaAnim); animation = new AnimatorSet(); ((AnimatorSet) animation).playTogether(yAnim, xAnim); animation.addListener(this); } } public void startAnimation(boolean endImmediately) { this.endImmediately = endImmediately; startText.setAlpha(.5f); repeatText.setAlpha(.5f); cancelText.setAlpha(.5f); endText.setAlpha(.5f); startTextAnimator.setAlpha(.5f); repeatTextAnimator.setAlpha(.5f); cancelTextAnimator.setAlpha(.5f); endTextAnimator.setAlpha(.5f); createAnimation(); animation.start(); } public void cancelAnimation() { createAnimation(); animation.cancel(); } public void endAnimation() { createAnimation(); animation.end(); } 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(); } public void onAnimationStart(Animator animation) { if (animation instanceof AnimatorSet) { startText.setAlpha(1f); } else { startTextAnimator.setAlpha(1f); } if (endImmediately) { animation.end(); } } public void onAnimationEnd(Animator animation) { if (animation instanceof AnimatorSet) { endText.setAlpha(1f); } else { endTextAnimator.setAlpha(1f); } } public void onAnimationCancel(Animator animation) { if (animation instanceof AnimatorSet) { cancelText.setAlpha(1f); } else { cancelTextAnimator.setAlpha(1f); } } public void onAnimationRepeat(Animator animation) { if (animation instanceof AnimatorSet) { repeatText.setAlpha(1f); } else { repeatTextAnimator.setAlpha(1f); } } } } //main.xml <?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="match_parent" android:layout_height="match_parent" android:id="@+id/container" > <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Play" android:id="@+id/startButton" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel" android:id="@+id/cancelButton" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="End" android:id="@+id/endButton" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="End Immediately" android:id="@+id/endCB" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sequencer Events: " /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start " android:id="@+id/startText" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Repeat " android:id="@+id/repeatText" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel " android:id="@+id/cancelText" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="End" android:id="@+id/endText" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Animator Events: " /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start " android:id="@+id/startTextAnimator" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Repeat " android:id="@+id/repeatTextAnimator" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel " android:id="@+id/cancelTextAnimator" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="End" android:id="@+id/endTextAnimator" /> </LinearLayout> </LinearLayout>
Create RotateAnimation
//package jp.mrshiromi.net.onamaenaani.util; import android.content.Context; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.OvershootInterpolator; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; class AnimUtil { public static Animation createAnimation(Context context) { AnimationSet set = new AnimationSet(true); RotateAnimation ra = new RotateAnimation(-8, 8, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ra.setDuration(6000); ra.setRepeatMode(Animation.REVERSE); ra.setRepeatCount(10); ScaleAnimation sa = new ScaleAnimation(0.95f, 1.05f, 0.95f, 1.05f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(6000); sa.setRepeatMode(Animation.REVERSE); sa.setRepeatCount(10); set.addAnimation(ra); set.addAnimation(sa); set.setFillAfter(true); return set; } }
Create TranslateAnimation
//package jp.mrshiromi.net.onamaenaani.util; import android.content.Context; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.OvershootInterpolator; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; class AnimUtil { public static Animation createKumoAnimation(Context context) { AnimationSet set = new AnimationSet(true); ScaleAnimation sa = new ScaleAnimation(0.98f, 1.02f, 0.98f, 1.02f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(12000); sa.setRepeatMode(Animation.REVERSE); sa.setRepeatCount(5); TranslateAnimation ta = new TranslateAnimation( Animation.RELATIVE_TO_SELF, -0.05f, Animation.RELATIVE_TO_SELF, 0.05f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f); ta.setDuration(6000); ta.setRepeatMode(Animation.REVERSE); ta.setRepeatCount(10); set.addAnimation(sa); set.addAnimation(ta); set.setFillAfter(true); return set; } }
Translate and alpha animation
package app.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ViewFlipper; public class Test extends Activity { static String[] items = { "lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing", "elit", "morbi", "vel", "ligula", "vitae", "arcu", "aliquet", "mollis", "etiam", "vel", "erat", "placerat", "ante", "porttitor", "sodales", "pellentesque", "augue", "purus" }; ViewFlipper flipper; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); flipper = (ViewFlipper) findViewById(R.id.details); flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in)); flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out)); for (String item : items) { Button btn = new Button(this); btn.setText(item); flipper.addView(btn, new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); } flipper.setFlipInterval(2000); flipper.startFlipping(); } } //main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ViewFlipper android:id="@+id/details" android:layout_width="fill_parent" android:layout_height="fill_parent" > </ViewFlipper> </LinearLayout> //anim/push_left_in.xml <?xml version="1.0" encoding="utf-8"?> //anim/push_left_out.xml <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="300"/> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" /> </set> <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="300"/> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" /> </set>
Animation: fade in, fade out
package app.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; public class Test extends Activity implements View.OnClickListener { View viewToAnimate; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button)findViewById(R.id.toggleButton); button.setOnClickListener(this); viewToAnimate = findViewById(R.id.theView); } @Override public void onClick(View v) { if(viewToAnimate.getVisibility() == View.VISIBLE) { Animation out = AnimationUtils.makeOutAnimation(this, true); viewToAnimate.startAnimation(out); viewToAnimate.setVisibility(View.INVISIBLE); } else { Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in); viewToAnimate.startAnimation(in); viewToAnimate.setVisibility(View.VISIBLE); } } } //main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/toggleButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Click to Toggle" /> <View android:id="@+id/theView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#AAA" /> </LinearLayout>
Frame based animation
package app.test; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.ComponentName; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; class FrameAnimationActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.frame_animations_layout); this.setupButton(); } private void setupButton() { Button b = (Button)this.findViewById(R.id.startFAButtonId); b.setOnClickListener( new Button.OnClickListener(){ public void onClick(View v) { parentButtonClicked(v); } }); } private void parentButtonClicked(View v) { animate(); } private void animate() { ImageView imgView = (ImageView)findViewById(R.id.imageView); imgView.setVisibility(ImageView.VISIBLE); imgView.setBackgroundResource(R.drawable.frame_animation); AnimationDrawable frameAnimation = (AnimationDrawable) imgView.getBackground(); if (frameAnimation.isRunning()) { frameAnimation.stop(); } else { frameAnimation.stop(); frameAnimation.start(); } } } public class Test extends Activity { Menu myMenu = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu) { //call the parent to attach any system level menus super.onCreateOptionsMenu(menu); this.myMenu = menu; MenuInflater mi = this.getMenuInflater(); mi.inflate(R.menu.main_menu,menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { handleMenus(item); return true; } private void handleMenus(MenuItem item) { this.appendMenuItemText(item); if (item.getItemId() == R.id.menu_clear) { this.emptyText(); } else if (item.getItemId() == R.id.menu_list_animation) { Intent intent = new Intent(this, FrameAnimationActivity.class); startActivity(intent); } } private TextView getTextView() { TextView tv = (TextView)this.findViewById(R.id.textViewId); return tv; } public void appendText(String text) { TextView tv = (TextView)this.findViewById(R.id.textViewId); tv.setText(tv.getText() + text); } public void appendMenuItemText(MenuItem menuItem) { String title = menuItem.getTitle().toString(); TextView tv = (TextView)this.findViewById(R.id.textViewId); tv.setText(tv.getText() + "\n" + title + ":" + menuItem.getItemId()); } private void emptyText() { TextView tv = (TextView)this.findViewById(R.id.textViewId); tv.setText(""); } } //main.xml <menu xmlns:android="http://schemas.android.com/apk/res/android"> <!-- This group uses the default category. --> <group android:id="@+id/menuGroup_Main"> <item android:id="@+id/menu_list_animation" android:orderInCategory="1" android:title="Animate Frame" /> <item android:id="@+id/menu_clear" android:orderInCategory="10" android:title="clear" /> </group> </menu>
extends Animation to create your own animation
import android.util.Log; import android.view.animation.AccelerateInterpolator; import android.view.animation.Animation; import android.view.animation.LinearInterpolator; import android.view.animation.Transformation; import android.graphics.Camera; import android.graphics.Matrix; public class ViewAnimation extends Animation { float centerX, centerY; public ViewAnimation(float cx, float cy) { centerX = cx; centerY = cy; } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); Log.d("d","width:" + width); Log.d("d","height:" + height); Log.d("d","pwidth:" + parentWidth); Log.d("d","pheight:" + parentHeight); setDuration(2500); setFillAfter(true); setInterpolator(new LinearInterpolator()); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { applyTransformationNew(interpolatedTime,t); } protected void applyTransformationNew(float interpolatedTime, Transformation t) { //Log.d("d","transform:" + interpolatedTime); final Matrix matrix = t.getMatrix(); matrix.setScale(interpolatedTime, interpolatedTime); matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); } }
Slide out down animation, bounce in down animation
package app.test; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.app.ListFragment; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; class TitlesFragment extends ListFragment { private Test myActivity = null; int mCurCheckPosition = 0; @Override public void onInflate(AttributeSet attrs, Bundle icicle) { for (int i = 0; i < attrs.getAttributeCount(); i++) { Log.v("",attrs.getAttributeName(i) + " = " + attrs.getAttributeValue(i)); } super.onInflate(attrs, icicle); } @Override public void onAttach(Activity myActivity) { super.onAttach(myActivity); this.myActivity = (Test) myActivity; } @Override public void onCreate(Bundle icicle) { if (icicle != null) { for (String key : icicle.keySet()) { Log.v(Test.TAG, " " + key); } }super.onCreate(icicle); if (icicle != null) { mCurCheckPosition = icicle.getInt("curChoice", 0); } } @Override public View onCreateView(LayoutInflater myInflater, ViewGroup container, Bundle icicle) { return super.onCreateView(myInflater, container, icicle); } @Override public void onActivityCreated(Bundle icicle) { if (icicle != null) { for (String key : icicle.keySet()) { Log.v(Test.TAG, " " + key); } } super.onActivityCreated(icicle); setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, new String[] { "A", "B", "C", "D", })); ListView lv = getListView(); lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); lv.setSelection(mCurCheckPosition); myActivity.showDetails(mCurCheckPosition); } @Override public void onStart() { super.onStart(); } @Override public void onResume() { super.onResume(); } @Override public void onPause() { super.onPause(); } @Override public void onSaveInstanceState(Bundle icicle) { super.onSaveInstanceState(icicle); icicle.putInt("curChoice", mCurCheckPosition); } @Override public void onListItemClick(ListView l, View v, int pos, long id) { myActivity.showDetails(pos); mCurCheckPosition = pos; } @Override public void onStop() { super.onStop(); } @Override public void onDestroyView() { super.onDestroyView(); } @Override public void onDestroy() { super.onDestroy(); } @Override public void onDetach() { super.onDetach(); myActivity = null; } } class DetailsActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { finish(); return; } if (getIntent() != null) { DetailsFragment details = DetailsFragment.newInstance(getIntent() .getExtras()); getFragmentManager().beginTransaction() .add(android.R.id.content, details).commit(); } } } class DetailsFragment extends Fragment { private int mIndex = 0; public static DetailsFragment newInstance(int index) { DetailsFragment df = new DetailsFragment(); Bundle args = new Bundle(); args.putInt("index", index); df.setArguments(args); return df; } public static DetailsFragment newInstance(Bundle bundle) { int index = bundle.getInt("index", 0); return newInstance(index); } @Override public void onInflate(AttributeSet attrs, Bundle savedInstanceState) { for (int i = 0; i < attrs.getAttributeCount(); i++) Log.v("",attrs.getAttributeName(i) + " = " + attrs.getAttributeValue(i)); super.onInflate(attrs, savedInstanceState); } @Override public void onAttach(Activity myActivity) { super.onAttach(myActivity); } @Override public void onCreate(Bundle myBundle) { if (myBundle != null) { for (String key : myBundle.keySet()) { Log.v(Test.TAG, " " + key); } }super.onCreate(myBundle); mIndex = getArguments().getInt("index", 0); } public int getShownIndex() { return mIndex; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.row, container, false); TextView text1 = (TextView) v.findViewById(R.id.text1); text1.setText(""); return v; } @Override public void onActivityCreated(Bundle savedState) { if (savedState != null) { for (String key : savedState.keySet()) { Log.v(Test.TAG, " " + key); } } super.onActivityCreated(savedState); } @Override public void onStart() { super.onStart(); } @Override public void onResume() { super.onResume(); } @Override public void onPause() { super.onPause(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } @Override public void onStop() { super.onStop(); } @Override public void onDestroyView() { super.onDestroyView(); } @Override public void onDestroy() { super.onDestroy(); } @Override public void onDetach() { super.onDetach(); } } public class Test extends Activity { public static final String TAG = "Test"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FragmentManager.enableDebugLogging(true); setContentView(R.layout.main); } @Override public void onAttachFragment(Fragment fragment) { super.onAttachFragment(fragment); } @Override public void onStart() { super.onStart(); } @Override public void onResume() { super.onResume(); } @Override public void onPause() { super.onPause(); } @Override public void onStop() { super.onStop(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } @Override public void onDestroy() { super.onDestroy(); } public boolean isMultiPane() { return getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE; } public void showDetails(int index) { if (isMultiPane()) { DetailsFragment details = (DetailsFragment) getFragmentManager() .findFragmentById(R.id.details); if (details == null || details.getShownIndex() != index) { details = DetailsFragment.newInstance(index); FragmentTransaction ft = getFragmentManager() .beginTransaction(); ft.setCustomAnimations(R.animator.bounce_in_down, R.animator.slide_out_down); ft.replace(R.id.details, details); ft.commit(); getFragmentManager().executePendingTransactions(); } } else { Intent intent = new Intent(); intent.setClass(this, DetailsActivity.class); intent.putExtra("index", index); startActivity(intent); } } } //layout/main.xml <?xml version="1.0" encoding="utf-8"?> <!-- This file is res/layout/main.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment class="app.test.TitlesFragment" android:id="@+id/titles" android:layout_width="match_parent" android:layout_height="match_parent" /> android:background="#00550033" /> <FrameLayout android:id="@+id/details" android:layout_weight="2" android:layout_width="0px" android:layout_height="match_parent" /> </LinearLayout> //layout/row.xml <?xml version="1.0" encoding="utf-8"?> <!-- This file is res/layout/details.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ScrollView android:id="@+id/scroller" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="match_parent" /> </ScrollView> </LinearLayout> //animator/bounce_in_down.xml <?xml version="1.0" encoding="utf-8" ?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:interpolator/bounce" android:valueFrom="-1280" android:valueTo="0" android:valueType="floatType" android:propertyName="Y" android:duration="2000" /> //animator/slide_out_down.xml <?xml version="1.0" encoding="utf-8" ?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <objectAnimator android:interpolator="@android:interpolator/accelerate_cubic" android:valueFrom="0" android:valueTo="1280" android:valueType="floatType" android:propertyName="Y" android:duration="2000" /> <objectAnimator android:interpolator="@android:interpolator/accelerate_cubic" android:valueFrom="1" android:valueTo="0" android:valueType="floatType" android:propertyName="Alpha" android:duration="2000" /> </set>
XML for Fade in animation
<?xml version="1.0" encoding="utf-8" ?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:valueFrom="0" android:valueTo="1" android:valueType="floatType" android:propertyName="alpha" android:duration="2000" />
Xml for Fade out animation
<?xml version="1.0" encoding="utf-8" ?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:valueFrom="1" android:valueTo="0" android:valueType="floatType" android:propertyName="alpha" android:duration="500" />
XML for slide out animation
<?xml version="1.0" encoding="utf-8" ?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:interpolator/accelerate_decelerate" android:valueFrom="-1280" android:valueTo="0" android:valueType="floatType" android:propertyName="X" android:duration="2000" />
Animation Cloning
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.widget.Button; import com.example.android.apis.R; import android.animation.*; 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.LinearLayout; import java.util.ArrayList; 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. */ 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) { shape.getPaint().setColor(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.getShape().getWidth(); } public void setWidth(float 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); } public ShapeHolder(ShapeDrawable s) { shape = s; } } public class AnimationCloning extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); 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() { public void onClick(View v) { animView.startAnimation(); } }); } public class MyAnimationView extends View implements ValueAnimator.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); } private void createAnimation() { if (animation == null) { ObjectAnimator anim1 = ObjectAnimator.ofFloat(balls.get(0), "y", 0f, getHeight() - balls.get(0).getHeight()).setDuration(500); ObjectAnimator anim2 = anim1.clone(); anim2.setTarget(balls.get(1)); anim1.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", 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(anim1, 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)(100 + Math.random() * 155); int blue = (int)(100 + Math.random() * 155); 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); 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(); } public void onAnimationUpdate(ValueAnimator animation) { invalidate(); } } } //main.xml <?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="match_parent" android:layout_height="match_parent" android:id="@+id/container" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Run" android:id="@+id/startButton" /> </LinearLayout>
Animation Loading
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.AnimatorInflater; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.graphics.Color; import com.example.android.apis.R; import java.util.ArrayList; import android.animation.Animator; 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 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. */ 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) { shape.getPaint().setColor(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.getShape().getWidth(); } public void setWidth(float 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); } public ShapeHolder(ShapeDrawable s) { shape = s; } } /** * This application demonstrates loading Animator 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() { public void onClick(View v) { animView.startAnimation(); } }); } public class MyAnimationView extends View implements ValueAnimator.AnimatorUpdateListener { private static final float BALL_SIZE = 100f; public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>(); Animator animation = null; public MyAnimationView(Context context) { super(context); addBall(50, 50); addBall(200, 50); addBall(350, 50); addBall(500, 50, Color.GREEN); } private void createAnimation() { Context appContext = AnimationLoading.this; if (animation == null) { ObjectAnimator anim = (ObjectAnimator) AnimatorInflater. loadAnimator(appContext, R.anim.object_animator); anim.addUpdateListener(this); anim.setTarget(balls.get(0)); ValueAnimator fader = (ValueAnimator) AnimatorInflater. loadAnimator(appContext, R.anim.animator); fader.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { balls.get(1).setAlpha((Float) animation.getAnimatedValue()); } }); AnimatorSet seq = (AnimatorSet) AnimatorInflater.loadAnimator(appContext, R.anim.animator_set); seq.setTarget(balls.get(2)); ObjectAnimator colorizer = (ObjectAnimator) AnimatorInflater. loadAnimator(appContext, R.anim.color_animator); colorizer.setTarget(balls.get(3)); animation = new AnimatorSet(); ((AnimatorSet) animation).playTogether(anim, fader, seq, colorizer); } } public void startAnimation() { createAnimation(); animation.start(); } private ShapeHolder createBall(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); return shapeHolder; } private void addBall(float x, float y, int color) { ShapeHolder shapeHolder = createBall(x, y); shapeHolder.setColor(color); balls.add(shapeHolder); } private void addBall(float x, float y) { ShapeHolder shapeHolder = createBall(x, 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 = shapeHolder.getShape().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); balls.add(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(ValueAnimator animation) { invalidate(); ShapeHolder ball = balls.get(0); ball.setY((Float)animation.getAnimatedValue()); } } } //layout/animation_loading.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/container" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Run" android:id="@+id/startButton" /> </LinearLayout> //anim/object_animator.xml <?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:valueTo="200" android:valueType="floatType" android:propertyName="y" android:repeatCount="1" android:repeatMode="reverse"/> //anim/animator.xml <?xml version="1.0" encoding="utf-8"?> <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"/> //anim/animator_set.xml <?xml version="1.0" encoding="utf-8"?> <set> <objectAnimator 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"/> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:valueTo="400" android:valueType="floatType" android:propertyName="y" android:repeatCount="1" android:repeatMode="reverse"/> </set> //anim/color_animator.xml <?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:valueFrom="#0f0" android:valueTo="#00ffff" android:propertyName="color" android:repeatCount="1" android:repeatMode="reverse"/>
Multi Property Animation
package com.example.android.apis.animation; import android.animation.*; import android.view.animation.AccelerateInterpolator; import com.example.android.apis.R; import java.util.ArrayList; 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.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. */ 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) { shape.getPaint().setColor(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.getShape().getWidth(); } public void setWidth(float 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); } public ShapeHolder(ShapeDrawable s) { shape = s; } } public class MultiPropertyAnimation extends Activity { private static final int DURATION = 1500; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); 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() { public void onClick(View v) { animView.startAnimation(); } }); } public class MyAnimationView extends View implements ValueAnimator.AnimatorUpdateListener { private static final float BALL_SIZE = 100f; public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>(); AnimatorSet animation = null; Animator bounceAnim = null; ShapeHolder ball = null; public MyAnimationView(Context context) { super(context); addBall(50, 0); addBall(150, 0); addBall(250, 0); addBall(350, 0); } private void createAnimation() { if (bounceAnim == null) { ShapeHolder ball; ball = balls.get(0); ObjectAnimator yBouncer = ObjectAnimator.ofFloat(ball, "y", ball.getY(), getHeight() - BALL_SIZE).setDuration(DURATION); yBouncer.setInterpolator(new BounceInterpolator()); yBouncer.addUpdateListener(this); ball = balls.get(1); PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", ball.getY(), getHeight() - BALL_SIZE); PropertyValuesHolder pvhAlpha = PropertyValuesHolder.ofFloat("alpha", 1.0f, 0f); ObjectAnimator yAlphaBouncer = ObjectAnimator.ofPropertyValuesHolder(ball, pvhY, pvhAlpha).setDuration(DURATION/2); yAlphaBouncer.setInterpolator(new AccelerateInterpolator()); yAlphaBouncer.setRepeatCount(1); yAlphaBouncer.setRepeatMode(ValueAnimator.REVERSE); ball = balls.get(2); PropertyValuesHolder pvhW = PropertyValuesHolder.ofFloat("width", ball.getWidth(), ball.getWidth() * 2); PropertyValuesHolder pvhH = PropertyValuesHolder.ofFloat("height", ball.getHeight(), ball.getHeight() * 2); PropertyValuesHolder pvTX = PropertyValuesHolder.ofFloat("x", ball.getX(), ball.getX() - BALL_SIZE/2f); PropertyValuesHolder pvTY = PropertyValuesHolder.ofFloat("y", ball.getY(), ball.getY() - BALL_SIZE/2f); ObjectAnimator whxyBouncer = ObjectAnimator.ofPropertyValuesHolder(ball, pvhW, pvhH, pvTX, pvTY).setDuration(DURATION/2); whxyBouncer.setRepeatCount(1); whxyBouncer.setRepeatMode(ValueAnimator.REVERSE); ball = balls.get(3); pvhY = PropertyValuesHolder.ofFloat("y", ball.getY(), getHeight() - BALL_SIZE); float ballX = ball.getX(); Keyframe kf0 = Keyframe.ofFloat(0f, ballX); Keyframe kf1 = Keyframe.ofFloat(.5f, ballX + 100f); Keyframe kf2 = Keyframe.ofFloat(1f, ballX + 50f); PropertyValuesHolder pvhX = PropertyValuesHolder.ofKeyframe("x", kf0, kf1, kf2); ObjectAnimator yxBouncer = ObjectAnimator.ofPropertyValuesHolder(ball, pvhY, pvhX).setDuration(DURATION/2); yxBouncer.setRepeatCount(1); yxBouncer.setRepeatMode(ValueAnimator.REVERSE); bounceAnim = new AnimatorSet(); ((AnimatorSet)bounceAnim).playTogether(yBouncer, yAlphaBouncer, whxyBouncer, yxBouncer); } } public void startAnimation() { createAnimation(); bounceAnim.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(ValueAnimator animation) { invalidate(); } } } //main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/container" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Run" android:id="@+id/startButton" /> </LinearLayout>
Animation: push up in,push up out,push left in,push left out,fade in,fade out,hyperspace in,hyperspace out
package app.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.ViewFlipper; public class Test extends Activity implements AdapterView.OnItemSelectedListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mFlipper = ((ViewFlipper) this.findViewById(R.id.flipper)); mFlipper.startFlipping(); Spinner s = (Spinner) findViewById(R.id.spinner); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mStrings); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); s.setAdapter(adapter); s.setOnItemSelectedListener(this); } public void onItemSelected(AdapterView<?> parent, View v, int position, long id) { switch (position) { case 0: mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_up_in)); mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_up_out)); break; case 1: mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in)); mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out)); break; case 2: mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); break; default: mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.hyperspace_in)); mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.hyperspace_out)); break; } } public void onNothingSelected(AdapterView<?> parent) { } private String[] mStrings = { "Push up", "Push left", "Cross fade", "Hyperspace"}; private ViewFlipper mFlipper; } //layout/main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="10dip" android:layout_width="match_parent" android:layout_height="wrap_content"> <ViewFlipper android:id="@+id/flipper" android:layout_width="match_parent" android:layout_height="wrap_content" android:flipInterval="2000" android:layout_marginBottom="20dip" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textSize="26sp" android:text="animation_2_text_1"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textSize="26sp" android:text="animation_2_text_2"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textSize="26sp" android:text="animation_2_text_3"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textSize="26sp" android:text="animation_2_text_4"/> </ViewFlipper> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dip" android:text="instructions" /> <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> //animator/push_up_in.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="300"/> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" /> </set> //animator/push_up_out.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="300"/> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" /> </set> //animator/push_left_in.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="300"/> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" /> </set> //animator/push_left_out.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="0" android:toYDelta="-100%p" android:duration="300"/> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" /> </set> //animator/fade_in.xml <?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="50" /> //animator/fade_out.xml <?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="50" /> //animator/hyperspace_in.xml <?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" android:startOffset="1200" /> //animator/hyperspace_out.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="1.0" android:toXScale="1.4" android:fromYScale="1.0" android:toYScale="0.6" android:pivotX="50%" android:pivotY="50%" android:fillAfter="false" android:duration="700" /> <set android:interpolator="@android:anim/accelerate_interpolator" android:startOffset="700"> <scale android:fromXScale="1.4" android:toXScale="0.0" android:fromYScale="0.6" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:duration="400" /> <rotate android:fromDegrees="0" android:toDegrees="-45" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:duration="400" /> </set> </set>
Using AlphaAnimation class to do animation in code
package com.example.android.apis.view; import android.app.ListActivity; import android.os.Bundle; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.LayoutAnimationController; import android.view.animation.TranslateAnimation; import android.widget.ArrayAdapter; import android.widget.ListView; public class LayoutAnimation2 extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings)); AnimationSet set = new AnimationSet(true); Animation animation = new AlphaAnimation(0.0f, 1.0f); animation.setDuration(50); set.addAnimation(animation); animation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f ); animation.setDuration(100); set.addAnimation(animation); LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f); ListView listView = getListView(); listView.setLayoutAnimation(controller); } private String[] mStrings = { "Bordeaux", "Lyon", "Marseille", "Nancy", "Paris", "Toulouse", "Strasbourg" }; }
Animation Interpolator
//package org.ametro.util; import android.graphics.PointF; public class AnimationInterpolator { public final static int SCALE = 1; public final static int SCROLL = 2; public final static int SCALE_AND_SCROLL = 3; private PointF mStartPoint = new PointF(); private PointF mEndPoint = new PointF(); private PointF mNowPoint = new PointF(); private float mStartScale; private float mEndScale; private long mPeriod; private long mEndTime; private long mNowTime; private int mMode; public void begin(PointF startPoint, PointF endPoint, Float startScale, Float endScale, long time){ this.mNowTime = System.currentTimeMillis(); this.mEndTime = mNowTime + time; this.mPeriod = time; this.mMode = 0; if(startScale!=null && endScale!=null){ this.mStartScale = startScale; this.mEndScale = endScale; this.mMode |= SCALE; } if(startPoint!=null && endPoint!=null){ this.mStartPoint.set(startPoint); this.mEndPoint.set(endPoint); this.mNowPoint.set(startPoint); this.mMode |= SCROLL; } } public void begin(float startScale, float endScale, long time){ begin(null,null,startScale, endScale, time); } public void begin(PointF startPoint, PointF endPoint, long time){ begin(startPoint, endPoint, null, null, time); } public boolean more(){ return mNowTime < mEndTime; } public void next(){ mNowTime = System.currentTimeMillis(); } public PointF getPoint(){ if(mNowTime < mEndTime){ float k = getProgress(); float x = mEndPoint.x - k * (mEndPoint.x - mStartPoint.x); float y = mEndPoint.y - k * (mEndPoint.y - mStartPoint.y); mNowPoint.set(x, y); return mNowPoint; }else{ return mEndPoint; } } public float getScale(){ if(mNowTime < mEndTime){ float k = getProgress(); return (mEndScale - k * (mEndScale - mStartScale)); }else{ return mEndScale; } } private float getProgress() { return (float)(mEndTime - mNowTime) / mPeriod; } public boolean hasScale() { return (mMode & SCALE)!=0; } public boolean hasScroll() { return (mMode & SCROLL)!=0; } }
Using TranslateAnimation class
//package br.com.tecnove.random.android.util; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Random; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.LayoutAnimationController; import android.view.animation.TranslateAnimation; import android.widget.ScrollView; import android.widget.TextView; /** * @author "Carlos Eduardo Ruhrwiem" * */ public class AnimationUtil { private final static int RUNNING_VALUES = 50; private final static String[] SPINNING = new String[] { "-", "/", "|", "\\" }; private final static Animation anim_TL = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f); private final static Animation anim_T = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f); private final static Animation anim_TR = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f); private final static Animation anim_R = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); private final static Animation anim_BR = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f); private final static Animation anim_B = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f); private final static Animation anim_BL = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f); private final static Animation anim_L = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); private final static Animation[] animations = new Animation[]{anim_TL, anim_T,anim_TR, anim_R, anim_BR, anim_B, anim_BL, anim_L}; public static String[] getValuesForAnimation(int min, int max) { String[] returnValues = new String[RUNNING_VALUES]; for (int i = 0; i < RUNNING_VALUES; i++) { returnValues[i] = String.valueOf(RandomGenerator.generateFromRange(min, max)); } return returnValues; } public static String[] generateAnimationString(String[] inputArray) { String[] returnValue = new String[inputArray.length]; int animationIndex = 0; for (int j = 0; j < inputArray.length; j++) { returnValue[j] = SPINNING[animationIndex] + " " + inputArray[j] + " " + SPINNING[animationIndex]; animationIndex++; } return returnValue; } public static Animation getRandomDirectionAnimation(){ return animations[RandomGenerator.generateFromRange(0, 7)]; } /** * This should be improved. But it works right now. * @param values * @param outView * @param animatedText */ public static void animateTextView(String result, ScrollView outView, TextView animatedText) { animatedText.setText(result); AnimationSet set = new AnimationSet(true); Animation animation = new AlphaAnimation(0.0f, 1.0f); animation.setDuration(500); set.addAnimation(animation); animation = AnimationUtil.getRandomDirectionAnimation(); animation.setDuration(1000); set.addAnimation(animation); LayoutAnimationController controller = new LayoutAnimationController(set, 0.25f); outView.setLayoutAnimation(controller); } } class RandomGenerator { private static final Random r = new Random(); /** * Random a value from a specified range * * @param min * minimun range (inclusive) * @param max * maximun range (inclusive) * @return randomed int value */ public static int generateFromRange(int min, int max) { // swap values if they come in the wrong order if (min > max) { int aux = min; min = max; max = aux; } int i = max - min; int x = r.nextInt(i + 1); return x + min; } /** * Random a value from a string vector * * @param input * String[] containing all the possible values * @return randomed String value */ public static String generateFromStringArray(String[] input) { int i = generateFromRange(0, input.length - 1); return input[i]; } /** * Random a value from a string collection. Just a facilitator for the generateFromStringArray * * @param input * Collection of String that contains the possible values * @return randomed String value */ public static String generateFromStringCollection(Collection<String> input) { return generateFromStringArray((String[]) input.toArray()); } /** * Generate a random upercase char * * @return randomed char */ public static char generateLetter() { return (char) generateFromRange((int) 'A', (int) 'Z'); } /** * Generate random numbers for a lottery * @param maxRange limit the range from 1 to this number (included) * @param numbersToGenerate how many numbers will be generated * @return List of Integers with the non repeating values */ public static List<Integer> generateLottery(Integer maxRange, Integer numbersToGenerate) { List<Integer> numbersGenerated = new ArrayList<Integer>(); for (int c = 0; c < numbersToGenerate; c++) { int nGenerated = 0; do { nGenerated = r.nextInt(maxRange); // adding +1 to make the int start at 1 and the maxRange be included in the sorted values nGenerated++; // if the number is allready on the list, sort a new number } while (numbersGenerated.contains(nGenerated)); numbersGenerated.add(nGenerated); } return numbersGenerated; } /** * Random a value from a string with values separated by '/' * @param valor * @return */ public static String generateFromString(String valor) { // the input must be like: curitiba/fortaleza/google/facebook/android/motorola String[] vetor = valor.split("/"); String outValue = ""; if (vetor.length>0){ outValue = vetor[r.nextInt(vetor.length)]; } return outValue; } }
Demonstrates the seeking capability of ValueAnimator.
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.AnimatorListenerAdapter; import android.animation.Animator; import android.animation.ObjectAnimator; import android.view.animation.AccelerateInterpolator; import android.view.animation.DecelerateInterpolator; import android.view.animation.Interpolator; import android.widget.ArrayAdapter; import android.widget.ListView; import com.example.android.apis.R; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.SeekBar; /** * This application demonstrates the seeking capability of ValueAnimator. 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 ListFlipper extends Activity { private static final int DURATION = 1500; private SeekBar mSeekBar; private static final String[] LIST_STRINGS_EN = new String[] { "One", "Two", "Three", "Four", "Five", "Six" }; private static final String[] LIST_STRINGS_FR = new String[] { "Un", "Deux", "Trois", "Quatre", "Le Five", "Six" }; ListView mEnglishList; ListView mFrenchList; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //FrameLayout container = (LinearLayout) findViewById(R.id.container); mEnglishList = (ListView) findViewById(R.id.list_en); mFrenchList = (ListView) findViewById(R.id.list_fr); // Prepare the ListView final ArrayAdapter<String> adapterEn = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, LIST_STRINGS_EN); // Prepare the ListView final ArrayAdapter<String> adapterFr = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, LIST_STRINGS_FR); mEnglishList.setAdapter(adapterEn); mFrenchList.setAdapter(adapterFr); mFrenchList.setRotationY(-90f); Button starter = (Button) findViewById(R.id.button); starter.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { flipit(); } }); } private Interpolator accelerator = new AccelerateInterpolator(); private Interpolator decelerator = new DecelerateInterpolator(); private void flipit() { final ListView visibleList; final ListView invisibleList; if (mEnglishList.getVisibility() == View.GONE) { visibleList = mFrenchList; invisibleList = mEnglishList; } else { invisibleList = mFrenchList; visibleList = mEnglishList; } ObjectAnimator visToInvis = ObjectAnimator.ofFloat(visibleList, "rotationY", 0f, 90f); visToInvis.setDuration(500); visToInvis.setInterpolator(accelerator); final ObjectAnimator invisToVis = ObjectAnimator.ofFloat(invisibleList, "rotationY", -90f, 0f); invisToVis.setDuration(500); invisToVis.setInterpolator(decelerator); visToInvis.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator anim) { visibleList.setVisibility(View.GONE); invisToVis.start(); invisibleList.setVisibility(View.VISIBLE); } }); visToInvis.start(); } } //main.xml <?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="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button" android:text="Flip" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ListView android:id="@+id/list_en" android:layout_width="match_parent" android:layout_weight="1.0" android:layout_height="0dip"/> <ListView android:id="@+id/list_fr" android:layout_width="match_parent" android:layout_weight="1.0" android:layout_height="0dip" android:visibility="gone"/> </LinearLayout>
Animating by calling invalidate() from draw(),loading and drawing resources, handling onPause() in an animation
package app.test; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.TextView; /** * View that draws, takes keystrokes, etc. for a simple LunarLander game. * * Has a mode which RUNNING, PAUSED, etc. Has a x, y, dx, dy, ... capturing the * current ship physics. All x/y etc. are measured with (0,0) at the lower left. * updatePhysics() advances the physics based on realtime. draw() renders the * ship, and does an invalidate() to prompt another draw() as soon as possible * by the system. */ class LunarView extends SurfaceView implements SurfaceHolder.Callback { class LunarThread extends Thread { /* * Difficulty setting constants */ public static final int DIFFICULTY_EASY = 0; public static final int DIFFICULTY_HARD = 1; public static final int DIFFICULTY_MEDIUM = 2; /* * Physics constants */ public static final int PHYS_DOWN_ACCEL_SEC = 35; public static final int PHYS_FIRE_ACCEL_SEC = 80; public static final int PHYS_FUEL_INIT = 60; public static final int PHYS_FUEL_MAX = 100; public static final int PHYS_FUEL_SEC = 10; public static final int PHYS_SLEW_SEC = 120; // degrees/second rotate public static final int PHYS_SPEED_HYPERSPACE = 180; public static final int PHYS_SPEED_INIT = 30; public static final int PHYS_SPEED_MAX = 120; /* * State-tracking constants */ public static final int STATE_LOSE = 1; public static final int STATE_PAUSE = 2; public static final int STATE_READY = 3; public static final int STATE_RUNNING = 4; public static final int STATE_WIN = 5; /* * Goal condition constants */ public static final int TARGET_ANGLE = 18; // > this angle means crash public static final int TARGET_BOTTOM_PADDING = 17; // px below gear public static final int TARGET_PAD_HEIGHT = 8; // how high above ground public static final int TARGET_SPEED = 28; // > this speed means crash public static final double TARGET_WIDTH = 1.6; // width of target /* * UI constants (i.e. the speed & fuel bars) */ public static final int UI_BAR = 100; // width of the bar(s) public static final int UI_BAR_HEIGHT = 10; // height of the bar(s) private static final String KEY_DIFFICULTY = "mDifficulty"; private static final String KEY_DX = "mDX"; private static final String KEY_DY = "mDY"; private static final String KEY_FUEL = "mFuel"; private static final String KEY_GOAL_ANGLE = "mGoalAngle"; private static final String KEY_GOAL_SPEED = "mGoalSpeed"; private static final String KEY_GOAL_WIDTH = "mGoalWidth"; private static final String KEY_GOAL_X = "mGoalX"; private static final String KEY_HEADING = "mHeading"; private static final String KEY_LANDER_HEIGHT = "mLanderHeight"; private static final String KEY_LANDER_WIDTH = "mLanderWidth"; private static final String KEY_WINS = "mWinsInARow"; private static final String KEY_X = "mX"; private static final String KEY_Y = "mY"; /* * Member (state) fields */ /** The drawable to use as the background of the animation canvas */ private Bitmap mBackgroundImage; /** * Current height of the surface/canvas. * * @see #setSurfaceSize */ private int mCanvasHeight = 1; /** * Current width of the surface/canvas. * * @see #setSurfaceSize */ private int mCanvasWidth = 1; /** What to draw for the Lander when it has crashed */ private Drawable mCrashedImage; /** * Current difficulty -- amount of fuel, allowed angle, etc. Default is * MEDIUM. */ private int mDifficulty; /** Velocity dx. */ private double mDX; /** Velocity dy. */ private double mDY; /** Is the engine burning? */ private boolean mEngineFiring; /** What to draw for the Lander when the engine is firing */ private Drawable mFiringImage; /** Fuel remaining */ private double mFuel; /** Allowed angle. */ private int mGoalAngle; /** Allowed speed. */ private int mGoalSpeed; /** Width of the landing pad. */ private int mGoalWidth; /** X of the landing pad. */ private int mGoalX; /** Message handler used by thread to interact with TextView */ private Handler mHandler; /** * Lander heading in degrees, with 0 up, 90 right. Kept in the range * 0..360. */ private double mHeading; /** Pixel height of lander image. */ private int mLanderHeight; /** What to draw for the Lander in its normal state */ private Drawable mLanderImage; /** Pixel width of lander image. */ private int mLanderWidth; /** Used to figure out elapsed time between frames */ private long mLastTime; /** Paint to draw the lines on screen. */ private Paint mLinePaint; /** "Bad" speed-too-high variant of the line color. */ private Paint mLinePaintBad; /** The state of the game. One of READY, RUNNING, PAUSE, LOSE, or WIN */ private int mMode; /** Currently rotating, -1 left, 0 none, 1 right. */ private int mRotating; /** Indicate whether the surface has been created & is ready to draw */ private boolean mRun = false; /** Scratch rect object. */ private RectF mScratchRect; /** Handle to the surface manager object we interact with */ private SurfaceHolder mSurfaceHolder; /** Number of wins in a row. */ private int mWinsInARow; /** X of lander center. */ private double mX; /** Y of lander center. */ private double mY; public LunarThread(SurfaceHolder surfaceHolder, Context context, Handler handler) { // get handles to some important objects mSurfaceHolder = surfaceHolder; mHandler = handler; mContext = context; Resources res = context.getResources(); // cache handles to our key sprites & other drawables mLanderImage = context.getResources().getDrawable( R.drawable.lander_plain); mFiringImage = context.getResources().getDrawable( R.drawable.lander_firing); mCrashedImage = context.getResources().getDrawable( R.drawable.lander_crashed); // load background image as a Bitmap instead of a Drawable b/c // we don't need to transform it and it's faster to draw this way mBackgroundImage = BitmapFactory.decodeResource(res, R.drawable.earthrise); // Use the regular lander image as the model size for all sprites mLanderWidth = mLanderImage.getIntrinsicWidth(); mLanderHeight = mLanderImage.getIntrinsicHeight(); // Initialize paints for speedometer mLinePaint = new Paint(); mLinePaint.setAntiAlias(true); mLinePaint.setARGB(255, 0, 255, 0); mLinePaintBad = new Paint(); mLinePaintBad.setAntiAlias(true); mLinePaintBad.setARGB(255, 120, 180, 0); mScratchRect = new RectF(0, 0, 0, 0); mWinsInARow = 0; mDifficulty = DIFFICULTY_MEDIUM; // initial show-up of lander (not yet playing) mX = mLanderWidth; mY = mLanderHeight * 2; mFuel = PHYS_FUEL_INIT; mDX = 0; mDY = 0; mHeading = 0; mEngineFiring = true; } /** * Starts the game, setting parameters for the current difficulty. */ public void doStart() { synchronized (mSurfaceHolder) { // First set the game for Medium difficulty mFuel = PHYS_FUEL_INIT; mEngineFiring = false; mGoalWidth = (int) (mLanderWidth * TARGET_WIDTH); mGoalSpeed = TARGET_SPEED; mGoalAngle = TARGET_ANGLE; int speedInit = PHYS_SPEED_INIT; // Adjust difficulty params for EASY/HARD if (mDifficulty == DIFFICULTY_EASY) { mFuel = mFuel * 3 / 2; mGoalWidth = mGoalWidth * 4 / 3; mGoalSpeed = mGoalSpeed * 3 / 2; mGoalAngle = mGoalAngle * 4 / 3; speedInit = speedInit * 3 / 4; } else if (mDifficulty == DIFFICULTY_HARD) { mFuel = mFuel * 7 / 8; mGoalWidth = mGoalWidth * 3 / 4; mGoalSpeed = mGoalSpeed * 7 / 8; speedInit = speedInit * 4 / 3; } // pick a convenient initial location for the lander sprite mX = mCanvasWidth / 2; mY = mCanvasHeight - mLanderHeight / 2; // start with a little random motion mDY = Math.random() * -speedInit; mDX = Math.random() * 2 * speedInit - speedInit; mHeading = 0; // Figure initial spot for landing, not too near center while (true) { mGoalX = (int) (Math.random() * (mCanvasWidth - mGoalWidth)); if (Math.abs(mGoalX - (mX - mLanderWidth / 2)) > mCanvasHeight / 6) break; } mLastTime = System.currentTimeMillis() + 100; setState(STATE_RUNNING); } } /** * Pauses the physics update & animation. */ public void pause() { synchronized (mSurfaceHolder) { if (mMode == STATE_RUNNING) setState(STATE_PAUSE); } } /** * Restores game state from the indicated Bundle. Typically called when * the Activity is being restored after having been previously * destroyed. * * @param savedState Bundle containing the game state */ public synchronized void restoreState(Bundle savedState) { synchronized (mSurfaceHolder) { setState(STATE_PAUSE); mRotating = 0; mEngineFiring = false; mDifficulty = savedState.getInt(KEY_DIFFICULTY); mX = savedState.getDouble(KEY_X); mY = savedState.getDouble(KEY_Y); mDX = savedState.getDouble(KEY_DX); mDY = savedState.getDouble(KEY_DY); mHeading = savedState.getDouble(KEY_HEADING); mLanderWidth = savedState.getInt(KEY_LANDER_WIDTH); mLanderHeight = savedState.getInt(KEY_LANDER_HEIGHT); mGoalX = savedState.getInt(KEY_GOAL_X); mGoalSpeed = savedState.getInt(KEY_GOAL_SPEED); mGoalAngle = savedState.getInt(KEY_GOAL_ANGLE); mGoalWidth = savedState.getInt(KEY_GOAL_WIDTH); mWinsInARow = savedState.getInt(KEY_WINS); mFuel = savedState.getDouble(KEY_FUEL); } } @Override public void run() { while (mRun) { Canvas c = null; try { c = mSurfaceHolder.lockCanvas(null); synchronized (mSurfaceHolder) { if (mMode == STATE_RUNNING) updatePhysics(); doDraw(c); } } finally { // do this in a finally so that if an exception is thrown // during the above, we don't leave the Surface in an // inconsistent state if (c != null) { mSurfaceHolder.unlockCanvasAndPost(c); } } } } /** * Dump game state to the provided Bundle. Typically called when the * Activity is being suspended. * * @return Bundle with this view's state */ public Bundle saveState(Bundle map) { synchronized (mSurfaceHolder) { if (map != null) { map.putInt(KEY_DIFFICULTY, Integer.valueOf(mDifficulty)); map.putDouble(KEY_X, Double.valueOf(mX)); map.putDouble(KEY_Y, Double.valueOf(mY)); map.putDouble(KEY_DX, Double.valueOf(mDX)); map.putDouble(KEY_DY, Double.valueOf(mDY)); map.putDouble(KEY_HEADING, Double.valueOf(mHeading)); map.putInt(KEY_LANDER_WIDTH, Integer.valueOf(mLanderWidth)); map.putInt(KEY_LANDER_HEIGHT, Integer .valueOf(mLanderHeight)); map.putInt(KEY_GOAL_X, Integer.valueOf(mGoalX)); map.putInt(KEY_GOAL_SPEED, Integer.valueOf(mGoalSpeed)); map.putInt(KEY_GOAL_ANGLE, Integer.valueOf(mGoalAngle)); map.putInt(KEY_GOAL_WIDTH, Integer.valueOf(mGoalWidth)); map.putInt(KEY_WINS, Integer.valueOf(mWinsInARow)); map.putDouble(KEY_FUEL, Double.valueOf(mFuel)); } } return map; } /** * Sets the current difficulty. * * @param difficulty */ public void setDifficulty(int difficulty) { synchronized (mSurfaceHolder) { mDifficulty = difficulty; } } /** * Sets if the engine is currently firing. */ public void setFiring(boolean firing) { synchronized (mSurfaceHolder) { mEngineFiring = firing; } } /** * Used to signal the thread whether it should be running or not. * Passing true allows the thread to run; passing false will shut it * down if it's already running. Calling start() after this was most * recently called with false will result in an immediate shutdown. * * @param b true to run, false to shut down */ public void setRunning(boolean b) { mRun = b; } /** * Sets the game mode. That is, whether we are running, paused, in the * failure state, in the victory state, etc. * * @see #setState(int, CharSequence) * @param mode one of the STATE_* constants */ public void setState(int mode) { synchronized (mSurfaceHolder) { setState(mode, null); } } /** * Sets the game mode. That is, whether we are running, paused, in the * failure state, in the victory state, etc. * * @param mode one of the STATE_* constants * @param message string to add to screen or null */ public void setState(int mode, CharSequence message) { /* * This method optionally can cause a text message to be displayed * to the user when the mode changes. Since the View that actually * renders that text is part of the main View hierarchy and not * owned by this thread, we can't touch the state of that View. * Instead we use a Message + Handler to relay commands to the main * thread, which updates the user-text View. */ synchronized (mSurfaceHolder) { mMode = mode; if (mMode == STATE_RUNNING) { Message msg = mHandler.obtainMessage(); Bundle b = new Bundle(); b.putString("text", ""); b.putInt("viz", View.INVISIBLE); msg.setData(b); mHandler.sendMessage(msg); } else { mRotating = 0; mEngineFiring = false; Resources res = mContext.getResources(); CharSequence str = ""; if (mMode == STATE_READY) str = res.getText(R.string.mode_ready); else if (mMode == STATE_PAUSE) str = res.getText(R.string.mode_pause); else if (mMode == STATE_LOSE) str = res.getText(R.string.mode_lose); else if (mMode == STATE_WIN) str = res.getString(R.string.mode_win_prefix) + mWinsInARow + " " + res.getString(R.string.mode_win_suffix); if (message != null) { str = message + "\n" + str; } if (mMode == STATE_LOSE) mWinsInARow = 0; Message msg = mHandler.obtainMessage(); Bundle b = new Bundle(); b.putString("text", str.toString()); b.putInt("viz", View.VISIBLE); msg.setData(b); mHandler.sendMessage(msg); } } } /* Callback invoked when the surface dimensions change. */ public void setSurfaceSize(int width, int height) { // synchronized to make sure these all change atomically synchronized (mSurfaceHolder) { mCanvasWidth = width; mCanvasHeight = height; // don't forget to resize the background image mBackgroundImage = Bitmap.createScaledBitmap( mBackgroundImage, width, height, true); } } /** * Resumes from a pause. */ public void unpause() { // Move the real time clock up to now synchronized (mSurfaceHolder) { mLastTime = System.currentTimeMillis() + 100; } setState(STATE_RUNNING); } /** * Handles a key-down event. * * @param keyCode the key that was pressed * @param msg the original event object * @return true */ boolean doKeyDown(int keyCode, KeyEvent msg) { synchronized (mSurfaceHolder) { boolean okStart = false; if (keyCode == KeyEvent.KEYCODE_DPAD_UP) okStart = true; if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) okStart = true; if (keyCode == KeyEvent.KEYCODE_S) okStart = true; if (okStart && (mMode == STATE_READY || mMode == STATE_LOSE || mMode == STATE_WIN)) { // ready-to-start -> start doStart(); return true; } else if (mMode == STATE_PAUSE && okStart) { // paused -> running unpause(); return true; } else if (mMode == STATE_RUNNING) { // center/space -> fire if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_SPACE) { setFiring(true); return true; // left/q -> left } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_Q) { mRotating = -1; return true; // right/w -> right } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT || keyCode == KeyEvent.KEYCODE_W) { mRotating = 1; return true; // up -> pause } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) { pause(); return true; } } return false; } } /** * Handles a key-up event. * * @param keyCode the key that was pressed * @param msg the original event object * @return true if the key was handled and consumed, or else false */ boolean doKeyUp(int keyCode, KeyEvent msg) { boolean handled = false; synchronized (mSurfaceHolder) { if (mMode == STATE_RUNNING) { if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_SPACE) { setFiring(false); handled = true; } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_Q || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT || keyCode == KeyEvent.KEYCODE_W) { mRotating = 0; handled = true; } } } return handled; } /** * Draws the ship, fuel/speed bars, and background to the provided * Canvas. */ private void doDraw(Canvas canvas) { // Draw the background image. Operations on the Canvas accumulate // so this is like clearing the screen. canvas.drawBitmap(mBackgroundImage, 0, 0, null); int yTop = mCanvasHeight - ((int) mY + mLanderHeight / 2); int xLeft = (int) mX - mLanderWidth / 2; // Draw the fuel gauge int fuelWidth = (int) (UI_BAR * mFuel / PHYS_FUEL_MAX); mScratchRect.set(4, 4, 4 + fuelWidth, 4 + UI_BAR_HEIGHT); canvas.drawRect(mScratchRect, mLinePaint); // Draw the speed gauge, with a two-tone effect double speed = Math.sqrt(mDX * mDX + mDY * mDY); int speedWidth = (int) (UI_BAR * speed / PHYS_SPEED_MAX); if (speed <= mGoalSpeed) { mScratchRect.set(4 + UI_BAR + 4, 4, 4 + UI_BAR + 4 + speedWidth, 4 + UI_BAR_HEIGHT); canvas.drawRect(mScratchRect, mLinePaint); } else { // Draw the bad color in back, with the good color in front of // it mScratchRect.set(4 + UI_BAR + 4, 4, 4 + UI_BAR + 4 + speedWidth, 4 + UI_BAR_HEIGHT); canvas.drawRect(mScratchRect, mLinePaintBad); int goalWidth = (UI_BAR * mGoalSpeed / PHYS_SPEED_MAX); mScratchRect.set(4 + UI_BAR + 4, 4, 4 + UI_BAR + 4 + goalWidth, 4 + UI_BAR_HEIGHT); canvas.drawRect(mScratchRect, mLinePaint); } // Draw the landing pad canvas.drawLine(mGoalX, 1 + mCanvasHeight - TARGET_PAD_HEIGHT, mGoalX + mGoalWidth, 1 + mCanvasHeight - TARGET_PAD_HEIGHT, mLinePaint); // Draw the ship with its current rotation canvas.save(); canvas.rotate((float) mHeading, (float) mX, mCanvasHeight - (float) mY); if (mMode == STATE_LOSE) { mCrashedImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop + mLanderHeight); mCrashedImage.draw(canvas); } else if (mEngineFiring) { mFiringImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop + mLanderHeight); mFiringImage.draw(canvas); } else { mLanderImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop + mLanderHeight); mLanderImage.draw(canvas); } canvas.restore(); } /** * Figures the lander state (x, y, fuel, ...) based on the passage of * realtime. Does not invalidate(). Called at the start of draw(). * Detects the end-of-game and sets the UI to the next state. */ private void updatePhysics() { long now = System.currentTimeMillis(); // Do nothing if mLastTime is in the future. // This allows the game-start to delay the start of the physics // by 100ms or whatever. if (mLastTime > now) return; double elapsed = (now - mLastTime) / 1000.0; // mRotating -- update heading if (mRotating != 0) { mHeading += mRotating * (PHYS_SLEW_SEC * elapsed); // Bring things back into the range 0..360 if (mHeading < 0) mHeading += 360; else if (mHeading >= 360) mHeading -= 360; } // Base accelerations -- 0 for x, gravity for y double ddx = 0.0; double ddy = -PHYS_DOWN_ACCEL_SEC * elapsed; if (mEngineFiring) { // taking 0 as up, 90 as to the right // cos(deg) is ddy component, sin(deg) is ddx component double elapsedFiring = elapsed; double fuelUsed = elapsedFiring * PHYS_FUEL_SEC; // tricky case where we run out of fuel partway through the // elapsed if (fuelUsed > mFuel) { elapsedFiring = mFuel / fuelUsed * elapsed; fuelUsed = mFuel; // Oddball case where we adjust the "control" from here mEngineFiring = false; } mFuel -= fuelUsed; // have this much acceleration from the engine double accel = PHYS_FIRE_ACCEL_SEC * elapsedFiring; double radians = 2 * Math.PI * mHeading / 360; ddx = Math.sin(radians) * accel; ddy += Math.cos(radians) * accel; } double dxOld = mDX; double dyOld = mDY; // figure speeds for the end of the period mDX += ddx; mDY += ddy; // figure position based on average speed during the period mX += elapsed * (mDX + dxOld) / 2; mY += elapsed * (mDY + dyOld) / 2; mLastTime = now; // Evaluate if we have landed ... stop the game double yLowerBound = TARGET_PAD_HEIGHT + mLanderHeight / 2 - TARGET_BOTTOM_PADDING; if (mY <= yLowerBound) { mY = yLowerBound; int result = STATE_LOSE; CharSequence message = ""; Resources res = mContext.getResources(); double speed = Math.sqrt(mDX * mDX + mDY * mDY); boolean onGoal = (mGoalX <= mX - mLanderWidth / 2 && mX + mLanderWidth / 2 <= mGoalX + mGoalWidth); // "Hyperspace" win -- upside down, going fast, // puts you back at the top. if (onGoal && Math.abs(mHeading - 180) < mGoalAngle && speed > PHYS_SPEED_HYPERSPACE) { result = STATE_WIN; mWinsInARow++; doStart(); return; // Oddball case: this case does a return, all other cases // fall through to setMode() below. } else if (!onGoal) { message = res.getText(R.string.message_off_pad); } else if (!(mHeading <= mGoalAngle || mHeading >= 360 - mGoalAngle)) { message = res.getText(R.string.message_bad_angle); } else if (speed > mGoalSpeed) { message = res.getText(R.string.message_too_fast); } else { result = STATE_WIN; mWinsInARow++; } setState(result, message); } } } /** Handle to the application context, used to e.g. fetch Drawables. */ private Context mContext; /** Pointer to the text view to display "Paused.." etc. */ private TextView mStatusText; /** The thread that actually draws the animation */ private LunarThread thread; public LunarView(Context context, AttributeSet attrs) { super(context, attrs); // register our interest in hearing about changes to our surface SurfaceHolder holder = getHolder(); holder.addCallback(this); // create thread only; it's started in surfaceCreated() thread = new LunarThread(holder, context, new Handler() { @Override public void handleMessage(Message m) { mStatusText.setVisibility(m.getData().getInt("viz")); mStatusText.setText(m.getData().getString("text")); } }); setFocusable(true); // make sure we get key events } /** * Fetches the animation thread corresponding to this LunarView. * * @return the animation thread */ public LunarThread getThread() { return thread; } /** * Standard override to get key-press events. */ @Override public boolean onKeyDown(int keyCode, KeyEvent msg) { return thread.doKeyDown(keyCode, msg); } /** * Standard override for key-up. We actually care about these, so we can * turn off the engine or stop rotating. */ @Override public boolean onKeyUp(int keyCode, KeyEvent msg) { return thread.doKeyUp(keyCode, msg); } /** * Standard window-focus override. Notice focus lost so we can pause on * focus lost. e.g. user switches to take a call. */ @Override public void onWindowFocusChanged(boolean hasWindowFocus) { if (!hasWindowFocus) thread.pause(); } /** * Installs a pointer to the text view used for messages. */ public void setTextView(TextView textView) { mStatusText = textView; } /* Callback invoked when the surface dimensions change. */ public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { thread.setSurfaceSize(width, height); } /* * Callback invoked when the Surface has been created and is ready to be * used. */ public void surfaceCreated(SurfaceHolder holder) { // start the thread here so that we don't busy-wait in run() // waiting for the surface to be created thread.setRunning(true); thread.start(); } /* * Callback invoked when the Surface has been destroyed and must no longer * be touched. WARNING: after this method returns, the Surface/Canvas must * never be touched again! */ public void surfaceDestroyed(SurfaceHolder holder) { // we have to tell thread to shut down & wait for it to finish, or else // it might touch the Surface after we return and explode boolean retry = true; thread.setRunning(false); while (retry) { try { thread.join(); retry = false; } catch (InterruptedException e) { } } } } /** * This is a simple LunarLander activity that houses a single LunarView. It * demonstrates... * <ul> * <li>animating by calling invalidate() from draw() * <li>loading and drawing resources * <li>handling onPause() in an animation * </ul> */ public class LunarLander extends Activity { private static final int MENU_EASY = 1; private static final int MENU_HARD = 2; private static final int MENU_MEDIUM = 3; private static final int MENU_PAUSE = 4; private static final int MENU_RESUME = 5; private static final int MENU_START = 6; private static final int MENU_STOP = 7; /** A handle to the thread that's actually running the animation. */ private LunarThread mLunarThread; /** A handle to the View in which the game is running. */ private LunarView mLunarView; /** * Invoked during init to give the Activity a chance to set up its Menu. * * @param menu the Menu to which entries may be added * @return true */ @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, MENU_START, 0, R.string.menu_start); menu.add(0, MENU_STOP, 0, R.string.menu_stop); menu.add(0, MENU_PAUSE, 0, R.string.menu_pause); menu.add(0, MENU_RESUME, 0, R.string.menu_resume); menu.add(0, MENU_EASY, 0, R.string.menu_easy); menu.add(0, MENU_MEDIUM, 0, R.string.menu_medium); menu.add(0, MENU_HARD, 0, R.string.menu_hard); return true; } /** * Invoked when the user selects an item from the Menu. * * @param item the Menu entry which was selected * @return true if the Menu item was legit (and we consumed it), false * otherwise */ @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_START: mLunarThread.doStart(); return true; case MENU_STOP: mLunarThread.setState(LunarThread.STATE_LOSE, getText(R.string.message_stopped)); return true; case MENU_PAUSE: mLunarThread.pause(); return true; case MENU_RESUME: mLunarThread.unpause(); return true; case MENU_EASY: mLunarThread.setDifficulty(LunarThread.DIFFICULTY_EASY); return true; case MENU_MEDIUM: mLunarThread.setDifficulty(LunarThread.DIFFICULTY_MEDIUM); return true; case MENU_HARD: mLunarThread.setDifficulty(LunarThread.DIFFICULTY_HARD); return true; } return false; } /** * Invoked when the Activity is created. * * @param savedInstanceState a Bundle containing state saved from a previous * execution, or null if this is a new execution */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // tell system to use the layout defined in our XML file setContentView(R.layout.lunar_layout); // get handles to the LunarView from XML, and its LunarThread mLunarView = (LunarView) findViewById(R.id.lunar); mLunarThread = mLunarView.getThread(); // give the LunarView a handle to the TextView used for messages mLunarView.setTextView((TextView) findViewById(R.id.text)); if (savedInstanceState == null) { // we were just launched: set up a new game mLunarThread.setState(LunarThread.STATE_READY); Log.w(this.getClass().getName(), "SIS is null"); } else { // we are being restored: resume a previous game mLunarThread.restoreState(savedInstanceState); Log.w(this.getClass().getName(), "SIS is nonnull"); } } /** * Invoked when the Activity loses user focus. */ @Override protected void onPause() { super.onPause(); mLunarView.getThread().pause(); // pause game when Activity pauses } /** * Notification that something is about to happen, to give the Activity a * chance to save state. * * @param outState a Bundle into which this Activity should save its state */ @Override protected void onSaveInstanceState(Bundle outState) { // just have the View's thread save its state into our Bundle super.onSaveInstanceState(outState); mLunarThread.saveState(outState); Log.w(this.getClass().getName(), "SIS called"); } } // //res\layout\lunar_layout.xml <?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2007 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. --> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.android.lunarlander.LunarView android:id="@+id/lunar" android:layout_width="match_parent" android:layout_height="match_parent"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/text" android:text="@string/lunar_layout_text_text" android:visibility="visible" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center_horizontal" android:textColor="#88ffffff" android:textSize="24sp"/> </RelativeLayout> </FrameLayout> // //res\values\strings.xml <?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2007 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. --> <resources> <string name="app_name">Lunar Lander</string> <string name="menu_start">Start</string> <string name="menu_stop">Stop</string> <string name="menu_pause">Pause</string> <string name="menu_resume">Resume</string> <string name="menu_easy">Easy</string> <string name="menu_medium">Medium</string> <string name="menu_hard">Hard</string> <string name="mode_ready">Lunar Lander\nPress Up To Play</string> <string name="mode_pause">Paused\nPress Up To Resume</string> <string name="mode_lose">Game Over\nPress Up To Play</string> <string name="mode_win_prefix">Success!\n</string> <string name="mode_win_suffix">in a row\nPress Up to Play</string> <string name="message_stopped">Stopped</string> <string name="message_off_pad">Off Landing Pad</string> <string name="message_too_fast">Too Fast</string> <string name="message_bad_angle">Bad Angle</string> <string name="lunar_layout_text_text"></string> </resources>