⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 touchinterceptor.java

📁 Android平台上的media player, iPhone风格
💻 JAVA
字号:
/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.android.imusic;import android.content.Context;import android.content.SharedPreferences;import android.graphics.Bitmap;import android.graphics.PixelFormat;import android.graphics.Rect;import android.util.AttributeSet;import android.util.Log;import android.view.GestureDetector;import android.view.Gravity;import android.view.MotionEvent;import android.view.View;import android.view.ViewConfiguration;import android.view.ViewGroup;import android.view.WindowManager;import android.view.GestureDetector.SimpleOnGestureListener;import android.widget.AdapterView;import android.widget.ImageView;import android.widget.ListAdapter;import android.widget.ListView;public class TouchInterceptor extends ListView {        private View mDragView;    private WindowManager mWindowManager;    private WindowManager.LayoutParams mWindowParams;    private int mDragPos;      // which item is being dragged    private int mFirstDragPos; // where was the dragged item originally    private int mDragPoint;    // at what offset inset the item did the user grab it    private int mCoordOffset;  // the difference between screen coordinates and coordinates in this view    private DragListener mDragListener;    private DropListener mDropListener;    private RemoveListener mRemoveListener;    private int mUpperBound;    private int mLowerBound;    private int mHeight;    private int mTouchSlop;    private int mBackGroundColor;    private GestureDetector mGestureDetector;    private static final int FLING = 0;    private static final int SLIDE = 1;    private int mRemoveMode = -1;    private Rect mTempRect = new Rect();    public TouchInterceptor(Context context, AttributeSet attrs) {        super(context, attrs);        SharedPreferences pref = context.getSharedPreferences("Music", 3);        mRemoveMode = pref.getInt("deletemode", -1);    }        @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        if (mRemoveListener != null && mGestureDetector == null) {            if (mRemoveMode == FLING) {                mGestureDetector = new GestureDetector(new SimpleOnGestureListener() {                    @Override                    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,                            float velocityY) {                        if (mDragView != null) {                            if (velocityX > 1000) {                                Rect r = mTempRect;                                mDragView.getDrawingRect(r);                                if ( e2.getX() > r.right * 2 / 3) {                                    // fast fling right with release near the right edge of the screen                                    stopDragging();                                    mRemoveListener.remove(mFirstDragPos);                                    unExpandViews(true);                                }                            }                            // flinging while dragging should have no effect                            return true;                        }                        return false;                    }                });            }        }        if (mDragListener != null || mDropListener != null) {            switch (ev.getAction()) {                case MotionEvent.ACTION_DOWN:                    int x = (int) ev.getX();                    int y = (int) ev.getY();                    int itemnum = pointToPosition(x, y);                    if (itemnum == AdapterView.INVALID_POSITION) {                        break;                    }                    ViewGroup item = (ViewGroup) getChildAt(itemnum - getFirstVisiblePosition());                    mDragPoint = y - item.getTop();                    mCoordOffset = ((int)ev.getRawY()) - y;                    View dragger = item.findViewById(R.id.icon);                    Rect r = mTempRect;                    dragger.getDrawingRect(r);                    if (x < r.right) {                        item.setDrawingCacheEnabled(true);                        Bitmap bitmap = item.getDrawingCache();                        startDragging(bitmap, y);                        mDragPos = itemnum;                        mFirstDragPos = mDragPos;                        mHeight = getHeight();                        mTouchSlop = ViewConfiguration.getTouchSlop();                        mUpperBound = Math.min(y - mTouchSlop, mHeight / 3);                        mLowerBound = Math.max(y + mTouchSlop, mHeight * 2 /3);                        return false;                    }                    mDragView = null;                    break;            }        }        return super.onInterceptTouchEvent(ev);    }        /*     * pointToPosition() doesn't consider invisible views, but we     * need to, so implement a slightly different version.     */    private int myPointToPosition(int x, int y) {        Rect frame = mTempRect;        final int count = getChildCount();        for (int i = count - 1; i >= 0; i--) {            final View child = getChildAt(i);            child.getHitRect(frame);            if (frame.contains(x, y)) {                return getFirstVisiblePosition() + i;            }        }        return INVALID_POSITION;    }        private int getItemForPosition(int y) {        int pos = myPointToPosition(0, y - mDragPoint - 32);        if (pos >= 0) {            if (pos <= mFirstDragPos) {                pos += 1;            }        } else if ((y - mDragPoint) < 0) {            pos = 0;        }        return pos;    }        private void adjustScrollBounds(int y) {        if (y >= mHeight / 3) {            mUpperBound = mHeight / 3;        }        if (y <= mHeight * 2 / 3) {            mLowerBound = mHeight * 2 / 3;        }    }    /*     * Restore size and visibility for all listitems     */    private void unExpandViews(boolean deletion) {        for (int i = 0;; i++) {            View v = getChildAt(i);            if (v == null) {                if (deletion) {                    // HACK force update of mItemCount                    int position = getFirstVisiblePosition();                    int y = getChildAt(0).getTop();                    setAdapter(getAdapter());                    setSelectionFromTop(position, y);                    // end hack                }                layoutChildren(); // force children to be recreated where needed                v = getChildAt(i);                if (v == null) {                    break;                }            }            ViewGroup.LayoutParams params = v.getLayoutParams();            params.height = 64;            v.setLayoutParams(params);            v.setVisibility(View.VISIBLE);        }    }        /* Adjust visibility and size to make it appear as though     * an item is being dragged around and other items are making     * room for it:     * If dropping the item would result in it still being in the     * same place, then make the dragged listitem's size normal,     * but make the item invisible.     * Otherwise, if the dragged listitem is still on screen, make     * it as small as possible and expand the item below the insert     * point.     * If the dragged item is not on screen, only expand the item     * below the current insertpoint.     */    private void doExpansion() {        int childnum = mDragPos - getFirstVisiblePosition();        if (mDragPos > mFirstDragPos) {            childnum++;        }        View v = getChildAt(childnum);        if (v== null) {            return;        }        View first = getChildAt(mFirstDragPos - getFirstVisiblePosition());        for (int i = 0;; i++) {            View vv = getChildAt(i);            if (vv == null) {                break;            }            int height = 64;            int visibility = View.VISIBLE;            if (vv.equals(first)) {                // processing the item that is being dragged                if (mDragPos == mFirstDragPos) {                    // hovering over the original location                    visibility = View.INVISIBLE;                } else {                    // not hovering over it                    height = 1;                }            } else if (i == (mDragPos - getFirstVisiblePosition() + (mDragPos > mFirstDragPos ? 1 : 0))) {                height = 128;            }            ViewGroup.LayoutParams params = vv.getLayoutParams();            params.height = height;            vv.setLayoutParams(params);            vv.setVisibility(visibility);        }    }        @Override    public boolean onTouchEvent(MotionEvent ev) {        if (mGestureDetector != null) {            mGestureDetector.onTouchEvent(ev);        }        if ((mDragListener != null || mDropListener != null) && mDragView != null) {            switch (ev.getAction()) {                case MotionEvent.ACTION_UP:                case MotionEvent.ACTION_CANCEL:                    Rect r = mTempRect;                    mDragView.getDrawingRect(r);                    stopDragging();                    if (mRemoveMode == SLIDE && ev.getX() > r.right * 3 / 4) {                        if (mRemoveListener != null) {                            mRemoveListener.remove(mFirstDragPos);                        }                        unExpandViews(true);                    } else {                        if (mDropListener != null) {                            mDropListener.drop(mFirstDragPos, mDragPos);                        }                        unExpandViews(false);                    }                    break;                                    case MotionEvent.ACTION_MOVE:                    int x = (int) ev.getX();                    int y = (int) ev.getY();                    dragView(x, y);                    int itemnum = getItemForPosition(y);                    if (itemnum >= 0) {                        if (true || itemnum != mDragPos) {                            if (mDragListener != null) {                                mDragListener.drag(mDragPos, itemnum);                            }                            mDragPos = itemnum;                            doExpansion();                        }                        int speed = 0;                        adjustScrollBounds(y);                        if (y > mLowerBound) {                            // scroll the list up a bit                            speed = y > (mHeight + mLowerBound) / 2 ? 16 : 4;                        } else if (y < mUpperBound) {                            // scroll the list down a bit                            speed = y < mUpperBound / 2 ? -16 : -4;                        }                        if (speed != 0) {                            int ref = pointToPosition(0, mHeight / 2);                            if (ref == AdapterView.INVALID_POSITION) {                                //we hit a divider or an invisible view, check somewhere else                                ref = pointToPosition(0, mHeight / 2 + getDividerHeight() + 64);                            }                            View v = getChildAt(ref - getFirstVisiblePosition());                            if (v!= null) {                                int pos = v.getTop();                                setSelectionFromTop(ref, pos - speed);                            }                        }                    }                    break;            }            return true;        }        return super.onTouchEvent(ev);    }        private void startDragging(Bitmap bm, int y) {        mWindowParams = new WindowManager.LayoutParams();        mWindowParams.gravity = Gravity.TOP;        mWindowParams.x = 0;        mWindowParams.y = y - mDragPoint + mCoordOffset;        mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;        mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;        mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;        mWindowParams.format = PixelFormat.TRANSLUCENT;        mWindowParams.windowAnimations = 0;                ImageView v = new ImageView(this.getContext());        mBackGroundColor = this.getContext().getResources().getColor(R.color.dragndrop_background);        v.setBackgroundColor(mBackGroundColor);        v.setImageBitmap(bm);        mWindowManager = (WindowManager)this.getContext().getSystemService("window");        mWindowManager.addView(v, mWindowParams);        mDragView = v;    }        private void dragView(int x, int y) {        if (mRemoveMode == SLIDE) {            float alpha = 1.0f;            int width = mDragView.getWidth();            if (x > width / 2) {                alpha = ((float)(width - x)) / (width / 2);            }            mWindowParams.alpha = alpha;        }        mWindowParams.y = y - mDragPoint + mCoordOffset;        mWindowManager.updateViewLayout(mDragView, mWindowParams);    }        private void stopDragging() {        WindowManager wm = (WindowManager)this.getContext().getSystemService("window");        wm.removeView(mDragView);        mDragView = null;    }        public void setDragListener(DragListener l) {        mDragListener = l;    }        public void setDropListener(DropListener l) {        mDropListener = l;    }        public void setRemoveListener(RemoveListener l) {        mRemoveListener = l;    }    public interface DragListener {        void drag(int from, int to);    }    public interface DropListener {        void drop(int from, int to);    }    public interface RemoveListener {        void remove(int which);    }};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -