import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ImageView.ScaleType;
public class MainActivity extends Activity {
private LeftSlidingMenu leftSlidingMenu;
private List<String>list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDate();
initView();
}
private void initDate() {
list=new ArrayList<String>();
for (int i = 0; i < 30; i++) {
list.add("item"+i);
}
}
private void initView() {
leftSlidingMenu=(LeftSlidingMenu)findViewById(R.id.myslidingMenu);
ImageView imageView=new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
imageView.setScaleType(ScaleType.FIT_XY);
leftSlidingMenu.setBottomView(imageView);
ListView listView = new ListView(this);
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
leftSlidingMenu.setTopView(listView);
}
}
package cn.bgs.zidingyichouti3;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.FrameLayout.LayoutParams;
public class LeftSlidingMenu extends FrameLayout{
private LinearLayout top_layout;
private LinearLayout bootom_layout;
private boolean SlidingFlag=false;
private int Max_width=0;
private PointF startpf=new PointF();
private PointF startpf2=new PointF();
private PointF overf=new PointF();
private boolean isFirst=true;
public LeftSlidingMenu(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (isFirst) {
Max_width=(int)(getMeasuredWidth()*0.7);
bootom_layout.setLayoutParams(new FrameLayout.LayoutParams(Max_width,FrameLayout.LayoutParams.MATCH_PARENT));
top_layout.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));
addView(bootom_layout);
addView(top_layout);
}
isFirst=false;
}
private void initView() {
//设置底部layout 的属性
bootom_layout=new LinearLayout(getContext());
bootom_layout.setOrientation(LinearLayout.VERTICAL);
//设置top_layout属性
top_layout=new LinearLayout(getContext());
top_layout.setOrientation(LinearLayout.VERTICAL);
top_layout.setBackgroundColor(Color.LTGRAY);
}
public void setBottomView(View v){
v.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
bootom_layout.addView(v);
}
public void setTopView(View v){
v.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
top_layout.addView(v);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction()==MotionEvent.ACTION_DOWN) {
startpf.x=ev.getX();
startpf.y=ev.getY();
startpf2.x=ev.getX();
startpf2.y=ev.getY();
}else if (ev.getAction()==MotionEvent.ACTION_MOVE) {
overf.x=ev.getX();
overf.y=ev.getY();
float disx=overf.x-startpf.x;
float disy=overf.y-startpf.y;
if (Math.abs(disx)/2-Math.abs(disy)>0) {
Log.e("tag","11111");
}else {
if (!SlidingFlag) {
return super.dispatchTouchEvent(ev);
}else {
return true;
}
}
if (disx>0) {
FrameLayout.LayoutParams topparms=(LayoutParams)top_layout.getLayoutParams();
if (topparms.leftMargin>=Max_width) {
disx=Max_width;
SlidingFlag=true;
}
topparms.leftMargin=(int) disx;
topparms.rightMargin=(int) -disx;
top_layout.setLayoutParams(topparms);
}else if (disx<0) {
FrameLayout.LayoutParams topparms=(LayoutParams)top_layout.getLayoutParams();
if (topparms.leftMargin<=0) {
disx=0;
SlidingFlag=false;
}
topparms.leftMargin=(int) (topparms.leftMargin-Math.abs(disx));
topparms.rightMargin=-topparms.leftMargin;
top_layout.setLayoutParams(topparms);
startpf.x=overf.x;
}
requestLayout();
return true;
}else if (ev.getAction()==MotionEvent.ACTION_UP) {
int disX=(int)Math.abs(ev.getX()-startpf2.x);
if (disX>10) {
FrameLayout.LayoutParams topparms=(LayoutParams)top_layout.getLayoutParams();
if (topparms.leftMargin>Max_width/2) {
topparms.leftMargin=Max_width;
topparms.rightMargin=-Max_width;
SlidingFlag=true;
}else {
topparms.leftMargin=0;
topparms.rightMargin=0;
SlidingFlag=false;
}
top_layout.setLayoutParams(topparms);
requestLayout();
return true;
}
}
return super.dispatchTouchEvent(ev);
}
}