自定义viewgroup 继承 FrameLayout 仿QQ侧滑菜单

自定义viewgroup  继承了 framelayout  来完成QQ侧滑菜单这个效果


贴上代码


package com.myfarmelayout.myviews;

import android.content.Context;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.nineoldandroids.view.ViewHelper;

public class MyFrameLayout extends FrameLayout{

	private int screen_width;
	private float leftMinTransX,rightMinTransX;
	private View left,right;
	private boolean isLeftShowing = false;
	private int x = 0;
	private int maxLeft = 0;
	private int nowLeft = 0;
	private final int MODE_SHOWLEFT = 0;
	private final int MODE_DISSLEFT = 1;
	
	public MyFrameLayout(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public MyFrameLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public MyFrameLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		// TODO Auto-generated method stub
		super.onLayout(changed, left, top, right, bottom);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
	
	public void setScreenWidth(int screenWidth, int leftWidth){
		this.screen_width = screenWidth;
		maxLeft = leftWidth;
		leftMinTransX = (maxLeft * (float)0.4 ) / 2;
		rightMinTransX = maxLeft - (screenWidth * (float)0.1);
	}
	
	public void setViews(View left,View right){
		this.left = left;
		this.right = right;
		ViewHelper.setScaleX(left, 0.6f);
		ViewHelper.setScaleY(left, 0.5f);
		ViewHelper.setTranslationX(left, -leftMinTransX);
		nowLeft = 0;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		switch(event.getAction()){
			case MotionEvent.ACTION_DOWN :{
				x = (int) event.getX();
				break;
			}
			case MotionEvent.ACTION_MOVE :{
				int nowX = (int) event.getX();
				int cha = nowX - x;
				move(cha);
				x = nowX;
				break;
			}
			case MotionEvent.ACTION_UP :{
				up();
				break;
			}
		}
		return true;
	}
	
	private void move(int x){
		nowLeft += x;
		if(nowLeft < 0){
			nowLeft = 0;
			return;
		}
		else if(nowLeft > maxLeft){
			nowLeft = maxLeft;
			return;
		}
		//右侧缩放比例  0.8 ~1 
		float rx = (1 - ( ((float)nowLeft / maxLeft) * 0.2f));
		//右侧x移动距离 0 ~ rightMinTransX
		float rightx = (((float)nowLeft / maxLeft) * rightMinTransX);
		//左侧缩放比例 0.6 ~ 1
		float l = (0.6f + ( ((float)nowLeft / maxLeft) * 0.4f));
		//左侧X移动距离 -leftMinTransX ~ 0
		float lx = (-leftMinTransX + ( ((float)nowLeft / maxLeft) * leftMinTransX));
		//透明度 0 ~ 1
		float al = (((float)nowLeft / maxLeft) );
		
		left.setAlpha(al);
		ViewHelper.setScaleY(left, l);
		left.setTranslationX(lx);
		ViewHelper.setScaleX(left, l);
		
		ViewHelper.setScaleY(right, rx);
		ViewHelper.setScaleX(right, rx);
		right.setTranslationX(rightx);
	}
	
	private void up(){
		if(nowLeft > maxLeft / 2){
			new MyTask().execute(MODE_SHOWLEFT,nowLeft);
		}
		else{
			new MyTask().execute(MODE_DISSLEFT,nowLeft);
		}
	}
	
	private void showLeft(){
		ViewHelper.setScaleY(left, 1f);
		ViewHelper.setScaleX(left, 1f);
		left.setTranslationX(0);
		left.setAlpha( 1f);
		
		ViewHelper.setScaleY(right, 0.8f);
		ViewHelper.setScaleX(right, 0.8f);
		right.setTranslationX(rightMinTransX);
		nowLeft = maxLeft;
		isLeftShowing = true;
	}
	
	private void dissLeft(){
		ViewHelper.setScaleY(right, 1f);
		ViewHelper.setScaleX(right, 1f);
		right.setTranslationX(0);
		
		ViewHelper.setScaleY(left, 0.6f);
		ViewHelper.setScaleX(left, 0.6f);
		left.setTranslationX(-leftMinTransX);
		left.setAlpha( 0f);
		nowLeft = 0;
		isLeftShowing = false;
	}
	
	private class MyTask extends AsyncTask{

		@Override
		protected Integer doInBackground(Integer... params) {
			// TODO Auto-generated method stub
			int mode = params[0];
			int nowleft = params[1];
			int cha = 0;
			if(mode == MODE_DISSLEFT){
				cha = (0 - nowleft) / 50;
			}
			else{
				cha = (maxLeft - nowleft) / 50;
			}
			int i = 0;
			while(i < 50){
				publishProgress(cha);
				try{
					Thread.sleep(2);
				}catch(Exception e){
					
				}
				i++;
			}
			return mode;
		}
		
		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			move(values[0]);
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			// TODO Auto-generated method stub
			int mode = result;
			if(mode == MODE_SHOWLEFT){
				showLeft();
			}
			else{
				dissLeft();
			}
		}
	}
}


其中用到了ViewHelper 动画效果,jar包下载链接:

http://download.csdn.net/detail/wwwbjj1988/8948627


源码链接  

http://download.csdn.net/detail/wwwbjj1988/8948633

你可能感兴趣的:(android)