zoukankan      html  css  js  c++  java
  • Android事件详解——拖放事件DragEvent

    1、Android拖放框架的作用?

        利用Android的拖放框架,可以让用户用拖放手势把一个View中的数据移到当前layout内的另一个View中去。

     

    2、拖放框架的内容?

        1)拖放事件类

        2)拖放监听器

        3)其他辅助的方法和类

     

    3、拖放过程?

        拖放过程有四个基本步骤:

        1)启动 为了响应用户开始拖动的手势,需要调用View的startDrag方法来通知系统。startDrag方法的参数需要指定所拖动的数据、元数据和绘制拖动阴影的回调方法。

    作为响应,系统首先通过回调来获取拖动阴影,然后在设备上显示这个阴影。

     

    4、例子

    长按某个View,进行拖动操作。

    1)创建用于移动数据的ClipData和ClipData.Item。在ClipData对象中,需要给出存放元数据的ClipDescription对象。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    private static final String IMAGEVIEW_TAG = "icon bitmap"
     
    ImageView imageView = new ImageView(this);
    imageView.setImageBitmap(mIconBitmap);
    imageView.setTag(IMAGEVIEW_TAG);
     
        ...
     
    imageView.setOnLongClickListener(new View.OnLongClickListener() {
     
        public boolean onLongClick(View v) {
     
            ClipData.Item item = new ClipData.Item(v.getTag());
            ClipData dragData = new ClipData(v.getTag(),ClipData.MIMETYPE_TEXT_PLAIN,item);
            View.DragShadowBuilder myShadow = new MyDragShadowBuilder(imageView);
            v.startDrag(dragData,  // the data to be dragged
                        myShadow,  // the drag shadow builder
                        null,      // no need to use local data
                        0          // flags (not currently used, set to 0)
            );
        }
    });

     

    2)定义MyDragShadowBuilder

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
     private static class MyDragShadowBuilder extends View.DragShadowBuilder {
     
        private static Drawable shadow;
     
        public MyDragShadowBuilder(View v) {
            super(v);
            shadow = new ColorDrawable(Color.LTGRAY);
        }
     
        @Override
        public void onProvideShadowMetrics (Point size, Point touch) {
            private int width, height;
            width = getView().getWidth() / 2;
            height = getView().getHeight() / 2;
     
            shadow.setBounds(00, width, height);
            size.set(width, height);
            touch.set(width / 2, height / 2);
        }
     
        @Override
        public void onDrawShadow(Canvas canvas) {
            shadow.draw(canvas);
        }
    }
  • 相关阅读:
    SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程'sys.sp_OACreate' 的访问
    谷歌浏览器扩展程序manifest.json参数详解
    获取天气api
    UVA 10385 Duathlon
    UVA 10668 Expanding Rods
    UVALIVE 3891 The Teacher's Side of Math
    UVA 11149 Power of Matrix
    UVA 10655 Contemplation! Algebra
    UVA 11210 Chinese Mahjong
    UVA 11384 Help is needed for Dexter
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4803493.html
Copyright © 2011-2022 走看看