Drag and drop : Drag Drop « UI « Android






Drag and drop

   

package app.test;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.app.Fragment;
import android.content.ClipData;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.CycleInterpolator;
import android.widget.TextView;

class Dot extends View implements View.OnDragListener {
  private static final int DEFAULT_RADIUS = 50;
  private static final int DEFAULT_COLOR = Color.WHITE;
  private static final int SELECTED_COLOR = Color.MAGENTA;
  protected static final String DOTTAG = "DragDot";
  private Paint mNormalPaint;
  private Paint mDraggingPaint;
  private int mColor = DEFAULT_COLOR;
  private int mRadius = DEFAULT_RADIUS;
  private boolean inDrag;

  public Dot(Context context, AttributeSet attrs) {
    super(context, attrs);
    mNormalPaint = new Paint();
    mNormalPaint.setColor(mColor);
    mNormalPaint.setAntiAlias(true);

    mDraggingPaint = new Paint();
    mDraggingPaint.setColor(SELECTED_COLOR);
    mDraggingPaint.setAntiAlias(true);
    setOnLongClickListener(lcListener);
    setOnDragListener(this);
  }

  private static View.OnLongClickListener lcListener = new View.OnLongClickListener() {
    private boolean mDragInProgress;

    public boolean onLongClick(View v) {
      ClipData data = ClipData.newPlainText("DragData",
          (String) v.getTag());
      mDragInProgress = v.startDrag(data, new View.DragShadowBuilder(v),
          (Object) v, 0);
      Log.v((String) v.getTag(), "starting drag? " + mDragInProgress);
      return true;
    }
  };

  @Override
  protected void onMeasure(int widthSpec, int heightSpec) {
    int size = 2 * mRadius + getPaddingLeft() + getPaddingRight();
    setMeasuredDimension(size, size);
  }

  public boolean onDrag(View v, DragEvent event) {
    String dotTAG = (String) getTag();
    if (event.getLocalState() != this) {
      return false;
    }
    boolean result = true;
    int action = event.getAction();
    float x = event.getX();
    float y = event.getY();
    switch (action) {
    case DragEvent.ACTION_DRAG_STARTED:
      inDrag = true;
      break;
    case DragEvent.ACTION_DRAG_LOCATION:
      break;
    case DragEvent.ACTION_DRAG_ENTERED:
      break;
    case DragEvent.ACTION_DRAG_EXITED:
      break;
    case DragEvent.ACTION_DROP:
      result = false;
      break;
    case DragEvent.ACTION_DRAG_ENDED:
      inDrag = false; // change color of original dot back
      break;
    default:
      result = false;
      break;
    }
    return result;
  }

  public void draw(Canvas canvas) {
    float cx = this.getWidth() / 2 + getLeftPaddingOffset();
    float cy = this.getHeight() / 2 + getTopPaddingOffset();
    Paint paint = mNormalPaint;
    if (inDrag)
      paint = mDraggingPaint;
    canvas.drawCircle(cx, cy, mRadius, paint);
    invalidate();
  }
}

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

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle icicle) {
    View v = inflater.inflate(R.layout.row, container, false);
    dropMessage = (TextView) v.findViewById(R.id.dropmessage);
    dropTarget = (View) v.findViewById(R.id.droptarget);
    dropTarget.setOnDragListener(new View.OnDragListener() {
      private static final String DROPTAG = "DropTarget";
      private int dropCount = 0;
      private ObjectAnimator anim;

      public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();
        boolean result = true;
        switch (action) {
        case DragEvent.ACTION_DRAG_STARTED:
          break;
        case DragEvent.ACTION_DRAG_ENTERED:
          anim = ObjectAnimator
              .ofFloat((Object) v, "alpha", 1f, 0.5f);
          anim.setInterpolator(new CycleInterpolator(40));
          anim.setDuration(30 * 1000);
          anim.start();
          break;
        case DragEvent.ACTION_DRAG_EXITED:
          if (anim != null) {
            anim.end();
            anim = null;
          }
          break;
        case DragEvent.ACTION_DRAG_LOCATION:
          Log.v(DROPTAG,
              "drag proceeding in dropTarget: " + event.getX()
                  + ", " + event.getY());
          break;
        case DragEvent.ACTION_DROP:
          Log.v(DROPTAG, "drag drop in dropTarget");
          if (anim != null) {
            anim.end();
            anim = null;
          }
          ClipData data = event.getClipData();
          Log.v(DROPTAG, "Item data is "
              + data.getItemAt(0).getText());
          dropCount++;
          String message = dropCount + " drop";
          if (dropCount > 1)
            message += "s";
          dropMessage.setText(message);
          break;
        case DragEvent.ACTION_DRAG_ENDED:
          Log.v(DROPTAG, "drag ended in dropTarget");
          if (anim != null) {
            anim.end();
            anim = null;
          }
          break;
        default:
          Log.v(DROPTAG, "other action in dropzone: " + action);
          result = false;
        }
        return result;
      }
    });
    return v;
  }
}

class Palette extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle icicle) {
    View v = inflater.inflate(R.layout.add_edit, container, false);
    return v;
  }
}

public class Test extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }
}

//layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <fragment class="app.test.TitlesFragment"
            android:id="@+id/titles"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
            android:background="#00550033" />

    <FrameLayout
            android:id="@+id/details" android:layout_weight="2"
            android:layout_width="0px"
            android:layout_height="match_parent" />
</LinearLayout>

//row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  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"
    android:text="0 drops"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:paddingLeft="50dp"
    android:textSize="17sp" />

</LinearLayout>


//add_edit.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is res/layout/palette.xml -->
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:dot="http://schemas.android.com/apk/res/app.test.demo"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <app.test.Dot android:id="@+id/dot1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="30dp"
    android:tag="Blue dot"
    dot:color="#ff1111ff"
    dot:radius="20dp"
  />

  <app.test.Dot android:id="@+id/dot2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:tag="White dot"
    dot:color="#ffffffff"
    dot:radius="40dp"
  />

</LinearLayout>

   
    
    
  








Related examples in the same category

1.Draggable dot
2.Drag and move a dot