天天看点

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

            public boolean onDrag(View v, DragEvent event) {

                boolean result = true;

                Log.v(DROPTAG,v.toString());

                Log.v(DROPTAG,event.toString());

                switch(event.getAction()){

                case DragEvent.ACTION_DRAG_STARTED:

                   Log.v(DROPTAG,"Drag Started");

                    break;

                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;

                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开发相关文章