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>
- android 源码——packages/apps/Music学习一
- android 源码——packages/apps/Music学习二
- android music源码学习
- android music源码学习
- android music源码学习
- android Music 源码学习
- 集成android源码中的 packages/apps 到eclipse 比如launcher2
- (转)将 \packages\apps 下的 android 源码导入Eclipse编译的方法
- Android中music播放器源码要点详解【安卓music源码进化一】
- Android中music播放器源码要点详解【安卓music源码进化一】
- android 源码 Music 学习过程(随笔版)(ing)
- android7.1编译第三方apk到系统中的android.mk学习 基于packages/apps/Browser/Android.mk文件内容,如下: LOCAL_PATH := $(my-d
- android源码解析------Music 音乐播放器
- Android源码学习(一)
- Android 源码里packages里面的结构
- Android源码里packages里面的结构
- Android源码里packages里面的结构
- Android源码目录结构之packages目录
- 常见的视频接口介绍,VGA,YPbPr,DVI,HDMI,DisplayPort
- hibernate annotation 复合主键写法
- 关于Inline hook check的一些想法与问题
- oracle中的Schema
- aes padding method
- android 源码——packages/apps/Music学习一
- iOS 视图控制器(UIViewController)剖析
- All Git Object Types: Blob, Tree, Commit And Tag
- 让你的C程序更有效率的10种方法
- CPen类
- Android手机: 软件静默安装
- iOS: 自定义navigation bar
- Iphone文件读写操作
- 软件测试转型之路