要实现的效果如图:
代码很简单,主要是TranslateAnimation类的
public TranslateAnimation (int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
Since:
API Level 1
Constructor to use when building a TranslateAnimation from code
Parameters
fromXType | Specifies how fromXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT. |
---|---|
fromXValue | Change in X coordinate to apply at the start of the animation. This value can either be an absolute number if fromXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. |
toXType | Specifies how toXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT. |
toXValue | Change in X coordinate to apply at the end of the animation. This value can either be an absolute number if toXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. |
fromYType | Specifies how fromYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT. |
fromYValue | Change in Y coordinate to apply at the start of the animation. This value can either be an absolute number if fromYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. |
toYType | Specifies how toYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT. |
toYValue | Change in Y coordinate to apply at the end of the animation. This value can either be an absolute number if toYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise |
- package
com.lenovo.wufl.translateanimationmenu; -
- import
android.app.Activity; - import
android.os.Bundle; - import
android.view.View; - import
android.view.View.OnClickListener; - import
android.view.animation.Animation; - import
android.view.animation.TranslateAnimation; - import
android.widget.Button; - import
android.widget.LinearLayout; - import
android.widget.TextView; -
- public
class TranslateAnimationMenuAc tivity extends Activity implements -
OnClickListener { -
private TextView mMenu; -
private Button mButton; -
private TranslateAnimation mShowAnimation; -
private TranslateAnimation mHideAnimation; -
private boolean isShow; -
-
-
@Override -
public void onCreate(Bundle savedInstanceState) { -
super.onCreate(savedInstanceState); -
setContentView(R.layout.main); -
-
initView(); -
initAnimation(); -
} -
-
private void initView() { -
mMenu = (TextView) findViewById(R.id.menu); -
mButton = (Button) findViewById(R.id.button); -
mButton.setOnClickListener(this); -
mMenu.setVisibility(View.GONE); -
isShow = false; -
} -
-
private void initAnimation() { -
// 从自已-1倍的位置移到自己原来的位置 -
mShowAnimation = 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); -
mHideAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -
0.0f, Animation.RELATIVE_TO_SELF, 0.0f, -
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -
-1.0f); -
mShowAnimation.setDuration(500); -
mHideAnimation.setDuration(500); -
-
} -
-
@Override -
public void onClick(View v) { -
if (v.getId() == R.id.button) { -
if (isShow) { -
isShow = false; -
mMenu.startAnimation(mHideAnimation); -
mMenu.setVisibility(View.GONE); -
} else { -
isShow = true; -
mMenu.startAnimation(mShowAnimation); -
mMenu.setVisibility(View.VISIBLE); -
} -
} -
} - }
package com.lenovo.wufl.translateanimationmenu; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class TranslateAnimationMenuActivity extends Activity implements OnClickListener { private TextView mMenu; private Button mButton; private TranslateAnimation mShowAnimation; private TranslateAnimation mHideAnimation; private boolean isShow; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); initAnimation(); } private void initView() { mMenu = (TextView) findViewById(R.id.menu); mButton = (Button) findViewById(R.id.button); mButton.setOnClickListener(this); mMenu.setVisibility(View.GONE); isShow = false; } private void initAnimation() { // 从自已-1倍的位置移到自己原来的位置 mShowAnimation = 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); mHideAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f); mShowAnimation.setDuration(500); mHideAnimation.setDuration(500); } @Override public void onClick(View v) { if (v.getId() == R.id.button) { if (isShow) { isShow = false; mMenu.startAnimation(mHideAnimation); mMenu.setVisibility(View.GONE); } else { isShow = true; mMenu.startAnimation(mShowAnimation); mMenu.setVisibility(View.VISIBLE); } } } }
布局: