Pro Android学习笔记(一四九):拖拽(3):Android3.0及后的方式(中)

文章转载只能用于非商业性质,且不能带有虚拟货币、积分、注册等附加条件。转载须注明出处http://blog.csdn.net/flowingflying/以及作者@恺风Wei。

代码:DropZone

DropZone是下的fragment,目标方块将检测是否有对象被拖拽进来。我们先看看这部分的layout.xml,左右水平摆放,左边是绿色的方块,右边是一个用于计算的TextView,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout … …   android:orientation="horizontal" >     
  <View android:id="@+id/droptarget"
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:layout_gravity="center_vertical"
    android:background="#00ff00" />

  <TextView android:id="@+id/dropmessage" …… /> 
</LinearLayout>

DropZone.java代码如下:

public class DropZone extends Fragment
    private View dropTarget;
    private TextView dropMessage;

   
    @Override //由于是fragment,所以需要重写onCreateView()
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //【1】根据layout文件inflate view,并作为onCreateView()的返回值
        View v = inflater.inflate(R.layout.dropzone, container, false);        

        dropTarget = v.findViewById(R.id.droptarget);
        dropMessage = (TextView) v.findViewById(R.id.dropmessage);

        
        //【2】对目标对象(左边绿色方块)设置监听器,监听drag事件
        dropTarget.setOnDragListener(new OnDragListener() {

            private  static final String DROPTAG = "DropTarget";
            private int dropCount = 0;
            private ObjectAnimator anim ;

           
            @Override/*【2.1】监听器只有一个回调函数onDrag(),重写。onDrage带有两个参数,view是接收DragEvent的对象,本例为绿色小方块dropTraget。由于可以检测到所有拖拽事件,可先判断是否是我们关心的view。返回true表示我们关心这次拖拽动作,返回false表示不关心这次拖拽动作,后续的除了动作结束ended外都不需要再通知我。*/
            public boolean onDrag(View v, DragEvent event) {

                boolean result = true;
                Log.v(DROPTAG,v.toString());
                Log.v(DROPTAG,event.toString());

               
                /*【2.1.1】根据action进行相应的处理,通常不需要处理view的位置变化,一般处理 进入区域(ACTION_DRAG_ENTERED)和离开区域(ACTION_DRAG_EXITED),以及关键的拖拽确定(ACTION_DROP)。本例在进入区域时进行动画(渐变) 处理,离开时恢复 */
                switch(event.getAction()){
                case DragEvent.ACTION_DRAG_STARTED:
                   Log.v(DROPTAG,"Drag Started");
                    break;

                   
                /*【2.1.2】 当有感兴趣的拖拽进入时,方块进行一些动画动作,表示原因接收。本例用过alpha的改变,视觉上出现一些闪烁。这个动作需要在离开(exited),确定拖拽(droped),以及结束(ended,应该会经历exited和droped两个状态,要安全起见,可以在结束时再次确保停止动画)时确保结束动画动作*/
                case DragEvent.ACTION_DRAG_ENTERED:
                    Log.v(DROPTAG,"Drag eneter");
                    anim = ObjectAnimator.ofFloat((Object)v, "alpha", 1f,0.5f);
                    anim.setInterpolator(new CycleInterpolator(40));
                    anim.setDuration(30*1000);
                    anim.start();
                    break;
               
                //对于拖拽动作,只有进入方块的有效范围,才开始触发location,即在entered之后
               case DragEvent.ACTION_DRAG_LOCATION:
                    Log.v(DROPTAG,"drag proceeding: " + event.getX() + "," + event.getY());
                    break;

               
                case DragEvent.ACTION_DRAG_EXITED:
                    Log.v(DROPTAG,"Drag exited");
                    if(anim != null){
                        anim.end();
                        anim = null;
                    }
                    break;
               
                /*【2.1.3】在方块的有效范围内,确定发生了拖拽动作。本例子,我们见结束方块的动画(见2.2.1),检查携带的信息ClipData,进行相应的操作(计数器加一)*/
                case DragEvent.ACTION_DROP:
                   Log.v(DROPTAG,"Drag drop");
                    if(anim != null){
                        anim.end();
                        anim = null;
                    }
                    ClipData data = event.getClipData();
                    Log.v(DROPTAG,"Item data is " + data.getItemAt(0).getText());
                    dropCount ++;
                    dropMessage.setText("" + dropCount + " drop" + (dropCount == 1 ? "":"s"));
                    break;
               
                case DragEvent.ACTION_DRAG_ENDED:
                    Log.v(DROPTAG,"Drag ended");
                    if(anim != null){
                        anim.end();
                        anim = null;
                    }
                    break;
               
                default:
                    result = false;
                    break;
                }
               
                return result;
            }
        });
       
        return v;
    }
   
}

相关小例子代码:Pro Android学习:拖拽小例子

相关链接:我的Android开发相关文章

你可能感兴趣的:(Pro Android学习笔记(一四九):拖拽(3):Android3.0及后的方式(中))