拖动视图,DragShadowBuilder使用

首先自定义一个view继承ImageView

package com.example.dragview;

import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.DragEvent;
import android.view.View;
import android.view.View.OnDragListener;
import android.widget.ImageView;

@SuppressLint("NewApi")
public class DropTargetView extends ImageView implements OnDragListener{


    private boolean mDropped;

    public DropTargetView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        init();
    }

    public DropTargetView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        init();
    }

    public DropTargetView(Context context, AttributeSet attrs, int defaultStyle) {
        super(context, attrs, defaultStyle);
        // TODO Auto-generated constructor stub
        init();
    }

    @SuppressLint("NewApi")
    private void init(){
        setOnDragListener(this);
    }

    @Override
    public boolean onDrag(View v, DragEvent event) {
        // TODO Auto-generated method stub
        PropertyValuesHolder pvhx, pvhy;
        switch(event.getAction()){
        case DragEvent.ACTION_DRAG_STARTED:
            pvhx = PropertyValuesHolder.ofFloat("scaleX", 0.5f);
            pvhy = PropertyValuesHolder.ofFloat("scaleY", 0.5f);
            ObjectAnimator.ofPropertyValuesHolder(this, pvhx, pvhy).start();
            setImageDrawable(null);
            mDropped = false;
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            if(!mDropped){
                pvhx = PropertyValuesHolder.ofFloat("scaleX", 1f);
                pvhy = PropertyValuesHolder.ofFloat("scaleY", 1f);
                ObjectAnimator.ofPropertyValuesHolder(this, pvhx, pvhy).start();
                mDropped = false;
            }
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            pvhx = PropertyValuesHolder.ofFloat("scaleX", 0.75f);
            pvhy = PropertyValuesHolder.ofFloat("scaleY", 0.75f);
            ObjectAnimator.ofPropertyValuesHolder(this, pvhx, pvhy).start();
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            pvhx = PropertyValuesHolder.ofFloat("scaleX", 0.5f);
            pvhy = PropertyValuesHolder.ofFloat("scaleY", 0.5f);
            ObjectAnimator.ofPropertyValuesHolder(this, pvhx, pvhy).start();
            break;
        case DragEvent.ACTION_DROP:
            Keyframe frame0 = Keyframe.ofFloat(0f, 0.75f);
            Keyframe frame1 = Keyframe.ofFloat(0.5f, 0f);
            Keyframe frame2 = Keyframe.ofFloat(1f, 0.75f);
            pvhx = PropertyValuesHolder.ofKeyframe("scaleX", frame0, frame1, frame2);
            pvhy = PropertyValuesHolder.ofKeyframe("scaleY", frame0, frame1, frame2);   
            ObjectAnimator.ofPropertyValuesHolder(this, pvhx, pvhy).start();
            setImageDrawable((Drawable)event.getLocalState());
            mDropped = true;
            break;
        default:
            return false;
        }
        return true;
    }

}

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.dragview.MainActivity" >

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <ImageView 
            android:id="@+id/image1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher"/>
        <ImageView 
            android:id="@+id/image2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher"/>
        <ImageView 
            android:id="@+id/image3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher"/>

    LinearLayout>

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <com.example.dragview.DropTargetView
            android:id="@+id/drag_target1"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:background="#A00"/>
          <com.example.dragview.DropTargetView
            android:id="@+id/drag_target2"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:background="#0A0"/>
            <com.example.dragview.DropTargetView
            android:id="@+id/drag_target3"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:background="#00A"/>
    LinearLayout>

RelativeLayout>

main Activity

package com.example.dragview;

import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity implements OnLongClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.image1).setOnLongClickListener(this);
        findViewById(R.id.image2).setOnLongClickListener(this);
        findViewById(R.id.image3).setOnLongClickListener(this);

    }


    @SuppressLint("NewApi")
    @Override
    public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        DragShadowBuilder shadowBuilder = new DragShadowBuilder(v);
        v.startDrag(null, shadowBuilder, ((ImageView) v).getDrawable(), 0);

        return true;
    }
}

你可能感兴趣的:(Android学习)