`

类似iphone滑动按钮效果

 
阅读更多

先上一张效果图:

以后大家在设计UI时,可以将那些开关型的功能模块使用上述UI原型进行改造:)


DragTab.java文件:
package com.example.ex.view;

import com.example.ex.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DragTab extends FrameLayout {

	private CallDragTab callTab;
	private int currentTab = -1;
	private TextView game;
	private TextView app;
	private TextView between;
	private int width;
	private OnClickListener gameOnClickListener;
	private OnClickListener appOnClickListener;
	private boolean right;
	private int widgetWidth;
	private int betweenLeft;
	private int firstX;

	public DragTab(Context context) {
		super(context);
		callTab = (CallDragTab) context;
	}

	public DragTab(Context context, AttributeSet attrs) {
		super(context, attrs);
		callTab = (CallDragTab) context;
	}

	public void setCurrentDrayTab(int curTab) {
		setCurrentDrayTab(curTab, false);
	}

	public void setGameText(int redId) {
		if (redId > 0) {
			this.game.setText(redId);
		}
	}

	public void setGameText(String text) {
		if (text != null && text.length() != 0) {
			this.game.setText(text);
		}
	}

	public void setAppText(int redId) {
		if (redId > 0) {
			this.app.setText(redId);
		}
	}

	public void setAppText(String text) {
		if (text != null && text.length() != 0) {
			this.app.setText(text);
		}
	}

	public void setBetweenText(int redId) {
		if (redId > 0) {
			this.between.setText(redId);
		}
	}

	public void setBetweenText(String text) {
		if (text != null && text.length() != 0) {
			this.between.setText(text);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		final int action = ev.getAction();
		final int moveX = (int) ev.getX();
		final int scape = moveX - firstX;
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			firstX = (int) ev.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			move(scape);
			break;
		case MotionEvent.ACTION_UP:
			if (currentTab == 1) {
				if (betweenLeft != 0) {
					animationStart(-betweenLeft, 0);
				}

				callTab.getData(1);
			} else if (currentTab == 2) {
				if (betweenLeft != width) {
					animationStart(betweenLeft, width);
				}
				callTab.getData(2);
			}
			break;
		}
		return true;
	}

	private void animationStart(int left, int leftMargin) {
		TranslateAnimation trans = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 0, Animation.ABSOLUTE,
				left / width, Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, 0);
		trans.setStartOffset(0);
		trans.setDuration(100);
		trans.setFillBefore(false);
		between.startAnimation(trans);
		setLayoutParams(leftMargin);
	}

	private void move(int scape) {
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) between
				.getLayoutParams();
		betweenLeft = between.getLeft();
		if (width <= scape) {
			lp.leftMargin = width;
		} else if (scape <= 0) {
			if (betweenLeft == width) {
				right = true;
			} else if (betweenLeft == 0) {
				right = false;
			}
			if (right) {
				lp.leftMargin = width + scape;
				if (lp.leftMargin <= 0) {
					lp.leftMargin = 0;
				}
			} else {
				lp.leftMargin = scape;
				if (lp.leftMargin <= 0) {
					lp.leftMargin = 0;
				}
			}
		} else if (scape > 0 && width > scape) {
			lp.leftMargin = scape;
			if (betweenLeft == width) {
				lp.leftMargin = width;
			}
		}
		if (widgetWidth / 3 <= betweenLeft) {
			setCurrentDrayTab(2);
		} else if (widgetWidth / 3 >= betweenLeft) {
			setCurrentDrayTab(1);
		}
		between.setLayoutParams(lp);
	}

	public void setCurrentDrayTab(int curTab, boolean isSetLayoutParams) {
		if (currentTab == curTab) {
			return;
		}
		currentTab = curTab;
		if (curTab == 1) {
			game.setVisibility(INVISIBLE);
			app.setVisibility(VISIBLE);
			between.setText(game.getText());
			if (isSetLayoutParams) {
				setLayoutParams(0);
			}
		} else if (curTab == 2) {
			app.setVisibility(INVISIBLE);
			game.setVisibility(VISIBLE);
			between.setText(app.getText());
			if (isSetLayoutParams) {
				setLayoutParams(width);
			}
		}
	}

	@Override
	protected void onFinishInflate() {
		game = (TextView) findViewById(R.id.game);
		app = (TextView) findViewById(R.id.app);
		between = (TextView) findViewById(R.id.between);
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) between
				.getLayoutParams();
		widgetWidth = lp.width;
		width = (int) (widgetWidth / 1.0);
	}

	private void setLayoutParams(int leftMargin) {
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) between
				.getLayoutParams();
		lp.leftMargin = leftMargin;
		between.setLayoutParams(lp);
	}

	public void setGameClickListener(OnClickListener listener) {
		if (listener != null) {
			this.gameOnClickListener = listener;
			this.game.setOnClickListener(gameOnClickListener);
		}
	}

	public void setAppClickListener(OnClickListener listener) {
		if (listener != null) {
			this.appOnClickListener = listener;
			this.app.setOnClickListener(appOnClickListener);
		}
	}

	public interface CallDragTab {
		void getData(int curTab);
	}
}



SlideButtonActivity.java:
package com.example.ex;

import com.example.ex.view.DragTab;
import com.example.ex.view.DragTab.CallDragTab;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class SlideButtonActivity extends Activity implements CallDragTab {

	private DragTab mDragTab;
	private int currentClick = -1;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mDragTab = (DragTab) findViewById(R.id.self_tab);
		mDragTab.setCurrentDrayTab(1);
		mDragTab.setGameClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mDragTab.setCurrentDrayTab(1, true);
				getData(1);
			}
		});
		mDragTab.setAppClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mDragTab.setCurrentDrayTab(2, true);
				getData(2);
			}
		});

		getData(1);
	}

	@Override
	public void getData(int curTab) {
		if (currentClick == curTab) {
			return;
		}
		currentClick = curTab;
		if (curTab == 1) {
			Toast.makeText(this, "游戏", Toast.LENGTH_LONG).show();
		} else if (curTab == 2) {
			Toast.makeText(this, "应用", Toast.LENGTH_LONG).show();
		}

	}
}


main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<include layout="@layout/self_tab" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:id="@+id/self_tab" />
</LinearLayout>


self_tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.example.ex.view.DragTab
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="200dp" android:layout_height="wrap_content">
	<LinearLayout android:layout_width="200dp"
		android:layout_height="wrap_content" android:background="@drawable/rank_long">
		<TextView android:id="@+id/game" android:layout_width="100dp"
			android:layout_height="wrap_content" android:text="游戏"
			android:textSize="16sp" android:textColor="#000000" android:gravity="center_horizontal"
			android:layout_gravity="center" />
		<TextView android:id="@+id/app" android:layout_width="100dp"
			android:layout_height="wrap_content" android:text="应用"
			android:textSize="16sp" android:textColor="#000000" android:gravity="center_horizontal"
			android:layout_gravity="center" />
	</LinearLayout>
	<LinearLayout android:layout_width="200dp"
		android:layout_height="wrap_content">
		<TextView android:id="@+id/between" android:layout_width="100dp"
			android:layout_height="wrap_content" android:background="@drawable/rank_short"
			android:text="游戏" android:textSize="16sp" android:textColor="#000000"
			android:gravity="center" />
	</LinearLayout>
</com.example.ex.view.DragTab>


本来不想附上源码的,因为代码都贴在这了,还是附上吧,也许有的人需要现成的东东!!!!
  • 大小: 9.3 KB
分享到:
评论
8 楼 龙哥IT 2012-06-27  
7 楼 yangchch786 2011-12-11  
学习了,有个小bug,就是当向左拖动时,scape<0时出现的bug,已经改过
发一下出来。
if(widgetWidth/3 <= lp.leftMargin){
setCurrentDrayTab(2);
}else if(widgetWidth / 3 >= lp.leftMargin){
setCurrentDrayTab(1);
}
这里需要用lp.leftMargin,不用betweenLeft。

if(right){
lp.leftMargin = width + scape;
if(lp.leftMargin<=0){
lp.leftMargin = 0;
}
}else{
lp.leftMargin = 0;
}
这里当right==false时,就把lp.leftMargin设置为0.
6 楼 qjx1987904 2011-12-03  
很不错,学习了
5 楼 bear1122ccc 2011-10-08  
private int width; 这个宽度是代表什么啊?
private int currentTab = -1; 这个又代表哪种状态啊?
4 楼 bear1122ccc 2011-10-08  
求代码注释。
3 楼 huanzi5566 2011-09-05  
哇  真厉害
2 楼 lemonboxs 2011-08-19  
很好,解决了我的问题。
1 楼 pcq019 2011-08-17  
48个浏览,居然没人回复,我来谢谢你啦~

相关推荐

    类似Iphone日期选择器功能

    类似Iphone日期选择器功能,该项目实现了类似于Iphone中齿轮状的日期选择器,可以通过点击show picker按钮,会弹出一个对话框,该对话框中的内容即是日期,可以上下滑动进行切换时间,喜欢的朋友可以下载研究一下。

    Vue实现仿iPhone悬浮球的示例代码

    悬浮球插件简单的效果图: 很遗憾,没找到太好的视频转...-- 给定一个初始位置position,插槽中填写想滑动的部分 --&gt; 猪场&gt; 原理示意图 请结合代码注释来理解 悬浮球插件代码如下: &lt;div class=xuan

    KHSwipeCell:可滑动单元格

    一个易于使用的 UITableViewCell 子类,它实现了一个可滑动的内容视图,它公开了实用程序按钮(类似于 iOS 7 邮件应用程序) #演示图片 用法 在您的 Podfile 中: pod 'KHSwipeCell', '~&gt; 0.2.0' 或者只是克隆这...

    iPhone开发秘籍.part2.rar

    8.3 秘诀:为按钮响应制作动画效果.....196 8.4 秘诀:定制开关.....197 8.5 秘诀:添加自定义滑块缩略图.....200 8.6 秘诀:关闭UITextField 键盘.....204 8.7 秘诀:关闭UITextView 键盘.....205 8.8 秘诀:向文本...

    iPhone开发秘籍.part1.rar

    8.3 秘诀:为按钮响应制作动画效果.....196 8.4 秘诀:定制开关.....197 8.5 秘诀:添加自定义滑块缩略图.....200 8.6 秘诀:关闭UITextField 键盘.....204 8.7 秘诀:关闭UITextView 键盘.....205 8.8 秘诀:向文本...

    iPhone开发秘籍.part4.rar

    8.3 秘诀:为按钮响应制作动画效果.....196 8.4 秘诀:定制开关.....197 8.5 秘诀:添加自定义滑块缩略图.....200 8.6 秘诀:关闭UITextField 键盘.....204 8.7 秘诀:关闭UITextView 键盘.....205 8.8 秘诀:向文本...

    Cocos2D-iPhone游戏开发教程打包整理-(泰然论坛整理)

    (译)在cocos2d里如何制作各种按钮.pdf (译)如何使用cocos2d制作基于tilemap的游戏教程 第一部分.pdf (译)如何在cocos2d里面使用动画和spritesheet.pdf (译)如何用cocos2d制作iphone游戏:旋转炮塔.pdf (译)如何...

    st-slidetoremove-plugin:滑动删除 Sencha 触摸列表插件

    st-slidetoremove-插件滑动删除 Sencha 触摸列表插件SlideToRemove 是 Sencha Touch 2 的一个简单插件,它增加了滑动列表项以在确认后删除它们的功能,非常类似于 iPhone/iPad 上看到的本机 iOS 控件。 有一个支持 ...

    Bar Track ball Item(iPhone源代码)

    在弹出键盘上加上一个按钮,手指按下按钮,并且保持按钮处于按下状态,然后随便在屏幕上滑动手指,便可以任意定位文字光标。 小编注:这一解决方式很类似笔记本电脑键盘上的TrackPoint,用于定位屏幕鼠标。 [优才 ...

    JQuery&CSS;&CSS;+DIV实例大全.rar

    10.jquery仿卓越亚马逊网鼠标移到按钮弹出菜单的效果代码 11.jquery浮动变化的个性菜单插件floatmenu下载 12.jQuery黑色风格仿Flash版下滑菜单效果 13.jquery黑色循环滚动菜单特效插件下载 14.jquery灰色...

    vue实现移动端悬浮窗效果

    本文讲述,在使用VUE的移动端实现类似于iPhone的悬浮窗的效果。 相关知识点 touchstart 当在屏幕上按下手指时触发 touchmove 当在屏幕上移动手指时触发 touchend 当在屏幕上抬起手指时触发 mousedown mousemove ...

    (0055)-iOS/iPhone/iPAD/iPod源代码-视图布局(View Layout)-Menu Controller

    类似于Path或Facebook应用的效果,允许多个视图左右平移切换。主视图的导航条有两个按钮,点击左边按钮,主视图会往右移动大概4/5,左边4/5的屏幕显示底下的子视图。点击右边按钮,则右边屏幕显示底下另外一个子视图...

    Cocos2D游戏开发教程打包整理-(泰然论坛整理)

    (译)在cocos2d里如何制作各种按钮.pdf (译)如何使用cocos2d制作基于tilemap的游戏教程 第一部分.pdf (译)如何在cocos2d里面使用动画和spritesheet.pdf (译)如何用cocos2d制作iphone游戏:旋转炮塔.pdf (译)如何用...

    子龙山人翻译的所有ios文章

    (译)在cocos2d里如何制作各种按钮.pdf (译)如何使用cocos2d制作基于tilemap的游戏教程 第一部分.pdf (译)如何在cocos2d里面使用动画和spritesheet.pdf (译)如何用cocos2d制作iphone游戏:旋转炮塔.pdf (译)如何用...

    左右菜单Demo(iPhone源代码)

    来源:Licence:Custom平台:iOS设备:iPhone作者:程康_kain  左右菜单例子,类似于...点击主视图的导航条左右两边按钮,就会从左右两边分别滑动出子视图。 Code4App编译测试,适用环境:Xcode 4.3, iOS 5.0。

    来电通 安卓官方正式版

    来电通独家对类似+020110的冒警电话进行了骚扰来电提醒。 如果没被拦截的骚扰来电,骚扰短信,也无需烦恼哦,轻轻一个举报,它便不会再骚扰你啦。 爆点三:全新界面UI,绝对让你眼前一亮。 全新的UI,全新的体验,...

Global site tag (gtag.js) - Google Analytics