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

在viewgroup执行:

public void snapToScreen(int whichScreen) {
		whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
		boolean changingScreens = whichScreen != mCurrentScreen;


		mNextScreen = whichScreen;
		int mScrollX = this.getScrollX();
		final int newX = whichScreen * getWidth();
		final int delta = newX - mScrollX;
		System.out.println("====snapToScreen delta="+delta);
		<strong>mScroller.startScroll(mScrollX, 0, delta, 0, Math.abs(delta) * 2); //这个含义是从x坐标mScrollX的位置滑动delta距离</strong>
		//invalidate非常重要,不然你移动一点页面不能回复原状
		invalidate();
	}


为了使这个动画有效果,我们必须重载computeScroll
@Override
	public void computeScroll() {
		if (<strong>mScroller.computeScrollOffset()</strong>) {
			<strong>scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
		     postInvalidate();</strong>
		} else if (mNextScreen != -1) { <strong>//这个是滑动完成后执行的,这个时候必须set当前的view</strong>
			setCurrentScreen(Math.max(0,
					Math.min(mNextScreen, getChildCount() - 1)));
			mNextScreen = -1;


		}
	}


如果这个viewgroup一次性加载太多子控件,会造成性能的很大影响,为了解决这个问题,我们应该在显示当前的控件,其他的应该visible置为空

这样来处理:

void setCurrentScreen(int index) {
		mCurrentScreen = index;
		resetVisibilityForChildren();
	}
	
	private void resetVisibilityForChildren() {
	    int count = getChildCount();
	    for (int i = 0; i < count; i++) {
	        View child = getChildAt(i);
	        if (Math.abs(mCurrentScreen - i) <= 0) {
	            child.setVisibility(View.VISIBLE);
	        } else {
	            child.setVisibility(View.INVISIBLE);
	        }
	    }
	}

为了滑动的时候能显示下一页,我们必须重写dispatchDraw

@Override
	protected void dispatchDraw(Canvas canvas) {
		int childCount = getChildCount();
		if (childCount == 0) {
			return;
		}


		boolean restore = false;
		int restoreCount = 0;

		final long drawingTime = getDrawingTime();
		final float scrollPos = (float) getScrollX() / getWidth();
		final int leftScreen = (int) scrollPos;
		final int rightScreen = leftScreen + 1;
		if (leftScreen >= 0 && leftScreen < childCount) {
			<strong>drawChild(canvas, getChildAt(leftScreen), drawingTime); //这个会使leftScreen由gone的状态显示出来</strong>
		}
		if  (rightScreen < getChildCount()) {
			drawChild(canvas, getChildAt(rightScreen), drawingTime);
		}

		if (restore) {
			canvas.restoreToCount(restoreCount);
		}

	}

下面来说如何在这个界面上加入dot,来表示当前滑动哪个界面:

在myGroup类里加入:

	public void setmListener(OnViewChangedListener mListener) {
		this.mListener = mListener;
	}
	
	public interface OnViewChangedListener {
	    /**
	     * When view changed.
	     * @param viewIndex index.
	     */
		void onViewChanged(int viewIndex);
	}
    private static final int DEFAULT_POINT_MARGIN = 5;

	/**
     * 更新点点的状态,包括总数和当前位置。
     * @param dotsLayout 用来放置点点的layout.
     * @param total 总数。
     * @param current 当前位置。
     * @param state Bundle,参数扩展,可以为null.
     */
    public static void updateDots(ViewGroup dotsLayout, int total, int current, Bundle state) {
        
        if (total < 0) {
            total = 0;
        }
        
        int lastIndex = -1;
        Object tag = dotsLayout.getTag(R.id.dots_current);
        if (tag != null) {
            lastIndex = (Integer) tag;
        }
        
        int margin = DEFAULT_POINT_MARGIN;
        if (state != null) {
            margin = state.getInt(KEY_POINT_MARGIN, margin);
        }
        
        int childrenNum = dotsLayout.getChildCount();
        
        for (int i = childrenNum; i < total; i++) {
            ImageView pointView = new ImageView(dotsLayout.getContext());
            pointView.setImageResource(R.drawable.dot);
            dotsLayout.addView(pointView);
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) pointView.getLayoutParams();
            params.leftMargin = margin;
            params.rightMargin = margin;
        }
        
        for (int i = childrenNum - 1; i >= total; i--) {
            dotsLayout.removeViewAt(i);
        }
        
        if (current == lastIndex) {
            return;
        }
        
        if (lastIndex >= 0 && lastIndex < total) {
            ImageView pointView = (ImageView) dotsLayout.getChildAt(lastIndex);
            pointView.setImageResource(R.drawable.dot);
        }
        
        if (current >= 0 && current < total) {
            ImageView pointView = (ImageView) dotsLayout.getChildAt(current);
            pointView.setImageResource(R.drawable.dot_current);
        }
        
        dotsLayout.setTag(R.id.dots_current, current);
    }
    
	public interface WorkspaceSnapListener {
	    
		void onSnapToScreen(MyGroup workspace, int whichScreen);
	}

	public void setSnapListener(WorkspaceSnapListener listener) {
		snapListener = listener;
	}

@Override
	public void computeScroll() {
		if (mScroller.computeScrollOffset()) {
			
		} else if (mNextScreen != -1) {
	
			<strong>if (mListener != null) {
				mListener.onViewChanged(mCurrentScreen);
			}</strong>
		}
	}
public void snapToScreen(int whichScreen) {
		
		
		<strong>if (snapListener != null) {
			snapListener.onSnapToScreen(this, whichScreen);
		}</strong>
	}


在MainActivity的layout中加入:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MainRoot"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <com.ringcentral.android.utils.ui.widget.MyGroup
       android:background="@drawable/help_background_selector"
        android:id="@+id/workspace"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
    
     <LinearLayout android:id="@+id/dots_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="28dip"
        android:layout_gravity="bottom|center_horizontal"
        android:orientation="horizontal"/>

</FrameLayout>

点的画法:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <size android:width="6dip" android:height="6dip"/>
    <solid android:color="#7fffffff"/>
</shape>


oncreate:

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
        final ViewGroup dotsLayout = (ViewGroup) findViewById(R.id.dots_layout);

		final int[] images = new int[]{R.drawable.help_01,R.drawable.help_02,R.drawable.help_03};
		final MyGroup myGroup = (MyGroup) findViewById(R.id.workspace);
		
		final Bundle dotsState = new Bundle();
		int margin = getResources().getDimensionPixelSize(R.dimen.workspace_dot_margin);
		dotsState.putInt(MyGroup.KEY_POINT_MARGIN, margin);
		MyGroup.updateDots(dotsLayout, images.length, 0, dotsState);

	        
		for (int i = 0; i < 3; i++) {
			final View item = getLayoutInflater().inflate(
					R.layout.introduction_item_test, null);

			myGroup.addView(item);
			final ImageView imageView = (ImageView) item
					.findViewById(R.id.introduction_image_view);
			try {
				imageView.setImageResource(images[i]);
			} catch (OutOfMemoryError e) {
			}

		}
        
		myGroup.setmListener(new OnViewChangedListener() {

            @Override
            public void onViewChanged(int viewIndex) {
                myGroup.updateDots(dotsLayout, images.length, viewIndex, dotsState);
            }
        });
		
		myGroup.setSnapListener(new WorkspaceSnapListener() {

			@Override
			public void onSnapToScreen(MyGroup workspace, int whichScreen) {
				myGroup.updateDots(dotsLayout, images.length, whichScreen, dotsState);

				
			}
		});
	}

效果图:



代码:http://download.csdn.net/detail/baidu_nod/7731855

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是实现Android触摸点击屏幕隐藏控件再点击恢复显示的代码示例: XML布局文件: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:layout_centerInParent="true"/> </RelativeLayout> ``` Java代码: ```java public class MainActivity extends AppCompatActivity implements View.OnTouchListener { private RelativeLayout layout; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); layout = findViewById(R.id.layout); textView = findViewById(R.id.textView); layout.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { if (textView.getVisibility() == View.VISIBLE) { textView.setVisibility(View.INVISIBLE); } else { textView.setVisibility(View.VISIBLE); } return true; } return false; } } ``` 该代码与之前的代码示例相似,只是将RelativeLayout的点击事件改为了触摸事件,并且实现了View.OnTouchListener接口。当触摸事件为ACTION_DOWN时,判断TextView的可见性并设置其可见性的值。同时,返回true表示事件已经被处理,不再继续传递。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值