一直没怎么用过SlidingMenu的侧滑效果,最近简单的了解并尝试关联使用了一下...
1)首先是在网上下载了一个SlidingMenu关联包demo(librarySliding),然后直接关联引用就行了,但必须的注意你当前工程的版本是否和SlidingMenu有冲突,我简单的写完了之后发现运行老是报错,折腾了好久之后我就果断把我项目里的v4包给删了,然后运行就正常了,呵呵...
2)现在就是使用的情况了(我是使用的fragment作为侧滑的布局的根布局使用了framlayout,activity继承了FragmentActivity)
3)fragment:
布局:<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fl_right"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fffffff0" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fffffff0" >
<ImageView
android:id="@+id/img_head"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:src="@drawable/head" />
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/img_head"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="下雨天,太冷"
android:textColor="#0000ff"
android:textSize="20sp"
android:textStyle="bold|italic" />
</RelativeLayout>
</FrameLayout>
代码:
public class Fragment_Right extends Fragment {
private SlidingMenu slid;
public Fragment_Right(SlidingMenu slid) {
this.slid = slid;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.item_slide, null);
return v;
}
}
activity:
private SlidingMenu initView() {
img_out_head = (ImageView) findViewById(R.id.img_out_head);
SlidingMenu slid = new SlidingMenu(getApplicationContext(), null);
// 设置抽屉模式
slid.setMode(SlidingMenu.RIGHT);
// 设置布局
View v = LayoutInflater.from(getApplicationContext()).inflate(
R.layout.item_slide, null);
slid.setMenu(v);
// DisplayMetrics outMetrics = new DisplayMetrics();
// getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
// LayoutParams lp = slid.getLayoutParams();
// lp.width = outMetrics.widthPixels / 2;
// 设置抽屉的宽度
// slid.setBehindWidth(lp.width);
// slid.setBehindWidthRes(R.dimen.slid_width);
slid.setBehindWidth(300);
// 设置抽屉边框线宽度
// slid.setBehindOffset(3);
// 设置触摸模式
slid.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
// 绑定到activity
slid.attachToActivity(MainActivity.this, SlidingMenu.SLIDING_CONTENT);// 不覆盖导航栏
// slid.attachToActivity(MainActivity.this,
// SlidingMenu.SLIDING_WINDOW);// 覆盖导航栏
slid.setAlpha(5);
slid.setFadeEnabled(true);
// 设置渐入渐出效果的值
slid.setFadeDegree((float) 0.35);
slid.setShadowWidth(3);
slid.setShadowDrawable(R.color.blue);
scaleWay(slid);
return slid;
}
private void scaleWay(SlidingMenu menu) {
CanvasTransformer ctf = new CanvasTransformer() {
@Override
public void transformCanvas(Canvas canvas, float percentOpen) {
/** 一、缩放 */
// float scale = (float) (percentOpen * 0.25 + 0.75);
// canvas.scale(scale, scale, canvas.getWidth() / 2,
// canvas.getHeight() / 2);
/** 二 、位移 */
// canvas.translate(
// 0,
// canvas.getHeight()
// * (1 - interp.getInterpolation(percentOpen)));
/** 三 、拉伸动画 */
canvas.scale(percentOpen, 1, 0, 0);
}
};
menu.setBehindCanvasTransformer(ctf);
}
private static Interpolator interp = new Interpolator() {
@Override
public float getInterpolation(float input) {
input -= 1.0f;
return input * input * input + 1.0f;
}
};
onCreate()方法里调用:
SlidingMenu slide = initView();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fl_right, new Fragment_Right(slide)).commit();
其他监听方法就不写...