`
itoracja
  • 浏览: 142746 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Android 左右滑动 控件

    博客分类:
  • java
阅读更多
    先看效果:


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">	<textview android:id="@+id/textview" android:layout_width="fill_parent"		android:layout_height="wrap_content" android:text="ddd" />	<com.diydyq.android.swipetest.slipview		android:id="@+id/slipview" android:orientation="vertical"		android:layout_width="fill_parent" android:layout_height="fill_parent">	</com.diydyq.android.swipetest.slipview></linearlayout>


slipentity.java
package com.iaiai.activity;import java.util.hashmap;import java.util.map;import android.graphics.bitmap;import android.util.log;/** *  * <p> * title: slipentity.java * </p> * <p> * e-mail: 176291935@qq.com * </p> * <p> * qq: 176291935 * </p> * <p> * http: iaiai.iteye.com * </p> * <p> * create time: 2011-8-11 * </p> *  * @author 丸子 * @version 0.0.1 */public class slipentity {	public static final int flag_back = 1;	public static final int flag_frot = 2;	public static final float dock_l = 0;	public static final float dock_m = (float) 0.5;	public static final float dock_r = 1;	public static final float micro_x = 10;	/** background image */	bitmap backimage;	/** front image */	bitmap frotimage;	/** start position of back image */	float xbackstart;	/** start position of back image */	float ybackstart;	/** start position of front image */	float xfrotstart;	/** start position of front image */	float yfrotstart;	/** initial position of front image */	float xfrotinitpos;	/** initial position of front image */	float yfrotinitpos;	/** margin of front and back image in x-axis */	float xmarginleft;	/** margin of front and back image in y-axis */	float ymargintop;	/** containing dock position of the front image */	map<float, float> dockposlist = new hashmap<float, float>();	/** current dock percentage: dock_l | dock_m | dock_r */	float curdockper = dock_l;	/** weather has invoked initslipentity() */	boolean isinit = false;	public slipentity() {	}	public slipentity(bitmap backimage, bitmap frotimage) {		this.backimage = backimage;		this.frotimage = frotimage;	}	public slipentity(float xbackstart, float ybackstart, float xfrotstart,			float yfrotstart) {		this.xbackstart = xbackstart;		this.ybackstart = ybackstart;		this.xfrotstart = xfrotstart;		this.yfrotstart = yfrotstart;	}	public void initslipentity(float viewwidth, float viewheight) {		this.xbackstart = (viewwidth - this.backimage.getwidth()) / 2;		this.ybackstart = (viewheight - this.backimage.getheight()) / 2;		this.xmarginleft = 5;		this.ymargintop = (this.backimage.getheight() - this.frotimage				.getheight()) / 2;		this.xfrotinitpos = this.xbackstart + this.xmarginleft;		this.yfrotinitpos = this.ybackstart + this.ymargintop;		this.xfrotstart = this.xfrotinitpos;		this.yfrotstart = this.yfrotinitpos;		// add dock position		float dockl = this.xfrotinitpos;		float dockr = this.xbackstart + this.backimage.getwidth()				- this.frotimage.getwidth() - this.xmarginleft;		this.dockposlist.put(dock_l, dockl);		this.dockposlist.put(dock_r, dockr);		for (float dockper : this.dockposlist.keyset()) {			if (this.dockposlist.get(dockper) == 0) {				float docpos = (dockr - dockl) * dockper						+ this.dockposlist.get(dock_l);				this.dockposlist.put(dockper, docpos);			}		}		// dock at current position		this.xfrotstart = this.dockposlist.get(this.curdockper);		this.isinit = true;		// for debug information		stringbuilder sb = new stringbuilder();		sb.append("backimagew:" + this.backimage.getwidth() + "\n");		sb.append("backimageh:" + this.backimage.getheight() + "\n");		sb.append("frotimagew:" + this.frotimage.getwidth() + "\n");		sb.append("frotimageh:" + this.frotimage.getheight() + "\n");		sb.append("xbackstart:" + xbackstart + "\n");		sb.append("ybackstart:" + ybackstart + "\n");		sb.append("xmarginleft:" + xmarginleft + "\n");		sb.append("ymargintop:" + ymargintop + "\n");		sb.append("xfrotinitp:" + xfrotinitpos + "\n");		sb.append("yfrotinitp:" + yfrotinitpos + "\n");		sb.append("xfrotstart:" + xfrotstart + "\n");		sb.append("yfrotstart:" + yfrotstart + "\n");		log.v("slipentity", sb.tostring());	}	/**	 * weather the front image reaches the max right of background image, if	 * true, set xfrotstart to max right.	 * 	 * @return	 */	public boolean isreachright() {		if (this.xfrotstart > this.dockposlist.get(dock_r)) {			this.curdockper = dock_r;			this.xfrotstart = this.dockposlist.get(dock_r);			return true;		} else {			return false;		}	}	/**	 * weather the front image reaches the max left of background image, if	 * true, set xfrotstart to max left.	 * 	 * @return	 */	public boolean isreachleft() {		if (this.xfrotstart < this.dockposlist.get(dock_l)) {			this.curdockper = dock_l;			this.xfrotstart = this.dockposlist.get(dock_l);			return true;		} else {			return false;		}	}	/**	 * weather the point(x,y) is in the area of back or front image	 * 	 * @param type	 *            flag_frot(front image) | flag_back(back image)	 * @param x	 *            x-coordinate of point	 * @param y	 *            y-coordinate of point	 * @return weather the point is in specified area	 */	public boolean ispointinimage(int type, float x, float y) {		float rpointx;		float rpointy;		switch (type) {		case flag_frot:			rpointx = this.xfrotstart + this.frotimage.getwidth();			rpointy = this.yfrotstart + this.frotimage.getheight();			if (x > this.xfrotstart &amp;&amp; y > this.yfrotstart &amp;&amp; x < rpointx					&amp;&amp; y < rpointy)				return true;			else				return false;		case flag_back:			rpointx = this.xbackstart + this.backimage.getwidth();			rpointy = this.ybackstart + this.backimage.getheight();			if (x > this.xbackstart &amp;&amp; y > this.ybackstart &amp;&amp; x < rpointx					&amp;&amp; y < rpointy)				return true;			else				return false;		default:			return false;		}	}	/**	 * is the current touch in some dock position	 * 	 * @return return dockper if in, or -1 while no match	 */	public float isdock() {		for (float dockper : this.dockposlist.keyset()) {			float dockpos = this.dockposlist.get(dockper);			if (this.xfrotstart > dockpos - micro_x					&amp;&amp; this.xfrotstart < dockpos + micro_x) {				this.curdockper = dockper;				return dockper;			}		}		return -1;	}	/**	 * get the current dock percentage in x-axis	 * 	 * @return	 */	public float getcurdockper() {		return this.curdockper;	}	/**	 * get the current dock position in x-axis	 * 	 * @return	 */	public float getcurdockpos() {		return this.dockposlist.get(this.curdockper);	}	/**	 * add dock position to the list	 * 	 * @param dockper	 *            dock percent should be between (0.0,1.0)	 */	public void adddockpos(float dockper) {		if (dockper > 0 &amp;&amp; dockper < 1) {			this.dockposlist.put(dockper, (float) 0.0);		}	}	/**	 * return width of background image	 * 	 * @return	 */	public float getbackwidth() {		return this.backimage.getwidth();	}	/**	 * return height of background image	 * 	 * @return	 */	public float getbackheight() {		return this.backimage.getheight();	}	/**	 * return width of front image	 * 	 * @return	 */	public float getfrotwidth() {		return this.frotimage.getwidth();	}	/**	 * return height of front image	 * 	 * @return	 */	public float getfrotheight() {		return this.frotimage.getwidth();	}	/**	 * dock at some position	 * 	 * @param curdockper	 */	public void setcurdockpos(float curdockper) {		this.curdockper = curdockper;	}	public bitmap getbackimage() {		return backimage;	}	public void setbackimage(bitmap backimage) {		this.backimage = backimage;	}	public bitmap getfrotimage() {		return frotimage;	}	public void setfrotimage(bitmap frotimage) {		this.frotimage = frotimage;	}	public float getxbackstart() {		return xbackstart;	}	public void setxbackstart(float xbackstart) {		this.xbackstart = xbackstart;	}	public float getybackstart() {		return ybackstart;	}	public void setybackstart(float ybackstart) {		this.ybackstart = ybackstart;	}	public float getxfrotstart() {		return xfrotstart;	}	public void setxfrotstart(float xfrotstart) {		this.xfrotstart = xfrotstart;	}	public float getyfrotstart() {		return yfrotstart;	}	public void setyfrotstart(float yfrotstart) {		this.yfrotstart = yfrotstart;	}	public float getxfrotinitpos() {		return xfrotinitpos;	}	public void setxfrotinitpos(float xfrotinitpos) {		this.xfrotinitpos = xfrotinitpos;	}	public float getyfrotinitpos() {		return yfrotinitpos;	}	public void setyfrotinitpos(float yfrotinitpos) {		this.yfrotinitpos = yfrotinitpos;	}	public float getxmarginleft() {		return xmarginleft;	}	public void setxmarginleft(float xmarginleft) {		this.xmarginleft = xmarginleft;	}	public float getymargintop() {		return ymargintop;	}	public void setymargintop(float ymargintop) {		this.ymargintop = ymargintop;	}	public map<float, float> getdockposlist() {		return dockposlist;	}	public void setdockposlist(map<float, float> dockposlist) {		this.dockposlist = dockposlist;	}	public boolean isinit() {		return isinit;	}	public void setinit(boolean isinit) {		this.isinit = isinit;	}}


slipview.java
package com.iaiai.activity;import android.content.context;import android.graphics.bitmap;import android.graphics.canvas;import android.graphics.drawable.bitmapdrawable;import android.util.attributeset;import android.util.log;import android.view.motionevent;import android.view.view;/** *  * <p> * title: slipview.java * </p> * <p> * e-mail: 176291935@qq.com * </p> * <p> * qq: 176291935 * </p> * <p> * http: iaiai.iteye.com * </p> * <p> * create time: 2011-8-11 * </p> *  * @author 丸子 * @version 0.0.1 */public class slipview extends view {	private static final string tag = "slipview";	/** listen slip on the block, no matter the event is success or fail */	private onsliplistener onsliplistener;	/** slip entity to set the value about backimage, frontimage position */	private slipentity slipentity;	private float tmptouchx;	private float tmptouchgap;	public slipview(context context) {		super(context);		bitmap backimage = ((bitmapdrawable) getresources().getdrawable(				r.drawable.back5)).getbitmap();		bitmap frotimage = ((bitmapdrawable) getresources().getdrawable(				r.drawable.frot1)).getbitmap();		this.slipentity = new slipentity(backimage, frotimage);	}	public slipview(context context, attributeset attr) {		super(context, attr);		bitmap backimage = ((bitmapdrawable) getresources().getdrawable(				r.drawable.back5)).getbitmap();		bitmap frotimage = ((bitmapdrawable) getresources().getdrawable(				r.drawable.frot1)).getbitmap();		this.slipentity = new slipentity(backimage, frotimage);	}	public slipview(context context, slipentity slipentity) {		super(context);		this.slipentity = slipentity;	}	@override	protected void ondraw(canvas canvas) {		super.ondraw(canvas);		if (!this.slipentity.isinit) {			log.v(tag, "init slipentity");			this.slipentity.initslipentity(this.getwidth(), this.getheight());		}		canvas.drawbitmap(this.slipentity.getbackimage(),				this.slipentity.xbackstart, this.slipentity.ybackstart, null);		canvas.drawbitmap(this.slipentity.getfrotimage(),				this.slipentity.xfrotstart, this.slipentity.yfrotstart, null);	}	/**	 * listen touch events and notify listener	 */	@override	public boolean ontouchevent(motionevent event) {		log.v(tag, "touch position:" + event.getx());		switch (event.getaction()) {		case motionevent.action_down:			// log.v(tag, "down");			if (this.slipentity.ispointinimage(slipentity.flag_frot,					event.getx(), event.gety())) {				this.tmptouchx = event.getx();				this.tmptouchgap = event.getx() - this.slipentity.xfrotstart;			}			break;		case motionevent.action_move:			// log.v(tag, "move");			this.slipentity.isreachright();			this.slipentity.isreachleft();			// if point(x,y) is not in back image, front image will not move			if (this.slipentity.ispointinimage(slipentity.flag_back,					event.getx(), event.gety())) {				// log.v(tag, "move2");				this.slipentity.xfrotstart = event.getx() - this.tmptouchgap;			}			break;		case motionevent.action_up:			log.v(tag, "up");			int flaglr = 0;			if (event.getx() < this.tmptouchx)				flaglr = 1;			else				flaglr = 2;			float dockper = this.slipentity.isdock();			if (dockper != -1) {				// dock at some position				if (this.onsliplistener != null)					this.onsliplistener.slipdock(slipentity, event,							this.slipentity.getcurdockper());				if (event.getx() < this.tmptouchx) {					// slip: <==					if (dockper == 0.0) {						// reached						if (this.onsliplistener != null)							this.onsliplistener.slipleft(slipentity, event,									true);					} else {						// not reached						this.slipentity.xfrotstart = this.slipentity								.getcurdockpos();						if (this.onsliplistener != null)							this.onsliplistener.slipleft(slipentity, event,									false);					}				} else {					// slip: ==>					if (dockper == 1.0) {						// reached						if (this.onsliplistener != null)							this.onsliplistener.slipright(slipentity, event,									true);					} else {						// not reached						this.slipentity.xfrotstart = this.slipentity								.getcurdockpos();						if (this.onsliplistener != null)							this.onsliplistener.slipright(slipentity, event,									false);					}					break;				}			} else {				// no dock				this.slipentity.xfrotstart = this.slipentity.getcurdockpos();				if (flaglr == 1)					this.onsliplistener.slipleft(slipentity, event, false);				else					this.onsliplistener.slipright(slipentity, event, false);			}			// if (event.getx() < this.tmptouchx)			// {			// // slip: <== // if (this.slipentity.isreachleft())			// { // // reached			// if (this.onsliplistener != null)			// this.onsliplistener.slipleft(slipentity, event, true);			// }			// else			// {			// // not reached			// this.slipentity.xfrotstart = this.slipentity.getcurdockpos();			// if (this.onsliplistener != null)			// this.onsliplistener.slipleft(slipentity, event, false);			// }			// }			// else			// {			// // slip: ==>			// if (this.slipentity.isreachright())			// {			// // reached			// if (this.onsliplistener != null)			// this.onsliplistener.slipright(slipentity, event, true);			// }			// else			// {			// // not reached			// this.slipentity.xfrotstart = this.slipentity.getcurdockpos();			// if (this.onsliplistener != null)			// this.onsliplistener.slipright(slipentity, event, false);			// }			// break;			// }		}		this.invalidate();		return true;	}	/**	 * listener on slip event on slippery view author diydyq	 * 	 */	public interface onsliplistener {		/**		 * listen a slip after touch down and up, not including touch move		 * 		 * @param slipentity		 * @param event		 * @param issuccess		 */		public abstract void slipleft(slipentity slipentity, motionevent event,				boolean issuccess);		/**		 * listen a slip after touch down and up, not including touch move		 * 		 * @param slipentity		 * @param event		 * @param issuccess		 */		public abstract void slipright(slipentity slipentity,				motionevent event, boolean issuccess);		/**		 * listen some dock(more than dock_l,dock_r), normally need not		 * implement it unless more docks are needed.		 * 		 * @param slipentity		 * @param event		 * @param dockper		 */		public abstract void slipdock(slipentity slipentity, motionevent event,				float dockper);	}	public onsliplistener getonsliplistener() {		return onsliplistener;	}	public void setonsliplistener(onsliplistener onsliplistener) {		this.onsliplistener = onsliplistener;	}}


iaiaiactivity.java
package com.iaiai.activity;import android.app.activity;import android.graphics.bitmap;import android.graphics.drawable.bitmapdrawable;import android.os.bundle;import android.util.log;import android.view.motionevent;import com.iaiai.activity.slipview.onsliplistener;/** *  * <p> * title: iaiaiactivity.java * </p> * <p> * e-mail: 176291935@qq.com * </p> * <p> * qq: 176291935 * </p> * <p> * http: iaiai.iteye.com * </p> * <p> * create time: 2011-8-11 * </p> *  * @author 丸子 * @version 0.0.1 */public class iaiaiactivity extends activity {	@override	public void oncreate(bundle savedinstancestate) {		super.oncreate(savedinstancestate);		// setcontentview(r.layout.main);		slipview view;		// 1.code		bitmap backimage = ((bitmapdrawable) getresources().getdrawable(				r.drawable.back5)).getbitmap();		bitmap frotimage = ((bitmapdrawable) getresources().getdrawable(				r.drawable.frot1)).getbitmap();		slipentity entity = new slipentity(backimage, frotimage);		entity.setcurdockpos(slipentity.dock_r);		entity.adddockpos((float) 0.5);		view = new slipview(this, entity);		// 2.code		// view = new slipview(this);		// 3.xml		// view = (slipview)this.findviewbyid(r.id.slipview);		setcontentview(view);		view.setonsliplistener(new onsliplistener() {			@override			public void slipleft(slipentity slipentity, motionevent event,					boolean issuccess) {				log.v("left", boolean.tostring(issuccess));			}			@override			public void slipright(slipentity slipentity, motionevent event,					boolean issuccess) {				log.v("right", boolean.tostring(issuccess));			}			@override			public void slipdock(slipentity slipentity, motionevent event,					float dockper) {				log.v("dock", "dockper:" + dockper);			}		});	}}


下面是用到的三张图片:


 
0
2
分享到:
评论

相关推荐

    android左右滑动的选择控件

    如果选项数量较少,可以简单地将它们作为子View添加到我们的滑动控件中;如果选项很多,使用`RecyclerView`会更合适,它可以高效地复用视图并动态加载数据。 四、滑动动画 为了让滑动效果更加流畅,我们可以使用...

    左右滑动自定义控件

    1. `SlideWidget.java` - 这是自定义滑动控件的核心类,它会包含滑动逻辑的实现。 2. `res/layout/slide_widget.xml` - 这是布局文件,定义了控件的外观和结构,可能包含子View的排列和触摸监听器的设置。 3. `...

    Android自定义滑动接听电话控件组

    在Android开发中,自定义滑动接听电话控件组是一种常见的功能实现,它为用户提供了一种直观、便捷的接听电话方式。这样的控件通常由一个显示来电信息的界面和一个可滑动的接听/挂断按钮组成。在这个场景中,我们将...

    自定义Android可滑动控件源码

    在自定义滑动控件中,我们可以通过设置Scroller的滚动距离和速度,来实现平滑的滑动效果。通常,在ACTION_UP事件中启动Scroller,然后在`computeScroll()`方法中检查Scroller是否还在滚动,并更新视图的位置。 ...

    仿酷狗左右滑动控件

    【仿酷狗左右滑动控件】是一种在Android开发中常见的交互设计,它借鉴了酷狗音乐应用中的滑动切换效果,通常用于实现如歌曲列表、播放器界面等场景的左右切换。这种控件能够提供流畅且直观的用户体验,使得用户可以...

    android ScrollView弹性控件,横向和纵向滑动

    总的来说,ReboundScrollView是一个结合了物理动画和弹性滑动的Android自定义控件,它在标准ScrollView的基础上增加了横向弹性滑动功能,并优化了滑动体验,避免了已知的bug。开发者可以通过学习和使用这个控件,...

    Android 滑动效果 自定义控件

    在创建自定义滑动控件时,我们通常会重写以下方法: 1. **onMeasure()**: 这个方法用于测量所有子视图的大小,并确定ViewGroup自身的大小。我们需要根据自己的需求来计算合适的宽高。 2. **onLayout()**: 在这个...

    Android自定义组合控件:UIScrollLayout(支持界面滑动及左右菜单滑动)

    本文将深入探讨如何创建一个名为`UIScrollLayout`的自定义组合控件,它支持界面的流畅滑动以及左右菜单的滑动功能。这个控件可以通过设置`view_type`参数来切换不同的显示模式,从而为用户提供两种不同的视图体验。 ...

    Android中自定义滑动选中控件WheelView

    在Android开发中,有时我们需要创建具有独特交互效果的控件,比如滑动选择器,而`WheelView`就是这样一种自定义的控件。它通常用于日期选择、时间选择或者选项滚动等场景,提供了类似滚轮的视觉效果和操作体验。在你...

    左右滑动评分控件

    左右滑动评分,不限于做评分。一分,反正评论后会返分。

    Android item可左右滑动的listview控件-IT计算机-毕业设计.zip

    本项目名为"Android item可左右滑动的listview控件",是几年前的一个Android应用源码Demo,非常适合学生作为毕业设计学习。 在传统的ListView中,用户只能通过点击来交互,而这个Demo引入了侧滑功能,使得用户可以...

    Android ScrollView嵌套横向滑动控件时冲突问题

    看到这种问题,自然而然的就会想到scrollview和其他可滑动控件的冲突问题。 解决思路 用户的左右滑动操作被最外层的scrollView控件处理掉了,所以只要让scrollview对左右滑动事件不监听,让其子控件处理左右滑动事件...

    Android 自定义可上下左右滑动table

    标题"Android 自定义可上下左右滑动table"指向的就是一个专门为Android平台设计的定制化表格控件,它不仅支持垂直滚动(上下滑动),还支持水平滚动(左右滑动),这在处理大数据量或需要展示宽幅信息时非常有用。...

    Android滑动选择控件WheelView,双级联动,有图有真相.zip

    在Android应用开发中,滑动选择控件是常见的交互元素,用于实现用户通过滚动来选取特定值或选项。本资源提供了名为"WheelView"的自定义控件,它支持双级联动,即两个轮盘之间存在关联性,当一个轮盘的值改变时,另一...

    Android仿抖音上下滑动布局

    Android 仿抖音上下滑动布局详解 Android 仿抖音上下滑动布局是指在 Android 应用程序中实现类似抖音 App 的上下滑动布局效果,该效果可以实现上下滑动、监听播放、自动吸顶、吸底等功能。本文将详细介绍 Android ...

    Android-该自定义控件是一个可以滑动改变温度值的表盘控件

    通过研究这些内容,开发者不仅可以学习到如何创建滑动温度表盘控件,还能了解到Android自定义控件的完整流程,包括设计思路、代码实现、UI优化和文档编写等方面的知识。 总结来说,这个自定义控件是Android开发中的...

    一个android桌面滑动切换屏幕的控件(二)

    滑动切换屏幕的控件通常用于模仿手机或平板设备的主屏幕效果,用户可以通过左右滑动来浏览不同的屏幕页面。这种效果可以应用于各种场景,如应用启动页、多视图展示或者信息浏览等。在Android中,我们可以利用...

    Android ScrollView向上滑动控件顶部悬浮效果实现

    通过以上步骤,我们就成功实现了Android ScrollView向上滑动时控件顶部悬浮的效果。这种效果不仅提高了用户的操作便利性,也使界面设计更加美观和统一。在实际开发中,还可以根据项目需求进行适当的调整和优化,比如...

    Android-类似 iOS 带弹簧效果的左右滑动控件

    这个"Android-类似 iOS 带弹簧效果的左右滑动控件"就是一个专为此目的设计的库,它可以被集成到AbsListView(如ListView)或RecyclerView中,为item提供动态、真实的物理反馈效果。 该控件的核心功能是模拟iOS的...

    Android 左右滑动效果

    ViewPager是Android SDK中的一个非常实用的控件,常用于实现水平滑动的页面浏览。它可以承载多个Fragment或View,并通过手势左右滑动进行切换。要使用ViewPager,首先需要在布局文件中添加它,然后在代码中设置...

Global site tag (gtag.js) - Google Analytics