Android拖拽教程

摘要:
Android拖放教程原始地址开发环境:Eclipse 3.7、Java 1.6和Android 4.0前言:Android 4.0开始支持视图拖放。在第九行代码中,DragShadowBuilder允许您指定拖动操作的效果。通常直接在视图上拖动。在代码的第二部分,我们将定义一个拖放区域。可以将定义的视图拖到视图上。我们通过setOnDragListener()方法在拖放区域视图上注册一个OnDragListen侦听器。在OnDragListener侦听器中,可以定义相关的事件处理方法。1findViewById。setOnDragListener;23classMyDragListener实现OnDragListener{4DrawableenterShape=getResources().getDrawable;5DrawablenormalShape=getResources().get-Drawable;67@Override8publicbooleanonDrag{9intaction=event.getAction();10switch{11caseDrageEvent.ACTION_DRAG_STARTED:12//Donothing13break;14caseDrageTrageEvent:ACTION_DRANG_ENTERED:15v.setBackgroundDrawable;16break;17caseDrageDrage_EXITED:18v.setBackgroundDrawable;19break;20caseDragEvent.ACTION_DROP:21//已删除,重新分配ViewtoViewGroup22Viewview=event.get-LocalState();23ViewGroupwner=view.getParent() ; 24所有者。removeView;25LinearLayoutcontainer=v;26容器。addView;27视图。setVisibility;28断裂;29caseDragEvent。动作_ DRAG_结束:30v。setBackgroundDrawable;31默认值:32中断;33}34返回true;35}36}教程练习1.创建一个新的AndroidProject-˃projectname:“de.vogella.android.dragandrop”-˃createactivity:“DragActivity”2.在项目的res目录下创建一个“可绘制”文件夹,并创建文件“shape.xml”˂?
Android拖拽教程

原文地址



开发环境:

    Eclipse 3.7(Indigo) 、Java 1.6 和 Android 4.0(Ice Cream Sandwich) 前言:

    Android 4.0 开始支持视图(Views)的拖拽。你需要在视图(View)上注册一个监听器(Listener),比如 OnTouchListener 或者 LongClickListener,并且需要定义一个视图(View)作为拖拽的目标视图对象,比如 拖放区(drop zones)。下面我们通过两段代码来学习一下。

    第一段代码我们定义一个视图,你可以拖拽该视图,我们在该视图上使用setOnTouchListener方法注册了一个OnTouchListener事件。

复制代码
 1 // Assign the touch listener to your view which you want to move
 2 findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
 3 
 4 // This defines your touch listener
 5 private final class MyTouchListener implements OnTouchListener {
 6   public boolean onTouch(View view, MotionEvent motionEvent) {
 7     if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
 8       ClipData data = ClipData.newPlainText("", "");
 9       DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
10       view.startDrag(data, shadowBuilder, view, 0);
11       view.setVisibility(View.INVISIBLE);
12       return true;
13     } else {
14     return false;
15     }
16   }
17 }
复制代码

    上面的第 8 行代码,我们构造了一个 ClipData 对象,可以使用该对象来存放数据,并且该对象可以被处于拖放区(drop zones)的视图接收,即 ClipData 可用于视图拖拽前后数据的传递。第 9 行代码,DragShadowBuilder 允许你指定拖拽操作的效果,通常你直接在视图上进行拖拽操作。

    第二段代码我们将定义一个拖放区(drag zone),你可以将定义好的视图拖拽到该视图上,我们在该拖放区视图上通过 setOnDragListener() 方法注册了一个 OnDragListener 监听器,在 OnDragListener 监听器中你可以定义相关的事件处理方法。

复制代码
 1 findViewById(R.id.bottomright).setOnDragListener(new MyDragListener());
 2 
 3 class MyDragListener implements OnDragListener {
 4   Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
 5   Drawable normalShape = getResources().getDrawable(R.drawable.shape);
 6   
 7   @Override
 8   public boolean onDrag(View v, DragEvent event) {
 9     int action = event.getAction();
10     switch (event.getAction()) {
11     case DragEvent.ACTION_DRAG_STARTED:
12     // Do nothing
13       break;
14     case DragEvent.ACTION_DRAG_ENTERED:
15       v.setBackgroundDrawable(enterShape);
16       break;
17     case DragEvent.ACTION_DRAG_EXITED:        
18       v.setBackgroundDrawable(normalShape);
19       break;
20     case DragEvent.ACTION_DROP:
21       // Dropped, reassign View to ViewGroup
22       View view = (View) event.getLocalState();
23       ViewGroup owner = (ViewGroup) view.getParent();
24       owner.removeView(view);
25       LinearLayout container = (LinearLayout) v;
26       container.addView(view);
27       view.setVisibility(View.VISIBLE);
28       break;
29     case DragEvent.ACTION_DRAG_ENDED:
30       v.setBackgroundDrawable(normalShape);
31       default:
32       break;
33     }
34     return true;
35   }
36 }
复制代码

教程实践

1. 创建一个新的 Android Project

    -> project name: "de.vogella.android.draganddrop"

    -> create activity: "DragActivity"

2. 在项目的res目录下创建一个 "drawable" 文件夹,在该文件夹中创建文件 "shape.xml"

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape
="rectangle" >
    <stroke
        
android:width="2dp"
        android:color
="#FFFFFFFF" />
    <gradient
        
android:angle="225"
        android:endColor
="#DD2ECCFA"
        android:startColor
="#DD000000" />
    <corners
        
android:bottomLeftRadius="7dp"
        android:bottomRightRadius
="7dp"
        android:topLeftRadius
="7dp"
        android:topRightRadius
="7dp" />
</shape>
复制代码

3. 在 "drawable" 文件夹中创建文件 "shape_droptarget.xml"

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape
="rectangle" >
    <stroke
        
android:width="2dp"
        android:color
="#FFFF0000" />
    <gradient
        
android:angle="225"
        android:endColor
="#DD2ECCFA"
        android:startColor
="#DD000000" />
    <corners
        
android:bottomLeftRadius="7dp"
        android:bottomRightRadius
="7dp"
        android:topLeftRadius
="7dp"
        android:topRightRadius
="7dp" />
</shape>
复制代码

4. 按照下面代码修改"res/layout/main.xml"文件

复制代码
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width
="match_parent"
    android:layout_height
="match_parent"
    android:columnCount
="2"
    android:columnWidth
="300dp"
    android:orientation
="vertical"
    android:rowCount
="2"
    android:stretchMode
="columnWidth" >

    <LinearLayout
        
android:id="@+id/topleft"
        android:layout_width
="160dp"
        android:layout_height
="200dp"
        android:layout_column
="0"
        android:layout_row
="0"
        android:background
="@drawable/shape" >

        <ImageView
            
android:id="@+id/myimage1"
            android:layout_width
="wrap_content"
            android:layout_height
="wrap_content"
            android:layout_column
="0"
            android:layout_row
="0"
            android:src
="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        
android:id="@+id/topright"
        android:layout_width
="160dp"
        android:layout_height
="200dp"
        android:layout_column
="1"
        android:layout_row
="0"
        android:background
="@drawable/shape" >

        <ImageView
            
android:id="@+id/myimage2"
            android:layout_width
="wrap_content"
            android:layout_height
="wrap_content"
            android:layout_column
="0"
            android:layout_row
="0"
            android:src
="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        
android:id="@+id/bottomleft"
        android:layout_width
="160dp"
        android:layout_height
="200dp"
        android:layout_column
="0"
        android:layout_row
="1"
        android:background
="@drawable/shape" >

        <ImageView
            
android:id="@+id/myimage3"
            android:layout_width
="wrap_content"
            android:layout_height
="wrap_content"
            android:src
="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        
android:id="@+id/bottomright"
        android:layout_width
="160dp"
        android:layout_height
="200dp"
        android:layout_column
="1"
        android:layout_row
="1"
        android:background
="@drawable/shape" >

        <ImageView
            
android:id="@+id/myimage4"
            android:layout_width
="wrap_content"
            android:layout_height
="wrap_content"
            android:layout_column
="0"
            android:layout_row
="0"
            android:src
="@drawable/ic_launcher" />
    </LinearLayout>

</GridLayout>
复制代码

5. 修改 Activity文件

复制代码
package de.vogella.android.draganddrop;

import android.app.Activity;
import android.content.ClipData;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class DragActivity extends Activity {
  
/** Called when the activity is first created. */

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.myimage4).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.topleft).setOnDragListener(new MyDragListener());
    findViewById(R.id.topright).setOnDragListener(new MyDragListener());
    findViewById(R.id.bottomleft).setOnDragListener(new MyDragListener());
    findViewById(R.id.bottomright).setOnDragListener(new MyDragListener());

  }

  private final class MyTouchListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
      if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        ClipData data = ClipData.newPlainText("", "");
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(data, shadowBuilder, view, 0);
        view.setVisibility(View.INVISIBLE);
        return true;
      } else {
        return false;
      }
    }
  }

  class MyDragListener implements OnDragListener {
    Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
    Drawable normalShape = getResources().getDrawable(R.drawable.shape);

    @Override
    public boolean onDrag(View v, DragEvent event) {
      int action = event.getAction();
      switch (event.getAction()) {
      case DragEvent.ACTION_DRAG_STARTED:
        // Do nothing
        break;
      case DragEvent.ACTION_DRAG_ENTERED:
        v.setBackgroundDrawable(enterShape);
        break;
      case DragEvent.ACTION_DRAG_EXITED:
        v.setBackgroundDrawable(normalShape);
        break;
      case DragEvent.ACTION_DROP:
        // Dropped, reassign View to ViewGroup
        View view = (View) event.getLocalState();
        ViewGroup owner = (ViewGroup) view.getParent();
        owner.removeView(view);
        LinearLayout container = (LinearLayout) v;
        container.addView(view);
        view.setVisibility(View.VISIBLE);
        break;
      case DragEvent.ACTION_DRAG_ENDED:
        v.setBackgroundDrawable(normalShape);
      default:
        break;
      }
      return true;
    }
  }
}
复制代码

6. 启动你创建的Activity,你就可以拖拽 ImageViews 到另一个容器里面。

 Android拖拽教程第13张

参考链接:

http://www.vogella.com/articles/AndroidDragAndDrop/article.html

免责声明:文章转载自《Android拖拽教程》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇vue项目加载前空白的动画过渡效果DateTimeField *** received a naive datetime (***) while time zone support is active下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

[置顶] xamarin android使用gps定位获取经纬度

看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位、基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用的,肯定是无法获取获取位置信息(当然也肯定是有办法解决这种问题的) 3.android 的GPS定位需要一定时间的,大概一分钟左右。 4.andro...

android动态申请权限

申请权限对于安卓开发很重要,从Android6.0开始,Android系统提供动态申请权限的机制, APP在使用危险权限时,需要用户的授权才可进一步操作。所以这一篇文章介绍如何动态申请权限。非常简单。 一、在AndroidManifest.xml配置所需要的权限,代码如下 <?xml version="1.0" encoding="utf-8"?&g...

(转)AppiumLibrary基本操作

*** Settings ***Library AppiumLibraryLibrary CollectionsLibrary StringLibrary Dialogs*** Test Cases ***打开appComment Open Applicationhttp://localhost:4723/wd/hubalias=tudouapp plat...

安卓渗透和审计工具整理

1.cSploit: https://github.com/cSploit/android/releases 2.DroidSheephttp://bbs.zhiyoo.com/thread-13249611-1-1.html 3.androrathttps://github.com/wszf/androrat 4.Network Spoofhttps:/...

&amp;lt;Android基础&amp;gt;(一)

第一章Android 2003年10月,Andy Rubin等人创办了Android公司。2005年8月谷歌收购。 1.1 Android全貌 1.1.1 Android系统架构 1.Linux内核层:为Android设备的各种硬件提供底层驱动。如:显示驱动,音频驱动,蓝牙驱动,WiFi驱动,电源管理。 2.系统运行库层:c/c++库为Android层提供...

Android基础——广播(静态注册)

安卓版本高了就会有点问题,不能静态注册  令活动Main用来发广播,另一个接收器(不是Activity而是receiver)用来接收广播 注册文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/r...