首页 > 其他分享 >左边和右边都可拖出页面的效果

左边和右边都可拖出页面的效果

时间:2023-04-06 21:40:22浏览次数:32  
标签:右边 左边 stub width animation Override 页面 TODO panel


这个是上一篇的加强版,现在实现的软件并不是很多。其实第一个搞懂的话,这个就呼之欲出了。现全部公开源码:

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class SlideBothSideActivity extends BaseActivity implements OnTouchListener,OnGestureListener{
	private Context context;
	private Button btn_left,btn_right;
	private ViewGroup panel_left,panel_right,panel_mid;
	private ListView listViewLeft,listViewRight,listViewMid;
	private final int duration=400;
	private int width=400;//滑动的距离
	private boolean isShowingLeft=false;
	private boolean isShowingRight=false;
	
	private GestureDetector mGestureDetector;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slide_both_side);
        context=this;
        initViews();
    }

	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initValues();
	}

	@Override
	protected void updateViews(Object o) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void initViews() {
		// TODO Auto-generated method stub
		width=(int)(getResources().getDisplayMetrics().widthPixels*0.8);
		Log.i("tag", "width="+width);
		btn_left=(Button)findViewById(R.id.btn_left);
		btn_right=(Button)findViewById(R.id.btn_right);
		btn_left.setOnClickListener(onClickListener);
		btn_right.setOnClickListener(onClickListener);
		
		//定义手势识别  
		mGestureDetector = new GestureDetector(this,this);  
		mGestureDetector.setIsLongpressEnabled(false);
		
		panel_left=(ViewGroup)findViewById(R.id.panel_left);
		panel_right=(ViewGroup)findViewById(R.id.panel_right);
		panel_mid=(ViewGroup)findViewById(R.id.panel_mid);
		panel_mid.setOnTouchListener(this);
		listViewLeft=(ListView)findViewById(R.id.listViewLeft);
		listViewRight=(ListView)findViewById(R.id.listViewRight);
		listViewMid=(ListView)findViewById(R.id.listViewMid);
		ArrayList<String> texts=new ArrayList<String>();
  		texts.add("111");
  		texts.add("222");
  		texts.add("333");
  		texts.add("444");
  		texts.add("555");
  		texts.add("666");
  		texts.add("777");
  		ListViewAdapter adapterMid=new ListViewAdapter(texts);
  		listViewMid.setAdapter(adapterMid);
  		/**让ListView不拦截手势滑动*/
  		listViewMid.setOnTouchListener(new View.OnTouchListener(){

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				doSlideWhenTouchUp(event);
				mGestureDetector.onTouchEvent(event);
				return false;
			}
  			
  		});
  		listViewMid.setOnItemClickListener(new ListView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Log.i("tag", "position=="+position);
			}
		});
  		
  		ListViewAdapter adapterLeft=new ListViewAdapter(texts);
  		listViewLeft.setAdapter(adapterLeft);
  		ListViewAdapter adapterRight=new ListViewAdapter(texts);
  		listViewRight.setAdapter(adapterRight);
	}

	View.OnClickListener onClickListener=new View.OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_left:
				if(isShowingLeft){
					doSlideCloseLeftAnimation(panel_mid,width);
				}else{
					doSlideOpenLeftAnimation(panel_mid,width);
				}
				break;
			case R.id.btn_right:
				if(isShowingRight){
					doSlideCloseRightAnimation(panel_mid,width);
				}else{
					doSlideOpenRightAnimation(panel_mid,width);
				}
				break;

			default:
				break;
			}
		}
		
	};
	
	@Override
	protected void initValues() {
		// TODO Auto-generated method stub
	}

	@Override
	protected void initHandler() {
		// TODO Auto-generated method stub

	}
	
	
	private void doSlideOpenLeftAnimation(View v,int width){
		TranslateAnimation animation = new TranslateAnimation(0, width, 0, 0);
		animation.setInterpolator(new LinearInterpolator());
		animation.setDuration(duration);
		animation.setFillAfter(true);
		v.startAnimation(animation);
		animation.setAnimationListener(new AnimationListener(){

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				panel_left.setVisibility(View.VISIBLE);
				panel_right.setVisibility(View.GONE);
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				resetMidLayout(SlideBothSideActivity.this.width,0);
                isShowingLeft=true;
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
	private void doSlideCloseLeftAnimation(View v,int width){
		TranslateAnimation animation = new TranslateAnimation(0, -width, 0, 0);
		animation.setInterpolator(new LinearInterpolator());
		animation.setDuration(duration);
		animation.setFillAfter(true);
		v.startAnimation(animation);
		animation.setAnimationListener(new AnimationListener(){

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				resetMidLayout(0,0);
				isShowingLeft=false;
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
	
	private void resetMidLayout(int width,int height){
		AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams)panel_mid.getLayoutParams();
		params.x=width;
		params.y=height;
		panel_mid.setLayoutParams(params);
		panel_mid.clearAnimation();
	}
	
	private void doSlideOpenRightAnimation(View v,final int width){
		TranslateAnimation animation = new TranslateAnimation(0, -width, 0, 0);
		animation.setInterpolator(new LinearInterpolator());
		animation.setDuration(duration);
		animation.setFillAfter(true);
		v.startAnimation(animation);
		animation.setAnimationListener(new AnimationListener(){

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				panel_right.setVisibility(View.VISIBLE);
				panel_left.setVisibility(View.GONE);
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				resetMidLayout(-SlideBothSideActivity.this.width,0);
				isShowingRight=true;
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
	private void doSlideCloseRightAnimation(View v,final int width){
		TranslateAnimation animation = new TranslateAnimation(0, width, 0, 0);
		animation.setInterpolator(new LinearInterpolator());
		animation.setDuration(duration);
		animation.setFillAfter(true);
		v.startAnimation(animation);
		animation.setAnimationListener(new AnimationListener(){
			
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				resetMidLayout(0,0);
				isShowingRight=false;
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return super.onKeyDown(keyCode, event);
	}
	
	 
	class ListViewAdapter extends BaseAdapter{

		private ArrayList<String> list;
		public ListViewAdapter(ArrayList<String> list){
			this.list=list;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(
						R.layout.simple_item_1_for_listview, null);
			}
			TextView tv0=(TextView)convertView.findViewById(R.id.simple_item_0);
			tv0.setText(list.get(position));
			return convertView;
		}
		
	}


	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return true;
	}


	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	private int mScrollx;
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
//		Log.i("tag", "distanceX="+distanceX);
		mScrollx -= distanceX;//distanceX:向左为正,右为负
		AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams)panel_mid.getLayoutParams();
		params.x+=mScrollx;
		/**判断显示哪个部分*/
		if(params.x>=0){
			panel_left.setVisibility(View.VISIBLE);
			panel_right.setVisibility(View.GONE);
		}else if(params.x<0){
			panel_right.setVisibility(View.VISIBLE);
			panel_left.setVisibility(View.GONE);
		}
//		else if(params.x==0){
//			panel_left.setVisibility(View.GONE);
//			panel_right.setVisibility(View.GONE);
//		}
		/**边界判断*/
		if(params.x>width){//往右拖
			params.x=width;
		}
		if(params.x<-width){//往左拖
			params.x=-width;
		}
		panel_mid.setLayoutParams(params);
		/**完成后触发onTouch函数*/
		return false;
	}


	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		/** 手势快速滑动,打开/关闭panel。现已被onScroll()完成同样的功能 */
		/*if(!isShowingLeft&&!isShowingRight){//都没打开
			if(velocityX>500&&panel_mid.getLeft()==0){
				doSlideOpenLeftAnimation(panel_mid, width);
			}else if(velocityX<-500&&panel_mid.getLeft()==0){
				doSlideOpenRightAnimation(panel_mid, width);
			}
		}else if(isShowingLeft&&!isShowingRight){//左边的打开着
			if(velocityX<-500&&panel_mid.getLeft()==width){
				doSlideCloseLeftAnimation(panel_mid, width);
			}
		}else if(!isShowingLeft&&isShowingRight){//右边的打开着
			if(velocityX>500&&panel_mid.getLeft()==-width){
				doSlideCloseRightAnimation(panel_mid, width);
			}
		}*/
		return false;
	}


	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		doSlideWhenTouchUp(event);
		return mGestureDetector.onTouchEvent(event);
	}
	/**当手指离开时执行响应的动画。如果使用AdapterView需要也让该函数响应。*/
	private void doSlideWhenTouchUp(MotionEvent event){
		switch (event.getAction()) {
		case MotionEvent.ACTION_UP:
			int scrollDistance=width>>3;//拖动的距离值,可根据实际效果调节
			AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams)panel_mid.getLayoutParams();
			if(!isShowingLeft&&!isShowingRight){//都没打开
				/** 手势拖动到一 半松手时,根据拖动的距离判断打开/关闭 */
				if (params.x >= scrollDistance) {//往右
					doSlideOpenLeftAnimation(panel_mid, width-params.x);
				}else if(params.x < scrollDistance&¶ms.x>=0){
					doSlideCloseLeftAnimation(panel_mid, params.x);
				}
				else if(params.x <= -scrollDistance){//往左
					doSlideOpenRightAnimation(panel_mid, width+params.x);
				}else if(params.x > -scrollDistance&¶ms.x<0){
					doSlideCloseRightAnimation(panel_mid, -params.x);
				}
			}else if(isShowingLeft&&!isShowingRight){//左边的打开着
				if (params.x >= width-scrollDistance) {//往右拖没超过scorllDistance
					doSlideOpenLeftAnimation(panel_mid, width-params.x);
				}else if(params.x < width-scrollDistance){
					doSlideCloseLeftAnimation(panel_mid, params.x);
				}
			}else if(!isShowingLeft&&isShowingRight){//右边的打开着
				if (params.x <= -(width-scrollDistance)) {//往左拖没超过scorllDistance
					doSlideOpenRightAnimation(panel_mid, width+params.x);
				}else if(params.x > -(width-scrollDistance)){
					doSlideCloseRightAnimation(panel_mid, -params.x);
				}
			}
		default:
			break;
		}
	}
	
	
}



布局:


<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    >

    <LinearLayout
        android:id="@+id/panel_left"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:layout_x="0dp"
        android:layout_y="0dp"
        android:visibility="gone"
        android:background="@color/bg_blue_1"
        >
        <ListView 
	    android:id="@+id/listViewLeft"
	    android:layout_width="fill_parent"
    	android:layout_height="fill_parent" 
    	android:cacheColorHint="@android:color/transparent"
	    />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/panel_right"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:layout_x="0dp"
        android:layout_y="0dp"
        android:visibility="gone"
        android:background="@color/bg_blue_3"
        >
        <ListView 
	    android:id="@+id/listViewRight"
	    android:layout_width="fill_parent"
    	android:layout_height="fill_parent" 
    	android:cacheColorHint="@android:color/transparent"
	    />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/panel_mid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:background="@color/green"
        android:layout_x="0dp"
        android:layout_y="0dp"
        android:visibility="visible"
        >
        <RelativeLayout 
            android:layout_width="fill_parent"
        	android:layout_height="wrap_content"
            >
        <Button
            android:id="@+id/btn_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="left" 
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            />
        <Button
            android:id="@+id/btn_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="right" 
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            />
            
        </RelativeLayout>

        <ListView
            android:id="@+id/listViewMid"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="@android:color/transparent" />
    </LinearLayout>

</AbsoluteLayout>






标签:右边,左边,stub,width,animation,Override,页面,TODO,panel
From: https://blog.51cto.com/u_5454003/6174154

相关文章

  • 左边可拖出菜单(页面)效果
    这个效果现在很多软件都实现了,其实说穿了很简单的。就是一个动画,做的地道点的加一个手势拖拉效果。我写的代码(未参考任何代码,不知道别人怎么实现的。):importjava.util.ArrayList;importandroid.content.Context;importandroid.os.Bundle;importandroid.util.Log;import......
  • HTML页面背景渐变
    设置HTML背景颜色渐变可以用很多方法诸如1.用PS画张1400*2000的渐变图片,在BODY中设置为背景图片2.用JS写3.用CSS样式 我这里介绍第3种,-----------------------------直接贴出HTML代码-------------------------------<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional/......
  • winCE 页面绘制 例子一则
     效果如下:   usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Reflection;usingSystem.Drawing.Imaging;n......
  • vue3中路由错误自动跳转404页面 路由表写法
    定义路由表import{createRouter,createWebHashHistory}from"vue-router";constroutes=[ { path:"/", name:"home", component:Home, },//... { path:"/404", name:"404", component:()=&......
  • [K/3Cloud] 首页增加一个自定义页签及页面内容
    在K3Cloud登录后的门户首页增加一个页签,如增加一个【BBS论坛】 2013-7-3011:18:51上传下载附件 (84.81KB) 增加页签   可以这么来做:进入BOSIDE,找到名称为主控台经典版,唯一标识为BOS_MainConsoleSutra的动态表单;为它写一个继承自AbstractDynamicFormPl......
  • Selenium-处理弹窗弹出新页面的切换问题
    WebDriverWaitwait=newWebDriverWait(driver,TimeSpan.FromSeconds(60));wait.Until(driver=>{returndriver.WindowHandles.Count==waitforHandlesCount;});stringcurrent_handler=driver.Curren......
  • uni-app:ios/android中的nvue和vue页面加载自定义字体(hbuilderx 3.7.3)
    一,官方文档地址:https://uniapp.dcloud.net.cn/tutorial/nvue-api.html#addrule二,代码1,nvue页面:模板<viewclass="listTitle">{{item.title}}</view>......
  • EasyUI闪屏,EasyUI页面加载提示:原理+代码+效果图
    使用EasyUI时,有个经常遇到的问题,页面还没有渲染完成的时候,就展现了。刚刚开始很混乱,等加载完成后,就好了。    $.parser.onComplete,这个是在所有组件解析完成后执行的事件。其实这个事件很有用的。很多在布局用到easyui的时候总会出现一个问题。就是在一进入主界面的时候,页......
  • 页面滚动时固定菜单栏
    往下拖动页面滚动条时,固定住菜单栏(不随其他内容网上滚动,兼容各版本IE、Chrome、Firefox)<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml&qu......
  • loadrunner 录制脚本 无法打开页面
    问题:使用loadrunner 12.55录制脚本时,可以启动浏览器,但是浏览器内容一直加载不出来,页面空白 解决方案:1.开始录制前设置Recordingoptions--->httppropterties---->Advanced--->勾选 userlocalLoadrunnerproxyto。。。。。......