您的位置:首页 > 其它

仿QQ侧滑菜单

2015-10-20 14:18 267 查看


package com.anqiansong.sideslip.view;

import com.anqiansong.sideslip.R;

import android.R.integer;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.view.View.OnTouchListener;
public class SideSlipView extends FrameLayout implements OnTouchListener{

private LinearLayout ll_controler;//侧滑控制层容器
private LinearLayout ll_bottom;//侧滑菜单容器
private int currentX;
private int width,height;//屏幕长宽
private float scale=0;//滑动比例
private float S_Width=0;//侧滑区域,初始化为0
private int slip_swith=200;//侧滑预留空间
private LayoutParams sideSlipParams;//侧滑菜单Params
private View sideSlipView;
private View mainView;
private Context context;
private boolean status;
private long touchLong;//手指在屏幕上停留的时间,来判断此事件为点击事件还是touch事件
private FrameLayout fl_layout;
public SideSlipView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
DisplayMetrics dm=getResources().getDisplayMetrics();
this.context=context;
width=dm.widthPixels;
height=dm.heightPixels-50;
}

public SideSlipView(Context context, AttributeSet attrs) {
super(context, attrs);
DisplayMetrics dm=getResources().getDisplayMetrics();
this.context=context;
width=dm.widthPixels;
height=dm.heightPixels-50;
}

public SideSlipView(Context context) {
super(context);
DisplayMetrics dm=getResources().getDisplayMetrics();
this.context=context;
width=dm.widthPixels;
height=dm.heightPixels-50;
}

/**
* 在调用此方法之前必须先对侧滑菜单view和主界面view进行初始化,否则报空指针异常
*/
public void createView(){
LayoutInflater.from(context).inflate(R.layout.sideslipview, this);
S_Width=width-slip_swith;
ll_controler=(LinearLayout) findViewById(R.id.ll_above);
ll_bottom=(LinearLayout) findViewById(R.id.ll_bottom);
fl_layout=(FrameLayout) findViewById(R.id.fl_layout);
ll_bottom.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
smoothTo(ll_controler, 0);
}
});

if(sideSlipView!=null){
ll_bottom.addView(sideSlipView);
if(mainView!=null){
ll_controler.addView(mainView);
LayoutParams controlerParams=new LayoutParams(width, height);//控制层的params
controlerParams.height=height;
controlerParams.gravity=Gravity.CENTER_VERTICAL;
LayoutParams slipParams=new LayoutParams(width-220, height-200);//侧滑菜单容器层的params
slipParams.leftMargin=width-slip_swith;
slipParams.topMargin=slip_swith;
slipParams.bottomMargin=slip_swith;
slipParams.gravity=Gravity.CENTER_VERTICAL;
ll_controler.setLayoutParams(controlerParams);
ll_bottom.setLayoutParams(slipParams);
sideSlipParams=(LayoutParams) ll_bottom.getLayoutParams();
ll_controler.setOnTouchListener(this);
}else{
throw new NullPointerException("mainView can not be null");
}
}else{
throw new NullPointerException("sideSlipView can not be null");
}
}
/**
* 侧滑菜单
* @param view 其父布局必须为LinearLayout
*/
public void setSideSlipView(View view){
sideSlipView=view;
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.MATCH_PARENT);
sideSlipView.setLayoutParams(params);
}
/**
* activity view
* @param view 其父布局必须为LinearLayout
*/
public void setMainView(View view){
mainView=view;
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.MATCH_PARENT);
mainView.setLayoutParams(params);
}
public void setBackgroundResource(int resid){
fl_layout.setBackgroundResource(resid);
}
public void setBackgroundDrawable(Drawable background){
fl_layout.setBackgroundDrawable(background);
}
public void setBackgroundColor(int color){
fl_layout.setBackgroundColor(color);
}
/**
* @param width 侧滑菜单宽度
*/
public void setSideSlip(int width){
if(width>this.width){
throw new IllegalArgumentException("参数异常,侧滑菜单宽度不能大于屏幕宽度");
}else{
slip_swith=this.width-width;
}
}
/**
* 打开侧滑菜单
*/
public void openSideSlip(){
status=true;
smoothTo(ll_controler, width-slip_swith);
}
/**
* 关闭侧滑菜单
*/
public void closeSideSlip(){
status=false;
smoothTo(ll_controler, 0);
}
/**
* @return 判断当前侧滑是否处于打开状态
*/
public boolean isOpen(){
return status;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
currentX = (int) event.getRawX();
touchLong=System.currentTimeMillis();
Log.d("tag", "----Action Down-------");
break;
case MotionEvent.ACTION_MOVE:
int x2 = (int) event.getRawX();
int y2 = (int) event.getRawY();
LayoutParams params=(LayoutParams) v.getLayoutParams();
params.width=width;
params.height=height;
int x=x2-currentX;

if(params.leftMargin>=0){
params.leftMargin+=x;
if(params.leftMargin<0){
params.leftMargin=0;
params.topMargin=0;
params.bottomMargin=0;
params.height=this.height;
//底层
sideSlipParams.leftMargin=width-slip_swith;
sideSlipParams.topMargin=200;
sideSlipParams.bottomMargin=200;
}else{//
if(params.leftMargin>width-slip_swith){
params.leftMargin=width-slip_swith;
params.rightMargin=slip_swith-width ;
scale=Math.abs((slip_swith-width)/S_Width);
params.topMargin=(int) (scale*200);;
params.bottomMargin=(int) (scale*200);;
params.height=height-(params.topMargin*2);
//底层
sideSlipParams.leftMargin=0;
}else{
params.rightMargin=-params.leftMargin ;
scale=Math.abs(params.leftMargin/S_Width);
params.height=height-(params.topMargin*2);
params.topMargin=(int) (scale*200);
params.bottomMargin=(int) (scale*200);
//底层
sideSlipParams.leftMargin=-(width-slip_swith-params.leftMargin);
sideSlipParams.topMargin=200-params.topMargin;
sideSlipParams.bottomMargin=200-params.bottomMargin;
sideSlipParams.height=height-(sideSlipParams.topMargin*2);
sideSlipParams.width=width-slip_swith-20;
}
}
v.setLayoutParams(params);
ll_bottom.setLayoutParams(sideSlipParams);
currentX = x2;
}
break;
case MotionEvent.ACTION_UP:
touchLong=System.currentTimeMillis()-touchLong;
int leftMargin=v.getLeft();
if(leftMargin<(v.getWidth()-slip_swith)/2){
smoothTo(v, 0);
}else{
smoothTo(v, width-slip_swith);
}
if(touchLong>150){
Log.d("tag", "-----touch------");
return false;
}else{
Log.d("tag", "-----click------");
return true;
}
default:
break;
}
return true;
}

private View controler_view;
private int finalX;
private void smoothTo(View v,int x){
controler_view=v;
finalX=x;
int left=v.getLeft();

if(left>x){
handler.sendEmptyMessage(0);
}else{
handler.sendEmptyMessage(1);
}
}

private Handler handler=new Handler(){
//1秒:
public void handleMessage(Message msg) {
if(msg.what==0){
int left=controler_view.getLeft();
if(left>finalX){//向左
left-=50;
if(left<finalX){
left=finalX;
}

LayoutParams params=(LayoutParams) controler_view.getLayoutParams();
params.leftMargin=left;
if(left<=0){
params.height=height;
params.topMargin=0;
params.bottomMargin=0;
}else{
params.height=height-(params.topMargin*2);
params.rightMargin=-params.leftMargin ;
scale=Math.abs(params.leftMargin/S_Width);
params.topMargin=(int) (scale*200);
params.bottomMargin=(int) (scale*200);
}
//底层
sideSlipParams.leftMargin=-(width-slip_swith-params.leftMargin);
sideSlipParams.topMargin=200-params.topMargin;
sideSlipParams.bottomMargin=200-params.bottomMargin;
sideSlipParams.height=height-(sideSlipParams.topMargin*2);
sideSlipParams.width=width-slip_swith-20;
controler_view.setLayoutParams(params);
ll_bottom.setLayoutParams(sideSlipParams);
handler.sendEmptyMessageDelayed(0, 25);
}
}else if(msg.what==1){
int left=controler_view.getLeft();
if(left<finalX){//向右
left+=50;
if(left>finalX){
left=finalX;
}
LayoutParams params=(LayoutParams) controler_view.getLayoutParams();
params.leftMargin=left;
params.rightMargin=-finalX;
scale=Math.abs(params.leftMargin/S_Width);
params.topMargin=(int) (scale*200);
params.bottomMargin=(int) (scale*200);
params.height=height-(params.topMargin*2);
//底层
sideSlipParams.leftMargin=-(width-slip_swith-params.leftMargin);
sideSlipParams.topMargin=200-params.topMargin;
sideSlipParams.bottomMargin=200-params.bottomMargin;
sideSlipParams.height=height-(sideSlipParams.topMargin*2);
sideSlipParams.width=width-slip_swith-20;
controler_view.setLayoutParams(params);
ll_bottom.setLayoutParams(sideSlipParams);
handler.sendEmptyMessageDelayed(1, 25);
}
}
};
};

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: