以前在调TextVie的跑马灯效果的时候,总没有成功,网上很多人都说是TextView没有获取焦点导致。
以下效果再android4.4上测试:
1------最基本的跑马灯xml---无效果----
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
2-------点击才有效果-----------
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
android:clickable="true"
3.--------无效果-------
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
4.--------最理想最完美实现跑马灯,采用重载TextView-------
<bluetoothsoc.serialport.view.ScrollingTextView android:id="@+id/albumname"
android:textSize="30sp"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
package bluetoothsoc.serialport.view;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;
public class ScrollingTextView extends TextView {
public ScrollingTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ScrollingTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScrollingTextView(Context context) {
super(context);
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
if(focused)
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
@Override
public void onWindowFocusChanged(boolean focused) {
if(focused)
super.onWindowFocusChanged(focused);
}
@Override
public boolean isFocused() {
return true;
}
}
本文介绍了在Android中实现跑马灯效果的多种方法,并详细对比了不同配置下TextView的表现。通过自定义ScrollingTextView类,可以稳定实现理想的跑马灯滚动效果。
654

被折叠的 条评论
为什么被折叠?



