
本文示例所述程序为一个AndroID MovevIEw移动视图类。其主要实现主屏左右滑屏拖动功能,并适时显示拖动时候屏幕的显示情况,该代码中还包括完整的逻辑。其完整代码如下:
import androID.study.shift.ItemVIEw;import androID.study.shift.MainVIEw;import androID.study.shift.MovevIEw;import androID.content.Context;import androID.os.Handler;import androID.os.Message;import androID.util.AttributeSet;import androID.util.Log;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.vIEw.VIEw.MeasureSpec;public class MovevIEw extends VIEwGroup{ private final static int touch_STATE_REST = 0; private final static int touch_STATE_MOVING = 1; private final static int MOVE_TO_left = 1; //private final static int MOVE_TO_RIGHT = 2; private final static int MOVE_TO_REST = 0; public final static int MAIN = 0; public final static int left = 1; public final static int RIGHT = 2; private int touch_state = touch_STATE_REST; private int move_state = MOVE_TO_REST; private int Now_state = MAIN; private final float WIDTH_RATE = 0.18f; private MainVIEw main_show_vIEw; private ItemVIEw left_show_vIEw; private int min_distance = 30;//此处只是初始化为30 ,其实不起作用 private int screen_w; private int screen_h; private int move_x_v; private boolean isaimationMoving = false; private Handler mHandler = new Handler() { public voID handleMessage(Message msg) {//处理消息 synchronized (MovevIEw.this) {//同步 isaimationMoving = true; int move_change = (int) (screen_w * WIDTH_RATE / 5); int left = main_show_vIEw.getVIEw().getleft(); if (msg.what == 1) {//主屏在向右拖曳。。。 move(move_change + left);//参数为拖出来后主屏的坐标。 } if (msg.what == 11) { isaimationMoving = false; movetoleft(false); //满足可以显示的要求开始显示,false只是一个判定条件,以区别按钮起的效果 } if (msg.what == 2) {//代表主屏向左在拖曳。。。 move(-1 * move_change + left); } if (msg.what == 12) { isaimationMoving = false; //movetoRight(false); } if (msg.what == 0) { if (Now_state == left) {//如果现在已经显示左边 move(-1 * move_x_v);//那么还原成主屏 } else { move(move_x_v); } } if (msg.what == 10) { isaimationMoving = false; movetoMain(false); } } } }; public MovevIEw(Context context) { super(context); } public MovevIEw(Context context,AttributeSet attrs) { super(context,attrs); } public MovevIEw(Context context,AttributeSet attrs,int defStyle) { super(context,attrs,defStyle); } public voID initVIEw() { if (main_show_vIEw == null) { main_show_vIEw = new MainVIEw(this.getContext(),this); left_show_vIEw = new ItemVIEw(this.getContext()); //right_show_vIEw = new ItemVIEw(this.getContext(),"aaa"); } this.addVIEw(left_show_vIEw.getVIEw()); //this.addVIEw(right_show_vIEw.getVIEw()); this.addVIEw(main_show_vIEw.getVIEw()); } public voID initContent() { } public voID move(int start) {//本函数显示拖动的时候屏幕的显示情况。 int left = main_show_vIEw.getVIEw().getleft();//left代表主屏左边缘的位置坐标。 if (Now_state == MAIN) { if (left > 0) {//如果屏幕的左边被移向右,则left变成正数 if (move_state != MOVE_TO_left) { move_state = MOVE_TO_left; } left_show_vIEw.getVIEw().setVisibility(VIEw.VISIBLE);//设置左边可见 //right_show_vIEw.getVIEw().setVisibility(VIEw.GONE); } //else if (left < 0) {//如果屏幕的左边被移向左,则left变成负数 //if (move_state != MOVE_TO_RIGHT) { // move_state = MOVE_TO_RIGHT; //} //right_show_vIEw.getVIEw().setVisibility(VIEw.VISIBLE); //left_show_vIEw.getVIEw().setVisibility(VIEw.GONE); //} else { move_state = MOVE_TO_REST; } main_show_vIEw.getVIEw().layout(start,start + screen_w,screen_h);//参数依次为左、上、右、下。 } else { left = (int) (screen_w * WIDTH_RATE);//left为左边应该留出的控件空间,或者主屏左边应该在的位置 if (Now_state == RIGHT) { left = -1 * left;//若状态是在右边空出来,则主屏的左边为负数 } left = left + start; main_show_vIEw.getVIEw().layout(left,left + screen_w,screen_h); } } @OverrIDe protected voID onLayout(boolean arg0,int arg1,int arg2,int arg3,int arg4) { if (move_state == MOVE_TO_REST) { if (Now_state == MAIN) { int w = (int) (screen_w * WIDTH_RATE);//留下预定的宽度,显示各个vIEw的参数设置。 main_show_vIEw.getVIEw().layout(0,screen_w,screen_h); left_show_vIEw.getVIEw().layout(0,w,screen_h); } else if (Now_state == left) { movetoleft(false); } else { } } } protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { main_show_vIEw.getVIEw().measure(wIDthMeasureSpec,heightmeasureSpec); left_show_vIEw.getVIEw().measure(MeasureSpec.UnspecIFIED,//unspecifIEd为未指定的 heightmeasureSpec); left_show_vIEw.setWIDth((int) (screen_w * WIDTH_RATE));//设置vIEw宽度 super.onMeasure(wIDthMeasureSpec,heightmeasureSpec);//调用父类的构造函数 } private int start_x; private int start_y; private boolean isMoved; public boolean dispatchtouchEvent(MotionEvent ev) {//分发触摸消息事件 if (isaimationMoving) { return super.dispatchtouchEvent(ev); } else { int action = ev.getAction(); float x = ev.getX(); float y = ev.getY(); switch (action) { case MotionEvent.ACTION_DOWN: super.dispatchtouchEvent(ev); start_y = (int) y; move_x_v = 0; if (this.touch_state == touch_STATE_REST) { this.touch_state = touch_STATE_MOVING; start_x = (int) x; isMoved = false; move_state = MOVE_TO_REST; } break; case MotionEvent.ACTION_MOVE: int last_y = (int) y; int last_x = (int) x; super.dispatchtouchEvent(ev); if (!isMoved) { if (Math.abs(last_y - start_y) > Math.abs(last_x - start_x)) {//如果Y上面移动的距离大于X上面移动的距离。 super.ontouchEvent(ev); return true; } else {//X移动距离大于Y if (Math.abs(last_x - start_x) > 5) {//X移动距离大于5就被认为移动了 isMoved = true; } } } if (isMoved) { if (this.touch_state == touch_STATE_MOVING) { if (Math.abs(last_x - start_x) > 10) {//如果移动的X距离大于10 int left = main_show_vIEw.getVIEw().getleft(); Log.d("msg","left:" + left); Log.d("msg","x:" + last_x); isMoved = true; int move_x = last_x - start_x; if (move_x > 0 && Now_state == left) {//方向正确的移动才算是移动的状态 isMoved = false; break; } if (move_x < 0 && Now_state == RIGHT) {//如果现在在显示右边,那么再向右移就不反应。 isMoved = false; break; } if (move_x < 0 && Now_state ==MAIN){//如果现在在主屏,向右移动也是无效的 isMoved = false; break; } if (move_x > 234 && Now_state ==MAIN){ isMoved = true;//设置为true那么移动还是发生了,break之后还是会在ACTIONUP中起作用 break;//break之后不会执行下面的move()语句,} if (move_x < -234 && Now_state ==left){ isMoved = true; break; } move(move_x);//以移动的距离为参数调用move()实现移动的动态显示 } } return false; } break; case MotionEvent.ACTION_UP:// if (this.touch_state == touch_STATE_MOVING) { if (isMoved) { last_x = (int) x; if (Math.abs(last_x - start_x) > min_distance) {// if (Now_state == MAIN) { if (move_state == MOVE_TO_left) { this.movetoleft(false); } } else { this.movetoMain(false); } } else { // if (Now_state == MAIN) { this.movetoMain(false); } if (Now_state == left) { this.movetoleft(false); } if (Now_state == RIGHT) { //this.movetoRight(false); } } move_state = MOVE_TO_REST; } else { super.dispatchtouchEvent(ev); this.touch_state = touch_STATE_REST; return false;// } } super.ontouchEvent(ev); this.touch_state = touch_STATE_REST; break; } return true; } } public boolean getIsMoved() { return isMoved; } public voID movetoleft(boolean b) { if (!b) { int move_x = (int) (screen_w * WIDTH_RATE); left_show_vIEw.getVIEw().layout(0,screen_h); //right_show_vIEw.getVIEw().layout(move_x,move_x * 2,screen_h); main_show_vIEw.getVIEw().layout(move_x,move_x + screen_w,screen_h); Now_state = left;//为什么现在又把Now_state赋值为left? }// else {//else以及括号中的语句都是为原来的按钮准备的。。。// mHandler.postDelayed(new Runnable() {//一个新的可运行的函数//// @OverrIDe// public voID run() {// int move_change = (int) (screen_w * WIDTH_RATE / 5);//五分之一要显示的宽度// int left = (int) (screen_w * WIDTH_RATE - main_show_vIEw// .getVIEw().getleft());//getleft()返回的是主屏左边缘离左边的距离// Message msg = new Message();// if (left > move_change) {//括号里面不懂,起什么作用?// msg.what = 1;// mHandler.sendMessage(msg);// mHandler.postDelayed(this,10);//又调用自己,基本上非要是小于关系// } else {// msg.what = 11;// mHandler.sendMessage(msg);// mHandler.removeCallbacks(this);// }// }// },0);// } } public voID movetoMain(boolean b) { if (!b) { //right_show_vIEw.getVIEw().setVisibility(VIEw.VISIBLE); left_show_vIEw.getVIEw().setVisibility(VIEw.VISIBLE); int w = (int) (screen_w * WIDTH_RATE); main_show_vIEw.getVIEw().layout(0,screen_h); Now_state = MAIN; } } public voID initScreenSize(int w,int h) { this.screen_w = w; this.screen_h = h; Log.d("screen","screen_w:" + w); this.setKeepScreenOn(true); min_distance = (int) (screen_w / 12.0);//min_distance在这里又被初始化为屏幕的一个比例大小 initVIEw(); initContent(); movetoMain(false); } public int getNowState() { return this.Now_state; }} 总结 以上是内存溢出为你收集整理的Android Moveview滑屏移动视图类完整实例全部内容,希望文章能够帮你解决Android Moveview滑屏移动视图类完整实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)