android 源码——packages/apps/Music学习一

来源:互联网 发布:50知天命是什么意思 编辑:程序博客网 时间:2024/06/05 23:16

在音乐播放器的源码MediaPlaybackActivity.java中有这么一个功能, 就是实现当TextView的text长度大于view的长度时对text的拖拽,并在停止拖拽一定时间后恢复原位。如下图。



其代码如下:


    int mInitialX = -1;    int mLastX = -1;    int mTextWidth = 0;    int mViewWidth = 0;    boolean mDraggingLabel = false;        //此函数通过父Container来获取子TextView    TextView textViewForContainer(View v) {        View vv = v.findViewById(R.id.artistname);        if (vv != null) return (TextView) vv;        vv = v.findViewById(R.id.albumname);        if (vv != null) return (TextView) vv;        vv = v.findViewById(R.id.trackname);        if (vv != null) return (TextView) vv;        return null;    }@Override    public boolean onTouch(View v, MotionEvent event) {        int action = event.getAction();        TextView tv = textViewForContainer(v);        if (tv == null) {            return false;        }        if (action == MotionEvent.ACTION_DOWN) {            v.setBackgroundColor(0xff606060);            mInitialX = mLastX = (int) event.getX();            mDraggingLabel = false;        } else if (action == MotionEvent.ACTION_UP ||                action == MotionEvent.ACTION_CANCEL) {            v.setBackgroundColor(0);            if (mDraggingLabel) {                Message msg = mLabelScroller.obtainMessage(0, tv);                mLabelScroller.sendMessageDelayed(msg, 1000);            }        } else if (action == MotionEvent.ACTION_MOVE) {            if (mDraggingLabel) {                int scrollx = tv.getScrollX();                int x = (int) event.getX();                int delta = mLastX - x;                if (delta != 0) {                    mLastX = x;                    scrollx += delta;                    if (scrollx > mTextWidth) {                        // scrolled the text completely off the view to the left                        scrollx -= mTextWidth;                        scrollx -= mViewWidth;                    }                    if (scrollx < -mViewWidth) {                        // scrolled the text completely off the view to the right                        scrollx += mViewWidth;                        scrollx += mTextWidth;                    }                    tv.scrollTo(scrollx, 0);                }                return true;            }            int delta = mInitialX - (int) event.getX();            if (Math.abs(delta) > mTouchSlop) {                // start moving                mLabelScroller.removeMessages(0, tv);                                // Only turn ellipsizing off when it's not already off, because it                // causes the scroll position to be reset to 0.                //因为如果setEllipsize了以后,scroll的位置就会被置0了,这就是为什么当我们放开手后text会自动返回。                if (tv.getEllipsize() != null) {                    tv.setEllipsize(null);                }                Layout ll = tv.getLayout();                // layout might be null if the text just changed, or ellipsizing                // was just turned off                if (ll == null) {                    return false;                }                // get the non-ellipsized line width, to determine whether scrolling                // should even be allowed                mTextWidth = (int) tv.getLayout().getLineWidth(0);                mViewWidth = tv.getWidth();                if (mViewWidth > mTextWidth) {                    tv.setEllipsize(TruncateAt.END);                    v.cancelLongPress();                    return false;                }                mDraggingLabel = true;                tv.setHorizontalFadingEdgeEnabled(true);                v.cancelLongPress();                return true;            }        }        return false;     }    Handler mLabelScroller = new Handler() {        @Override        public void handleMessage(Message msg) {            TextView tv = (TextView) msg.obj;            int x = tv.getScrollX();            //这里每次移动到原来的3/4,移动间隔为15,直到x为0            x = x * 3 / 4;            tv.scrollTo(x, 0);            if (x == 0) {                tv.setEllipsize(TruncateAt.END);            } else {                Message newmsg = obtainMessage(0, tv);                mLabelScroller.sendMessageDelayed(newmsg, 15);            }        }    };


    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:baselineAligned="false"        android:paddingLeft="11dip"        android:paddingTop="4dip"        android:paddingBottom="8dip">        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginRight="4dip"            android:src="@drawable/ic_mp_artist_playback" />        <TextView android:id="@+id/artistname"            android:textSize="18sp"            android:singleLine="true"            android:ellipsize="end"            android:textStyle="bold"            android:layout_gravity="center_vertical"            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:baselineAligned="false"        android:paddingLeft="11dip"        android:paddingTop="4dip"        android:paddingBottom="8dip">        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginRight="4dip"            android:src="@drawable/ic_mp_album_playback" />        <TextView android:id="@+id/albumname"            android:textSize="14sp"            android:singleLine="true"            android:ellipsize="end"            android:layout_gravity="center_vertical"            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:baselineAligned="false"        android:paddingLeft="11dip"        android:paddingTop="0dip"        android:paddingBottom="8dip">        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginRight="4dip"            android:src="@drawable/ic_mp_song_playback" />        <TextView android:id="@+id/trackname"            android:textSize="14sp"            android:singleLine="true"            android:ellipsize="end"            android:layout_gravity="center_vertical"            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </LinearLayout>