PathButton是仿Path按钮动画效果的实例,项目如图:
效果如图:
包含测试文件就两个目标文件(除了布局文件)!
//动画类
public class MyAnimations
{
private final static int R = 200;
private static int xOffset = 15;
private static int yOffset = -13;
public static void initOffset(Context context)
{// 由布局文件
xOffset = (int) (10.667 * context.getResources().getDisplayMetrics().density);
yOffset = -(int) (8.667 * context.getResources().getDisplayMetrics().density);
}
public static Animation getRotateAnimation(float fromDegrees,
float toDegrees, int durationMillis)
{
RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotate.setDuration(durationMillis);
rotate.setFillAfter(true);
return rotate;
}
public static void startAnimationsIn(ViewGroup viewgroup, int durationMillis)
{
for (int i = 0; i < viewgroup.getChildCount(); i++)
{
ImageButton inoutimagebutton = (ImageButton) viewgroup
.getChildAt(i);
inoutimagebutton.setVisibility(0);
double marginTop = Math.sin(36.0 * i * Math.PI / 180) * R;
double marginRight = Math.cos(36.0 * i * Math.PI / 180) * R;
Log.d("animation", marginTop + "," + marginRight);
Animation animation = new TranslateAnimation(0, -(int) marginRight,
0, -(int) marginTop);
animation.setFillAfter(true);
animation.setDuration(durationMillis);
animation.setStartOffset((i * 100)
/ (-1 + viewgroup.getChildCount()));
animation.setInterpolator(new OvershootInterpolator(2F));
inoutimagebutton.startAnimation(animation);
}
}
public static void startAnimationsOut(ViewGroup viewgroup,
int durationMillis)
{
for (int i = 0; i < viewgroup.getChildCount(); i++)
{
final ImageButton inoutimagebutton = (ImageButton) viewgroup
.getChildAt(i);
double marginTop = Math.sin(36.0 * i * Math.PI / 180) * R;
double marginRight = Math.cos(36.0 * i * Math.PI / 180) * R;
Log.d("animation", marginTop + "," + marginRight);
Animation animation = new TranslateAnimation(-(int) marginRight, 0,
-(int) marginTop, 0);
animation.setFillAfter(true);
animation.setDuration(durationMillis);
animation.setStartOffset(((viewgroup.getChildCount() - i) * 100)
/ (-1 + viewgroup.getChildCount()));// 顺序倒一下比较舒服
animation.setInterpolator(new AnticipateInterpolator(2F));
animation.setAnimationListener(new Animation.AnimationListener()
{
@Override
public void onAnimationStart(Animation arg0)
{
}
@Override
public void onAnimationRepeat(Animation arg0)
{
}
@Override
public void onAnimationEnd(Animation arg0)
{
// TODO Auto-generated method stub
inoutimagebutton.setVisibility(8);
}
});
inoutimagebutton.startAnimation(animation);
}
}
}
测试界面类:
public class PathMenuActivity extends Activity
{
/** Called when the activity is first created. */
private boolean areButtonsShowing;
private RelativeLayout composerButtonsWrapper;
private ImageView composerButtonsShowHideButtonIcon;
private RelativeLayout composerButtonsShowHideButton;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyAnimations.initOffset(PathMenuActivity.this);
composerButtonsWrapper = (RelativeLayout) findViewById(R.id.composer_buttons_wrapper);
composerButtonsShowHideButton = (RelativeLayout) findViewById(R.id.composer_buttons_show_hide_button);
composerButtonsShowHideButtonIcon = (ImageView) findViewById(R.id.composer_buttons_show_hide_button_icon);
composerButtonsShowHideButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (!areButtonsShowing)
{
MyAnimations.startAnimationsIn(composerButtonsWrapper, 300);
composerButtonsShowHideButtonIcon
.startAnimation(MyAnimations.getRotateAnimation(0,
-270, 300));
} else
{
MyAnimations
.startAnimationsOut(composerButtonsWrapper, 300);
composerButtonsShowHideButtonIcon
.startAnimation(MyAnimations.getRotateAnimation(
-270, 0, 300));
}
areButtonsShowing = !areButtonsShowing;
}
});
for (int i = 0; i < composerButtonsWrapper.getChildCount(); i++)
{
composerButtonsWrapper.getChildAt(i).setOnClickListener(
new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
}
});
}
composerButtonsShowHideButton.startAnimation(MyAnimations
.getRotateAnimation(0, 360, 200));
}
}
代码简单易懂!~~
学习的目标是成熟!~