支持平台:Android
运行环境:Eclipse
开发语言:Java
开发工具:Eclipse
源码大小:2.99MB
源码下载地址:http://down.51cto.com/data/1984783
源码简介
该源码是基于Android系统的Launcher原理进行设计和编写。
主要包含以下功能模块:
1、利用自定义GridView(或继承ViewGroup)实现GridView控件的拖动;
2、GridView长按拖动的动画效果;
3、GridView切屏拖动;
4、Activity不同生命周期对GridView数据的刷新及保存;
5、数据库DBUtil的使用。
源码运行截图
GridView单屏拖动动画效果
GridView切屏时,拖动的动画效果
GridView切屏后,对象交换
删除GridView中某一对象
源码片段
- //添加一个item
- public void addItemView(MoveItem item) {
- mAdapter.add(item);
- this.addView(getView(mAdapter.getCount() - 1));
- showEdit(isEditting);
- requestLayout();
- }
- @Override
- public void addView(View child, int index, LayoutParams params) {
- child.setClickable(true);
- if (child.getVisibility() != View.VISIBLE)
- child.setVisibility(View.VISIBLE);
- super.addView(child, index, params);
- int pages = (int) Math.ceil(getChildCount() * 1.0 / itemPerPage);
- if (pages > totalPage) {
- if (this.onAddPage != null)
- onAddPage.onAddOrDeletePage(totalPage, true);
- totalPage = pages;
- }
- }
- //绘制Container所有item
- public void refreView() {
- removeAllViews();
- for (int i = 0; i < mAdapter.getCount(); i++) {
- this.addView(getView(i));
- }
- totalPage = (int) Math.ceil(getChildCount() * 1.0 / itemPerPage);
- requestLayout();
- }
- @Override
- public void removeView(View view) {
- super.removeView(view);
- int pages = (int) Math.ceil(getChildCount() * 1.0 / itemPerPage);
- if (pages < totalPage) {
- if (this.onAddPage != null)
- onAddPage.onAddOrDeletePage(totalPage, false);
- totalPage = pages;
- }
- }
- @Override
- public void removeViewAt(int index) {
- super.removeViewAt(index);
- int pages = (int) Math.ceil(getChildCount() * 1.0 / itemPerPage);
- if (pages < totalPage) {
- totalPage = pages;
- if (this.onAddPage != null)
- onAddPage.onAddOrDeletePage(totalPage, false);
- }
- }
- @Override
- public boolean dispatchTouchEvent(MotionEvent ev) {
- final int action = ev.getAction();
- final float x = ev.getX();
- final float y = ev.getY();
- int thresholdX = DensityUtil.dip2px(mContext, 8);
- switch (action) {
- case MotionEvent.ACTION_DOWN:
- startX = (int) x;
- if (mScroller.isFinished()) {
- if (!mScroller.isFinished()) {
- mScroller.abortAnimation();
- }
- temChangPosition = dragPosition = pointToPosition((int) x, (int) y);
- dragOffsetX = (int) (ev.getRawX() - x);
- dragOffsetY = (int) (ev.getRawY() - y);
- mLastMotionX = x;
- mLastMotionY = y;
- startX = (int) x;
- }
- break;
- case MotionEvent.ACTION_MOVE:
- int deltaX = (int) (mLastMotionX - x);
- if (IsCanMove(deltaX) && Math.abs(deltaX) > thresholdX && Mode != Mode_Drag) {
- mLastMotionX = x;
- scrollBy(deltaX, 0);
- Mode = Mode_Scroll;
- }
- if (Mode == Mode_Drag) {
- onDrag((int) x, (int) y);
- }
- break;
- case MotionEvent.ACTION_UP:
- float distance = ev.getRawX() - startX;
- if (distance > screenWidth / 6 && mCurScreen > 0
- && Mode != Mode_Drag) {
- snapToScreen(mCurScreen - 1);
- } else if (distance < -screenWidth / 6
- && mCurScreen < totalPage - 1 && Mode != Mode_Drag) {
- snapToScreen(mCurScreen + 1);
- } else if (Mode != Mode_Drag) {
- snapToDestination();
- }
- if (Mode == Mode_Drag) {
- stopDrag();
- }
- if (dragImageView != null) {
- animationMap.clear();
- showDropAnimation((int) x, (int) y);
- }
- startX = 0;
- break;
- case MotionEvent.ACTION_CANCEL:
- showEdit(false);
- }
- super.dispatchTouchEvent(ev);
- return true;
- }
- //开始拖动
- private void startDrag(Bitmap bm, int x, int y, View itemView) {
- dragPointX = x - itemView.getLeft() + mCurScreen * screenWidth;
- dragPointY = y - itemView.getTop();
- windowParams = new WindowManager.LayoutParams();
- windowParams.gravity = Gravity.TOP | Gravity.LEFT;
- windowParams.x = x - dragPointX + dragOffsetX;
- windowParams.y = y - dragPointY + dragOffsetY;
- windowParams.height = LayoutParams.WRAP_CONTENT;
- windowParams.width = LayoutParams.WRAP_CONTENT;
- windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
- | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
- | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
- | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
- windowParams.format = PixelFormat.TRANSLUCENT;
- windowParams.windowAnimations = 0;
- windowParams.alpha = 0.8f;
- ImageView iv = new ImageView(getContext());
- iv.setImageBitmap(bm);
- dragBitmap = bm;
- windowManager = (WindowManager) getContext().getSystemService(
- Context.WINDOW_SERVICE);
- windowManager.addView(iv, windowParams);
- dragImageView = iv;
- Mode = Mode_Drag;
- halfBitmapWidth = bm.getWidth() / 2;
- halfBitmapHeight = bm.getHeight() / 2;
- for (int i = 0; i < getChildCount(); i++) {
- getChildAt(i).getBackground().setAlpha((int) (0.8f * 255));
- }
- }
- //停止拖动
- private void stopDrag() {
- recoverChildren();
- if (Mode == Mode_Drag) {
- if (getChildAt(dragPosition).getVisibility() != View.VISIBLE)
- getChildAt(dragPosition).setVisibility(View.VISIBLE);
- Mode = Mode_Free;
- Log.e("test", "scroll menu move");
- mContext.sendBroadcast(new Intent("com.stg.menu_move"));
- }
- }