objectAnimator是一种焦点可以随着动画移动的动画,下面用一个简单的平移动画的例子,来实现侧滑菜单。
首先是页面主体代码:
package com.example.myobjectanimation;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
/** * 显示侧滑菜单 */
private static final int MENU_VISIBLE = 0;
/** * 隐藏侧滑菜单 */
private static final int MENU_GONE = 1;
private LinearLayout llMain;
private LinearLayout llMenu;
private LinearLayout llMenuRight;
private LinearLayout llMenuLeft;
private TextView tvMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
llMain = (LinearLayout) findViewById(R.id.ll_main);
llMain.setOnClickListener(this);
llMenu = (LinearLayout) findViewById(R.id.ll_menu);
llMenu.setOnClickListener(this);
llMenuLeft = (LinearLayout) findViewById(R.id.ll_menu_left);
llMenuLeft.setOnClickListener(this);
llMenuRight = (LinearLayout) findViewById(R.id.ll_menu_right);
llMenuRight.setOnClickListener(this);
tvMain = (TextView) findViewById(R.id.tv_main);
tvMain.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ll_menu_left:
setLayoutType(MENU_GONE);
break;
case R.id.tv_main:
setLayoutType(MENU_VISIBLE);
break;
default:
break;
}
}
/** * 控制显隐状态 * * @param type * 区分显示隐藏 */
private void setLayoutType(int type) {
switch (type) {
case MENU_VISIBLE:
llMenu.setVisibility(View.VISIBLE);
llMain.setFocusable(false);
startAnimation(MENU_VISIBLE);
break;
case MENU_GONE:
startAnimation(MENU_GONE);
break;
default:
break;
}
}
/** * 设置显示隐藏动画 * * @param type * 区分显示隐藏 */
private void startAnimation(int type) {
switch (type) {
case MENU_VISIBLE:
ObjectAnimator oa = ObjectAnimator.ofFloat(llMenuRight, "translationX", 0, getWidth() / 3 * 2);
oa.setDuration(0);
oa.start();
oa.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
ObjectAnimator oa2 = ObjectAnimator.ofFloat(llMenuRight, "translationX", getWidth() / 3 * 2, 0);
oa2.setDuration(200);
oa2.start();
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
break;
case MENU_GONE:
ObjectAnimator oa2 = ObjectAnimator.ofFloat(llMenuRight, "translationX", 0, getWidth() / 3 * 2);
oa2.setDuration(200);
oa2.start();
oa2.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
llMenu.setVisibility(View.GONE);
llMain.setFocusable(true);
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
break;
default:
break;
}
}
/** * 获取屏幕宽度 * * @return 屏幕宽度 */
private int getWidth() {
// 获取屏幕的长宽像素
DisplayMetrics dm = new DisplayMetrics();
// 此处this为一个activity
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
}
然后是布局界面,简单两个界面,一个显示一个隐藏,在代码中利用动画和显示隐藏交替实现来模拟侧滑菜单效果:
<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" >
<LinearLayout android:id="@+id/ll_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical" >
<TextView android:id="@+id/tv_main" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="主页面" android:textSize="25sp" />
</LinearLayout>
<LinearLayout android:id="@+id/ll_menu" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#aa2c2c32" android:orientation="horizontal" android:visibility="gone" >
<LinearLayout android:id="@+id/ll_menu_left" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:orientation="vertical" >
</LinearLayout>
<LinearLayout android:id="@+id/ll_menu_right" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:background="#ffffff" android:orientation="vertical" >
<TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="侧滑菜单" android:textColor="#000000" android:textSize="25sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
利用这个小界面可以实现一些轻量级的侧滑菜单的应用,而且不用导入什么三方包,可以减少程序的臃肿度。