距离上次发布(android高仿系列)今日头条 --新闻阅读器 (二)
相关的内容已经半个月了,最近利用空闲时间,把今日头条客户端完善了下。完善的功能一个一个全部实现后,就放整个源码。开发的进度就是按照一个一个功能的思路走的,所以开发一个小的功能,如果有用,就写一个专门的博客以便有人用到独立的功能可以方便使用。
这次实现的功能是很多新闻阅读器(网易,今日头条,360新闻等)以及腾讯视频等里面都会出现的频道管理功能。
 
下面先上这次实现功能的效果图:(注:这个效果图没有拖拽的时候移动动画,DEMO里面有,可以下载看看)

一、开发心里历程
刚开始接触这个的时候,不知道要如何实现,去网上翻了一大堆资料,懂了个大概,就是目前可以找到的都是拖拽的时候,不带移动动画的,和线上的客户端交互效果相差甚远,在反反复复的尝试查看相关东西,大致的做了出来,目前在模拟器上似乎有一点小BUG,真机测试没有问题,就先放上来,如果发现问题在修改优化。代码反面,没有好好的修改调整,可能会有点乱,请见谅哈。
 
二、开发前的准备
1.了解重写View的相关知识,并且知道GridView的一些内部方法,如:怎么通过触摸的坐标获取对应的position等(这里我采用的是继承GridView控件)
2.了解屏幕触摸动作传递原理    这里我以前转载的一篇或许会有帮助:Android事件分发机制完全解析,带你从源码的角度彻底理解(全)
3.了解位移动画Animation,本DEMO中主要用到:TranslateAnimation  平移动画
4.了解WindowManager的窗口机制,这里的item拖拽等都要设计到这个。
5.了解SQLiteDatabase 以及SQLiteOpenHelper等数据库操作相关的类,本DEMO中主要用到数据库进行存储频道信息,如果你要用文档进行存储读取也可以。
 
三、开发思路
1.  获取数据库中频道的列表,如果为空,赋予默认列表,并存入数据库,之后通过对应的适配器赋给对应的GridView
 
2.  2个GridView--(1.DragGrid   2. OtherGridView)
DragGrid 用于显示我的频道,带有长按拖拽效果
OtherGridView用于显示更多频道,不带推拽效果
注:由于屏幕大小不一定,外层使用ScrollView,所以2者都要重写计算高度
 
3.  点击2个GridView的时候,根据点击的Item对应的position,获取position对应的view,进行创建一层移动的动画层
起始位置:点击的positiongetLocationInWindow()获取。终点位置:另一个GridView的最后个ITEM 的position + 1的位置。
并赋予移动动画,等动画结束后对2者对应的频道列表进行数据的remove和add操作。
 
4.  设置点击和拖动的限制条件,如  推荐  这个ITEM是不允许用户操作的。
 
5.  拖动的DragGrid的操作:
(1)长按获取长按的ITEM的position  -- dragPosition 以及对应的view ,手指触摸屏幕的时候,调用onInterceptTouchEvent来获取MotionEvent.ACTION_DOWN事件,获取对应的数据。由于这里是继承了GridView,所以长按时间可以通过setOnItemLongClickListener监听来执行,或则你也可以通过计算点击时间来监听是否长按。
(2)通过onTouchEvent(MotionEvent ev)来监听手指的移动和抬起动作。当它移动到 其它的item下面,并且下方的item对应的position  不等于 dragPosition,进行数据交换,并且2者之间的所有item进行移动动画,动画结束后,数据更替刷新界面。
(3) 抬起手后,清除掉拖动时候创建的view,让GridView中的数据显示。
 
6.  退出时候,将改变后的频道列表存入数据库。
 
四、流程图
下面是大体的流程图:

 
五、核心代码
点击进行添加删除:
-     @Override  
-     public void onItemClick(AdapterView<?> parent, final View view, final int position,long id) {  
-         
-         if(isMove){  
-             return;  
-         }  
-         switch (parent.getId()) {  
-         case R.id.userGridView:  
-             
-             if (position != 0 && position != 1) {  
-                 final ImageView moveImageView = getView(view);  
-                 if (moveImageView != null) {  
-                     TextView newTextView = (TextView) view.findViewById(R.id.text_item);  
-                     final int[] startLocation = new int[2];  
-                     newTextView.getLocationInWindow(startLocation);  
-                     final ChannelItem channel = ((DragAdapter) parent.getAdapter()).getItem(position);
-                     otherAdapter.setVisible(false);  
-                     
-                     otherAdapter.addItem(channel);  
-                     new Handler().postDelayed(new Runnable() {  
-                         public void run() {  
-                             try {  
-                                 int[] endLocation = new int[2];  
-                                 
-                                 otherGridView.getChildAt(otherGridView.getLastVisiblePosition()).getLocationInWindow(endLocation);  
-                                 MoveAnim(moveImageView, startLocation , endLocation, channel,userGridView);  
-                                 userAdapter.setRemove(position);  
-                             } catch (Exception localException) {  
-                             }  
-                         }  
-                     }, 50L);  
-                 }  
-             }  
-             break;  
-         case R.id.otherGridView:  
-             final ImageView moveImageView = getView(view);  
-             if (moveImageView != null){  
-                 TextView newTextView = (TextView) view.findViewById(R.id.text_item);  
-                 final int[] startLocation = new int[2];  
-                 newTextView.getLocationInWindow(startLocation);  
-                 final ChannelItem channel = ((OtherAdapter) parent.getAdapter()).getItem(position);  
-                 userAdapter.setVisible(false);  
-                 
-                 userAdapter.addItem(channel);  
-                 new Handler().postDelayed(new Runnable() {  
-                     public void run() {  
-                         try {  
-                             int[] endLocation = new int[2];  
-                             
-                             userGridView.getChildAt(userGridView.getLastVisiblePosition()).getLocationInWindow(endLocation);  
-                             MoveAnim(moveImageView, startLocation , endLocation, channel,otherGridView);  
-                             otherAdapter.setRemove(position);  
-                         } catch (Exception localException) {  
-                         }  
-                     }  
-                 }, 50L);  
-             }  
-             break;  
-         default:  
-             break;  
-         }  
-     }  
 
 
- <span style="font-size:14px;">private void MoveAnim(View moveView, int[] startLocation,int[] endLocation, final ChannelItem moveChannel,  
-             final GridView clickGridView) {  
-         int[] initLocation = new int[2];  
-         
-         moveView.getLocationInWindow(initLocation);  
-         
-         final ViewGroup moveViewGroup = getMoveViewGroup();  
-         final View mMoveView = getMoveView(moveViewGroup, moveView, initLocation);  
-         
-         TranslateAnimation moveAnimation = new TranslateAnimation(  
-                 startLocation[0], endLocation[0], startLocation[1],  
-                 endLocation[1]);  
-         moveAnimation.setDuration(300L);
-         
-         AnimationSet moveAnimationSet = new AnimationSet(true);  
-         moveAnimationSet.setFillAfter(false);
-         moveAnimationSet.addAnimation(moveAnimation);  
-         mMoveView.startAnimation(moveAnimationSet);  
-         moveAnimationSet.setAnimationListener(new AnimationListener() {  
-               
-             @Override  
-             public void onAnimationStart(Animation animation) {  
-                 isMove = true;  
-             }  
-               
-             @Override  
-             public void onAnimationRepeat(Animation animation) {  
-             }  
-               
-             @Override  
-             public void onAnimationEnd(Animation animation) {  
-                 moveViewGroup.removeView(mMoveView);  
-                 
-                 if (clickGridView instanceof DragGrid) {  
-                     otherAdapter.setVisible(true);  
-                     otherAdapter.notifyDataSetChanged();  
-                     userAdapter.remove();  
-                 }else{  
-                     userAdapter.setVisible(true);  
-                     userAdapter.notifyDataSetChanged();  
-                     otherAdapter.remove();  
-                 }  
-                 isMove = false;  
-             }  
-         });  
-     }</span>  
 
可拖拽的DragGrid代码: 
- public class DragGrid extends GridView {  
-     
-     public int downX;  
-     
-     public int downY;  
-     
-     public int windowX;  
-     
-     public int windowY;  
-     
-     private int win_view_x;  
-     
-     private int win_view_y;  
-     
-     int dragOffsetX;  
-     
-     int dragOffsetY;  
-     
-     public int dragPosition;  
-     
-     private int dropPosition;  
-     
-     private int startPosition;  
-     
-     private int itemHeight;  
-     
-     private int itemWidth;  
-     
-     private View dragImageView = null;  
-     
-     private ViewGroup dragItemView = null;  
-     
-     private WindowManager windowManager = null;  
-     
-     private WindowManager.LayoutParams windowParams = null;  
-     
-     private int itemTotalCount;  
-     
-     private int nColumns = 4;  
-     
-     private int nRows;  
-     
-     private int Remainder;  
-     
-     private boolean isMoving = false;  
-     
-     private int holdPosition;  
-     
-     private double dragScale = 1.2D;  
-     
-     private Vibrator mVibrator;  
-     
-     private int mHorizontalSpacing = 15;  
-     
-     private int mVerticalSpacing = 15;  
-     
-     private String LastAnimationID;  
-   
-     public DragGrid(Context context) {  
-         super(context);  
-         init(context);  
-     }  
-   
-     public DragGrid(Context context, AttributeSet attrs, int defStyle) {  
-         super(context, attrs, defStyle);  
-         init(context);  
-     }  
-   
-     public DragGrid(Context context, AttributeSet attrs) {  
-         super(context, attrs);  
-         init(context);  
-     }  
-   
-     public void init(Context context) {  
-         mVibrator = (Vibrator) context  
-                 .getSystemService(Context.VIBRATOR_SERVICE);  
-         
-         mHorizontalSpacing = DataTools.dip2px(context, mHorizontalSpacing);  
-     }  
-   
-     @Override  
-     public boolean onInterceptTouchEvent(MotionEvent ev) {  
-         
-         if (ev.getAction() == MotionEvent.ACTION_DOWN) {  
-             downX = (int) ev.getX();  
-             downY = (int) ev.getY();  
-             windowX = (int) ev.getX();  
-             windowY = (int) ev.getY();  
-             setOnItemClickListener(ev);  
-         }  
-         return super.onInterceptTouchEvent(ev);  
-     }  
-   
-     @Override  
-     public boolean onTouchEvent(MotionEvent ev) {  
-         
-         boolean bool = true;  
-         if (dragImageView != null  
-                 && dragPosition != AdapterView.INVALID_POSITION) {  
-             
-             bool = super.onTouchEvent(ev);  
-             int x = (int) ev.getX();  
-             int y = (int) ev.getY();  
-             switch (ev.getAction()) {  
-             case MotionEvent.ACTION_DOWN:  
-                 downX = (int) ev.getX();  
-                 windowX = (int) ev.getX();  
-                 downY = (int) ev.getY();  
-                 windowY = (int) ev.getY();  
-                 break;  
-             case MotionEvent.ACTION_MOVE:  
-                 onDrag(x, y, (int) ev.getRawX(), (int) ev.getRawY());  
-                 if (!isMoving) {  
-                     OnMove(x, y);  
-                 }  
-                 if (pointToPosition(x, y) != AdapterView.INVALID_POSITION) {  
-                     break;  
-                 }  
-                 break;  
-             case MotionEvent.ACTION_UP:  
-                 stopDrag();  
-                 onDrop(x, y);  
-                 requestDisallowInterceptTouchEvent(false);  
-                 break;  
-   
-             default:  
-                 break;  
-             }  
-         }  
-         return super.onTouchEvent(ev);  
-     }  
-   
-     
-     private void onDrag(int x, int y, int rawx, int rawy) {  
-         if (dragImageView != null) {  
-             windowParams.alpha = 0.6f;  
-             windowParams.x = rawx - win_view_x;  
-             windowParams.y = rawy - win_view_y;  
-             windowManager.updateViewLayout(dragImageView, windowParams);  
-         }  
-     }  
-   
-     
-     private void onDrop(int x, int y) {  
-         
-         int tempPostion = pointToPosition(x, y);  
-         dropPosition = tempPostion;  
-         DragAdapter mDragAdapter = (DragAdapter) getAdapter();  
-         
-         mDragAdapter.setShowDropItem(true);  
-         
-         mDragAdapter.notifyDataSetChanged();  
-     }  
-   
-     
-     public void setOnItemClickListener(final MotionEvent ev) {  
-         setOnItemLongClickListener(new OnItemLongClickListener() {  
-   
-             @Override  
-             public boolean onItemLongClick(AdapterView<?> parent, View view,  
-                     int position, long id) {  
-                 int x = (int) ev.getX();
-                 int y = (int) ev.getY();
-                 startPosition = position;
-                 dragPosition = position;  
-                 if (startPosition <= 1) {  
-                     return false;  
-                 }  
-                 ViewGroup dragViewGroup = (ViewGroup) getChildAt(dragPosition  
-                         - getFirstVisiblePosition());  
-                 TextView dragTextView = (TextView) dragViewGroup  
-                         .findViewById(R.id.text_item);  
-                 dragTextView.setSelected(true);  
-                 dragTextView.setEnabled(false);  
-                 itemHeight = dragViewGroup.getHeight();  
-                 itemWidth = dragViewGroup.getWidth();  
-                 itemTotalCount = DragGrid.this.getCount();  
-                 int row = itemTotalCount / nColumns;
-                 Remainder = (itemTotalCount % nColumns);
-                 if (Remainder != 0) {  
-                     nRows = row + 1;  
-                 } else {  
-                     nRows = row;  
-                 }  
-                 
-                 if (dragPosition != AdapterView.INVALID_POSITION) {  
-                     
-                     win_view_x = windowX - dragViewGroup.getLeft();
-                     win_view_y = windowY - dragViewGroup.getTop();
-                     dragOffsetX = (int) (ev.getRawX() - x);
-                     dragOffsetY = (int) (ev.getRawY() - y);
-                     dragItemView = dragViewGroup;  
-                     dragViewGroup.destroyDrawingCache();  
-                     dragViewGroup.setDrawingCacheEnabled(true);  
-                     Bitmap dragBitmap = Bitmap.createBitmap(dragViewGroup  
-                             .getDrawingCache());  
-                     mVibrator.vibrate(50);
-                     startDrag(dragBitmap, (int) ev.getRawX(),  
-                             (int) ev.getRawY());  
-                     hideDropItem();  
-                     dragViewGroup.setVisibility(View.INVISIBLE);  
-                     isMoving = false;  
-                     requestDisallowInterceptTouchEvent(true);  
-                     return true;  
-                 }  
-                 return false;  
-             }  
-         });  
-     }  
-   
-     public void startDrag(Bitmap dragBitmap, int x, int y) {  
-         stopDrag();  
-         windowParams = new WindowManager.LayoutParams();
-         
-         windowParams.gravity = Gravity.TOP | Gravity.LEFT;  
-         
-         windowParams.x = x - win_view_x;  
-         windowParams.y = y - win_view_y;  
-         
-         windowParams.width = (int) (dragScale * dragBitmap.getWidth());
-         windowParams.height = (int) (dragScale * dragBitmap.getHeight());
-         this.windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  
-                 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE  
-                 | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON  
-                 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;  
-         this.windowParams.format = PixelFormat.TRANSLUCENT;  
-         this.windowParams.windowAnimations = 0;  
-         ImageView iv = new ImageView(getContext());  
-         iv.setImageBitmap(dragBitmap);  
-         windowManager = (WindowManager) getContext().getSystemService(  
-                 Context.WINDOW_SERVICE);
-         windowManager.addView(iv, windowParams);  
-         dragImageView = iv;  
-     }  
-   
-     
-     private void stopDrag() {  
-         if (dragImageView != null) {  
-             windowManager.removeView(dragImageView);  
-             dragImageView = null;  
-         }  
-     }  
-   
-     
-     @Override  
-     public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
-         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,  
-                 MeasureSpec.AT_MOST);  
-         super.onMeasure(widthMeasureSpec, expandSpec);  
-     }  
-   
-     
-     private void hideDropItem() {  
-         ((DragAdapter) getAdapter()).setShowDropItem(false);  
-     }  
-   
-     
-     public Animation getMoveAnimation(float toXValue, float toYValue) {  
-         TranslateAnimation mTranslateAnimation = new TranslateAnimation(  
-                 Animation.RELATIVE_TO_SELF, 0.0F, Animation.RELATIVE_TO_SELF,  
-                 toXValue, Animation.RELATIVE_TO_SELF, 0.0F,  
-                 Animation.RELATIVE_TO_SELF, toYValue);
-         mTranslateAnimation.setFillAfter(true);
-         mTranslateAnimation.setDuration(300L);  
-         return mTranslateAnimation;  
-     }  
-   
-     
-     public void OnMove(int x, int y) {  
-         
-         int dPosition = pointToPosition(x, y);  
-         
-         if (dPosition > 1) {  
-             if ((dPosition == -1) || (dPosition == dragPosition)) {  
-                 return;  
-             }  
-             dropPosition = dPosition;  
-             if (dragPosition != startPosition) {  
-                 dragPosition = startPosition;  
-             }  
-             int movecount;  
-             
-             if ((dragPosition == startPosition)  
-                     || (dragPosition != dropPosition)) {  
-                 
-                 movecount = dropPosition - dragPosition;  
-             } else {  
-                 
-                 movecount = 0;  
-             }  
-             if (movecount == 0) {  
-                 return;  
-             }  
-   
-             int movecount_abs = Math.abs(movecount);  
-   
-             if (dPosition != dragPosition) {  
-                 
-                 ViewGroup dragGroup = (ViewGroup) getChildAt(dragPosition);  
-                 dragGroup.setVisibility(View.INVISIBLE);  
-                 float to_x = 1;
-                 float to_y;
-                 
-                 float x_vlaue = ((float) mHorizontalSpacing / (float) itemWidth) + 1.0f;  
-                 
-                 float y_vlaue = ((float) mVerticalSpacing / (float) itemHeight) + 1.0f;  
-                 Log.d("x_vlaue", "x_vlaue = " + x_vlaue);  
-                 for (int i = 0; i < movecount_abs; i++) {  
-                     to_x = x_vlaue;  
-                     to_y = y_vlaue;  
-                     
-                     if (movecount > 0) {  
-                         
-                         holdPosition = dragPosition + i + 1;  
-                         if (dragPosition / nColumns == holdPosition / nColumns) {  
-                             to_x = -x_vlaue;  
-                             to_y = 0;  
-                         } else if (holdPosition % 4 == 0) {  
-                             to_x = 3 * x_vlaue;  
-                             to_y = -y_vlaue;  
-                         } else {  
-                             to_x = -x_vlaue;  
-                             to_y = 0;  
-                         }  
-                     } else {  
-                         
-                         holdPosition = dragPosition - i - 1;  
-                         if (dragPosition / nColumns == holdPosition / nColumns) {  
-                             to_x = x_vlaue;  
-                             to_y = 0;  
-                         } else if ((holdPosition + 1) % 4 == 0) {  
-                             to_x = -3 * x_vlaue;  
-                             to_y = y_vlaue;  
-                         } else {  
-                             to_x = x_vlaue;  
-                             to_y = 0;  
-                         }  
-                     }  
-                     ViewGroup moveViewGroup = (ViewGroup) getChildAt(holdPosition);  
-                     Animation moveAnimation = getMoveAnimation(to_x, to_y);  
-                     moveViewGroup.startAnimation(moveAnimation);  
-                     
-                     if (holdPosition == dropPosition) {  
-                         LastAnimationID = moveAnimation.toString();  
-                     }  
-                     moveAnimation.setAnimationListener(new AnimationListener() {  
-   
-                         @Override  
-                         public void onAnimationStart(Animation animation) {  
-                             
-                             isMoving = true;  
-                         }  
-   
-                         @Override  
-                         public void onAnimationRepeat(Animation animation) {  
-                             
-   
-                         }  
-   
-                         @Override  
-                         public void onAnimationEnd(Animation animation) {  
-                             
-                             
-                             if (animation.toString().equalsIgnoreCase(  
-                                     LastAnimationID)) {  
-                                 DragAdapter mDragAdapter = (DragAdapter) getAdapter();  
-                                 mDragAdapter.exchange(startPosition,  
-                                         dropPosition);  
-                                 startPosition = dropPosition;  
-                                 dragPosition = dropPosition;  
-                                 isMoving = false;  
-                             }  
-                         }  
-                     });  
-                 }  
-             }  
-         }  
-     }  
- }  
 
 
数据库SQLHelper文件 
- public class SQLHelper extends SQLiteOpenHelper {  
-     public static final String DB_NAME = "database.db";
-     public static final int VERSION = 1;  
-       
-     public static final String TABLE_CHANNEL = "channel";
-   
-     public static final String ID = "id";
-     public static final String NAME = "name";  
-     public static final String ORDERID = "orderId";  
-     public static final String SELECTED = "selected";  
-     private Context context;  
-     public SQLHelper(Context context) {  
-         super(context, DB_NAME, null, VERSION);  
-         this.context = context;  
-     }  
-   
-     public Context getContext(){  
-         return context;  
-     }  
-       
-     @Override  
-     public void onCreate(SQLiteDatabase db) {  
-         
-         String sql = "create table if not exists "+TABLE_CHANNEL +  
-                 "(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +  
-                 ID + " INTEGER , " +  
-                 NAME + " TEXT , " +  
-                 ORDERID + " INTEGER , " +  
-                 SELECTED + " SELECTED)";  
-         db.execSQL(sql);  
-     }  
-   
-     @Override  
-     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
-         
-         onCreate(db);  
-     }  
-   
- }  
 
 
注:本DEMO中,加入了长按震动,所以在权限里面记得加上“ 
- <!-- 在SDCard中创建与删除文件权限 -->  
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  
- <!-- 往SDCard写入数据权限 -->  
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
- <!-- 震动权限 -->  
- <uses-permission android:name="android.permission.VIBRATE"/>  
 
 
六、源码下载
 
版权声明:本文为博主原创文章,未经博主允许不得转载。
 Android 高仿 频道管理----网易、今日头条、腾讯视频 (可以拖动的GridView)附源码DEMO
原文:http://www.cnblogs.com/Free-Thinker/p/4785075.html