Android實現拖拽GridView到目標View

來源:互聯網
上載者:User

[java] 
package cn.com.weather.serviceDemo.view; 
 
import cn.com.weather.serviceDemo.interfaces.OnDropListener; 
import cn.com.weather.serviceDemo.util.DeviceUtil; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Matrix; 
import android.graphics.PixelFormat; 
import android.util.AttributeSet; 
import android.view.Gravity; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.WindowManager; 
import android.widget.AdapterView; 
import android.widget.GridView; 
import android.widget.ImageView; 
 
public class MyGridView extends GridView{ 
     
    private ImageView dragImageView; 
    private WindowManager windowManager; 
    private WindowManager.LayoutParams windowParams; 
     
    private int dragSrcPosition;//開始拖拽項 
    private int dragPosition; 
     
    //移動的位置 
    private int dragPointX; 
    private int dragPointY; 
    //當前位置距離邊界的位置 
    private int dragOffsetX; 
    private int dragOffsetY; 
     
    private View myView;//目標view 
    private int dropPosition = -1;//選中項 
     
    private OnDropListener onDropListener; 
 
    public MyGridView(Context context, AttributeSet attrs) { 
        super(context, attrs); 
    } 
     
    public void setView(View view){ 
        myView = view; 
    } 
     
    public void setOnDropListener(OnDropListener onDropListener){ 
        this.onDropListener = onDropListener; 
    } 
     
    private boolean setOnItemLongClickListener(final MotionEvent ev){ 
        this.setOnItemLongClickListener(new OnItemLongClickListener() { 
 
            @Override 
            public boolean onItemLongClick(AdapterView<?> parent, View view, 
                    int position, long id) { 
                final int x = (int)ev.getX();//觸摸點相對於GridView的座標  
                final int y = (int)ev.getY(); 
                 
                dragSrcPosition = dragPosition = position; 
                 
                if(dragPosition == AdapterView.INVALID_POSITION ){ 
                    return false; 
                } 
                 
                final ViewGroup item = (ViewGroup)getChildAt(dragPosition - getFirstVisiblePosition()); 
                 
                dragPointX = x - item.getLeft(); 
                dragPointY = y - item.getTop(); 
                dragOffsetX = (int)ev.getRawX() - x; 
                dragOffsetY = (int)ev.getRawY() - y; 
                 
                item.destroyDrawingCache();//清除cache 
                item.setDrawingCacheEnabled(true); 
                Bitmap bitmap = Bitmap.createBitmap(item.getDrawingCache()); 
                startDrag(getScaleBitmap(bitmap), x, y); 
                DeviceUtil.Vibrate(getContext(), 100); 
                return false; 
            } 
        }); 
         
        return super.onInterceptTouchEvent(ev); 
    } 
 
    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
        if(ev.getAction() == MotionEvent.ACTION_DOWN){ 
            return setOnItemLongClickListener(ev); 
        } 
        return super.onInterceptTouchEvent(ev); 
    } 
     
    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 
        if(dragImageView != null && dragPosition != AdapterView.INVALID_POSITION){ 
            int x = (int)ev.getX(); 
            int y = (int)ev.getY(); 
            switch (ev.getAction()) { 
            case MotionEvent.ACTION_UP: 
                stopDrag(); 
                onDrop((int)ev.getRawX(), (int)ev.getRawY()); 
                break; 
 
            case MotionEvent.ACTION_MOVE: 
                onDrag(x, y); 
                break; 
            } 
             
             
        } 
        return super.onTouchEvent(ev); 
    } 
 
    private void startDrag(Bitmap bm, int x, int y){ 
        stopDrag(); 
         
        windowParams = new WindowManager.LayoutParams(); 
        windowParams.gravity = Gravity.TOP|Gravity.LEFT; 
        //左上方相對於螢幕的座標 
        windowParams.x = x - dragPointX + dragOffsetX; 
        windowParams.y = y - dragPointY + dragOffsetY; 
         
        windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT; 
        windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT; 
        windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 
                                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE 
                                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON 
                                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; 
        windowParams.format = PixelFormat.TRANSLUCENT; 
        windowParams.windowAnimations = 0; 
         
        ImageView imageView = new ImageView(this.getContext()); 
        imageView.setImageBitmap(bm); 
         
        windowManager = (WindowManager)this.getContext().getSystemService(Context.WINDOW_SERVICE); 
        windowManager.addView(imageView, windowParams); 
        dragImageView = imageView; 
    } 
     
    private void stopDrag(){ 
        if (dragImageView != null) { 
            windowManager.removeView(dragImageView); 
            dragImageView = null; 
        } 
    } 
     
    private void onDrag(int x, int y){ 
        if(dragImageView != null){ 
            windowParams.alpha = 0.8f; 
            windowParams.x = x - dragPointX + dragOffsetX; 
            windowParams.y = y - dragPointY + dragOffsetY; 
            windowManager.updateViewLayout(dragImageView, windowParams); 
        } 
         
    } 
     
    private void onDrop(int x, int y){ 
        boolean inView = isInView(x, y, myView); 
        if(inView){ 
            if(onDropListener!=null){ 
                onDropListener.onDrop(dragSrcPosition); 
            } 
        } 
    } 
     
    private Bitmap getScaleBitmap(Bitmap bitmap){ 
        Matrix matrix = new Matrix(); 
        matrix.postScale(1.1f, 1.1f); 
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
    } 
     
    private boolean isInView(int x, int y, View view){ 
        if(view == null) return false; 
         
        int[] location = new int[2]; 
        view.getLocationInWindow(location); 
        int _x = location[0]; 
        int _y = location[1]; 
        int width = view.getWidth(); 
        int height = view.getHeight(); 
         
        if(_x <x && x <_x+width && _y<y && y<_y+height){ 
            return true; 
        } 
        return false; 
    } 
     
     
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.