Back to project page android-animation.
The source code is released under:
MIT License
If you think the Android project android-animation listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.myanimation; //from www . java 2 s .c om import android.R.integer; import android.animation.Animator; import android.animation.Animator.AnimatorListener; import android.animation.AnimatorSet; import android.animation.ArgbEvaluator; import android.animation.LayoutTransition; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.animation.ValueAnimator.AnimatorUpdateListener; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.animation.LinearInterpolator; import android.widget.Button; import android.widget.LinearLayout; public class MainActivity extends Activity { // ??? (ApiDemos??) private static final int RED = 0xffFF8080; private static final int BLUE = 0xff8080FF; private static final int CYAN = 0xff80ffff; private static final int GREEN = 0xff80ff80; // ?????? public void buttonMethod(View button){ // ObjectAnimator????????????? ObjectAnimator colorAnim = ObjectAnimator.ofInt(button, "backgroundColor", RED, BLUE); colorAnim.setDuration(3000); colorAnim.setEvaluator(new ArgbEvaluator()); colorAnim.setRepeatCount(ValueAnimator.INFINITE); colorAnim.setRepeatMode(ValueAnimator.REVERSE); colorAnim.start(); // ObjectAnimator???????????????? ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(button, "alpha", 1f, 0.5f); alphaAnim.setDuration(3000); alphaAnim.setRepeatCount(ValueAnimator.INFINITE); alphaAnim.setRepeatMode(ValueAnimator.REVERSE); alphaAnim.start(); // ObjectAnimator???????????????? ObjectAnimator scaleAnimX = ObjectAnimator.ofFloat(button, "scaleX", 1f, 0.5f); scaleAnimX.setDuration(3000); scaleAnimX.setRepeatCount(ValueAnimator.INFINITE); scaleAnimX.setRepeatMode(ValueAnimator.REVERSE); scaleAnimX.start(); ObjectAnimator scaleAnimY = ObjectAnimator.ofFloat(button, "scaleY", 1f, 0.5f); scaleAnimY.setDuration(3000); scaleAnimY.setRepeatCount(ValueAnimator.INFINITE); scaleAnimY.setRepeatMode(ValueAnimator.REVERSE); scaleAnimY.start(); // ???? final Button b = (Button)button; colorAnim.addListener(new AnimatorListener() { @Override public void onAnimationStart(Animator animation) { b.setText("START"); } @Override public void onAnimationRepeat(Animator animation) { Integer i = (Integer)b.getTag(); if(i == null){ i = Integer.valueOf(0); } b.setText("REPEAT " + i); i++; b.setTag(i); } @Override public void onAnimationEnd(Animator animation) { b.setText("END"); } @Override public void onAnimationCancel(Animator animation) { b.setText("CANCEL"); } }); colorAnim.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { // TODO Auto-generated method stub } }); } public void buttonMethod2(View button){ // ObjectAnimator????????????? ObjectAnimator colorAnim = ObjectAnimator.ofInt(button, "backgroundColor", RED, BLUE); colorAnim.setDuration(3000); colorAnim.setEvaluator(new ArgbEvaluator()); colorAnim.setRepeatCount(ValueAnimator.INFINITE); colorAnim.setRepeatMode(ValueAnimator.REVERSE); colorAnim.start(); // ObjectAnimator???????????????? ObjectAnimator scaleAnimX = ObjectAnimator.ofFloat(button, "scaleX", 1f, 0.5f); scaleAnimX.setDuration(1500); ObjectAnimator scaleAnimY = ObjectAnimator.ofFloat(button, "scaleY", 1f, 0f); scaleAnimY.setDuration(1500); // ObjectAnimator???????????????? ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(button, "alpha", 1f, 0f); alphaAnim.setDuration(1500); // ???? final Button b = (Button)button; scaleAnimX.addListener(new AnimatorListener() { @Override public void onAnimationStart(Animator animation) { b.setText("START"); } @Override public void onAnimationRepeat(Animator animation) { b.setText("REPEAT"); } @Override public void onAnimationEnd(Animator animation) { b.setText("END"); } @Override public void onAnimationCancel(Animator animation) { b.setText("CANCEL"); } }); // ???? AnimatorSet animSet = new AnimatorSet(); animSet.play(alphaAnim).after(scaleAnimX); animSet.play(scaleAnimX); animSet.play(scaleAnimY).after(scaleAnimX); animSet.start(); } public void buttonMethod3(View button){ // ObjectAnimator???????????????? ObjectAnimator scaleAnimX = ObjectAnimator.ofFloat(button, "scaleX", 1f, 0f, 1f); scaleAnimX.setDuration(500); scaleAnimX.start(); } public void buttonMethod4(View button){ // ObjectAnimator???????????????? ObjectAnimator scaleAnimX = ObjectAnimator.ofFloat(button, "rotationX", 0f, 360f); scaleAnimX.setDuration(1000); scaleAnimX.setInterpolator(new LinearInterpolator()); scaleAnimX.setRepeatCount(10); scaleAnimX.start(); ObjectAnimator scaleAnimY = ObjectAnimator.ofFloat(button, "rotationY", 0f, 360f); scaleAnimY.setDuration(10000); scaleAnimY.setInterpolator(new LinearInterpolator()); scaleAnimY.start(); } LinearLayout mContainer; int mCount = 0; public void buttonMethod5(View button){ Button newButton = new Button(this); newButton.setText(String.valueOf(mCount++)); newButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { mContainer.removeView(v); } }); mContainer.addView(newButton, Math.min(1, mContainer.getChildCount())); } LayoutTransition mTransition; ObjectAnimator mAppearingAnim; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Container mContainer = (LinearLayout)findViewById(R.id.LinearLayout1); // LayoutTransition?????? mTransition = new LayoutTransition(); mContainer.setLayoutTransition(mTransition); // LayoutTransition?? //mAppearingAnim = new ObjectAnimator(); //mAppearingAnim.ofFloat(values); //mTransition.setAnimator(LayoutTransition.APPEARING, mAppearingAnim); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }