动画导航
版权声明:曾经的Blog文章合并。原创作品,谢绝转载。否则将追究法律责任。
定义动画布局例如以下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFFFF" > <Button android:id="@+id/button_composer_sleep" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/composer_sleep" android:layout_alignParentBottom="true" android:layout_marginLeft="10dip" android:layout_marginBottom="10dip" /> <Button android:id="@+id/button_composer_thought" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/composer_thought" android:layout_alignParentBottom="true" android:layout_marginLeft="10dip" android:layout_marginBottom="10dip" /> <Button android:id="@+id/button_composer_music" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/composer_music" android:layout_alignParentBottom="true" android:layout_marginLeft="10dip" android:layout_marginBottom="10dip" /> <Button android:id="@+id/button_composer_place" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/composer_place" android:layout_alignParentBottom="true" android:layout_marginLeft="10dip" android:layout_marginBottom="10dip" /> <Button android:id="@+id/button_composer_with" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/composer_with" android:layout_alignParentBottom="true" android:layout_marginLeft="10dip" android:layout_marginBottom="10dip" /> <Button android:id="@+id/button_composer_camera" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/composer_camera" android:layout_alignParentBottom="true" android:layout_marginLeft="10dip" android:layout_marginBottom="10dip" /> <Button android:id="@+id/button_friends_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/friends_delete" android:layout_alignParentBottom="true" android:layout_marginLeft="10dip" android:layout_marginBottom="10dip" /> </RelativeLayout>
public class MainActivity extends Activity implements OnClickListener{ private Button mCameraBtn,mWithBtn,mPlaceBtn,mMusicBtn,mThoughtBtn,mSleepBtn,mDeleteBtn; private static int HEIGHT; private Animation rotateAnimation,translateAnimation; private LayoutParams params=new LayoutParams(0,0); private boolean isClick=false; @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getDisplay(); initBtn(); } private void getDisplay() { Display display=getWindowManager().getDefaultDisplay(); HEIGHT=display.getHeight(); } private void initBtn() { params.width=50; params.height=50; params.setMargins(10,HEIGHT-98,0,0); mCameraBtn=initSubMenu(mCameraBtn,R.id.button_composer_camera,params); mWithBtn=initSubMenu(mWithBtn,R.id.button_composer_with,params); mPlaceBtn=initSubMenu(mPlaceBtn,R.id.button_composer_place,params); mMusicBtn=initSubMenu(mMusicBtn,R.id.button_composer_music,params); mThoughtBtn=initSubMenu(mThoughtBtn,R.id.button_composer_thought,params); mSleepBtn=initSubMenu(mSleepBtn,R.id.button_composer_sleep,params); mDeleteBtn=(Button) findViewById(R.id.button_friends_delete); mDeleteBtn.setLayoutParams(params); mDeleteBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (!isClick) { mDeleteBtn.startAnimation(roatateAnimation(-45.0f,0.5f,0.45f)); mCameraBtn.startAnimation(translateAnimation(0.0f,-180.0f,10,HEIGHT-275,mCameraBtn,80)); mWithBtn.startAnimation(translateAnimation(0.0f,-150.0f,55,HEIGHT-250, mWithBtn, 110)); mPlaceBtn.startAnimation(translateAnimation(0.0f,-130.0f,100,HEIGHT-220, mPlaceBtn, 140)); mMusicBtn.startAnimation(translateAnimation(0.0f,-110.0f,140,HEIGHT-185, mMusicBtn, 170)); mThoughtBtn.startAnimation(translateAnimation(0.0f,-90.0f,180,HEIGHT-145, mThoughtBtn, 200)); mSleepBtn.startAnimation(translateAnimation(0.0f,-90.0f,210,HEIGHT-100, mSleepBtn, 230)); isClick=true; }else { mDeleteBtn.startAnimation(roatateAnimation(90.0f,0.5f,0.45f)); mCameraBtn.startAnimation(translateAnimation(0.0f,140.0f,10,HEIGHT-98,mCameraBtn,180)); mWithBtn.startAnimation(translateAnimation(-40.0f,110.0f,10,HEIGHT-98, mWithBtn, 160)); mPlaceBtn.startAnimation(translateAnimation(-80.0f,80.0f,10,HEIGHT-98, mPlaceBtn, 140)); mMusicBtn.startAnimation(translateAnimation(-120.0f,50.0f,10,HEIGHT-98, mMusicBtn, 170)); mThoughtBtn.startAnimation(translateAnimation(-160.0f,25.0f,10,HEIGHT-98, mThoughtBtn, 200)); mSleepBtn.startAnimation(translateAnimation(-200.0f,0.0f,10,HEIGHT-98, mSleepBtn, 230)); isClick=false; } } }); } private Button initSubMenu(Button menuBtn,int resId,LayoutParams layoutParams) { menuBtn=(Button) findViewById(resId); menuBtn.setLayoutParams(layoutParams); menuBtn.setOnClickListener(this); return menuBtn; } protected Animation translateAnimation(float toX, float toY, final int lastX, final int lastY, final Button btn,long duration) { translateAnimation=new TranslateAnimation(0,toX,0,toY); translateAnimation.setDuration(duration); translateAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { params=new LayoutParams(50,50); params.setMargins(lastX,lastY,0,0); btn.setLayoutParams(params); btn.clearAnimation(); } }); return translateAnimation; } protected Animation roatateAnimation(float toDegrees, float pivotXValue, float pivotYValue) { rotateAnimation=new RotateAnimation(0, toDegrees, Animation.RELATIVE_TO_SELF, pivotXValue, Animation.RELATIVE_TO_SELF, pivotYValue); rotateAnimation.setDuration(500); rotateAnimation.setFillAfter(true); return rotateAnimation; } private Animation scaleAnimation(float toX,float toY){ ScaleAnimation scaleAnimation=new ScaleAnimation(1.0f, toX,1.0f, toY ,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); scaleAnimation.setInterpolator(new AccelerateDecelerateInterpolator()); scaleAnimation.setFillAfter(false); scaleAnimation.setDuration(500); return scaleAnimation; } @Override public void onClick(View v) { mCameraBtn.startAnimation(scaleAnimation(0.0f, 0.0f)); mWithBtn.startAnimation(scaleAnimation(0.0f, 0.0f)); mPlaceBtn.startAnimation(scaleAnimation(0.0f, 0.0f)); mMusicBtn.startAnimation(scaleAnimation(0.0f, 0.0f)); mThoughtBtn.startAnimation(scaleAnimation(0.0f, 0.0f)); mSleepBtn.startAnimation(scaleAnimation(0.0f, 0.0f)); v.startAnimation(scaleAnimation(2.5f, 2.5f)); } }
执行例如以下