textview设置一些属性,就可以弄成跑马灯。但是如果字符长度不够长。。。。你会发现怎么设置都没效果。因此我google好久,参考了别人的代码,终于找到合适自己几种解决方法。我总结下。还是国外的程序员厉害,国内搜来搜去都是千遍一律的文章!
1、
<LinearLayout
android:id="@+id/ticker_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
public void setticker(String text, Context contx) {
if (text != "") {
LinearLayout parent_layout = (LinearLayout) ((Activity) contx)
.findViewById(R.id.ticker_area);
TextView view = new TextView(contx);
view.setText(text);
view.setTextColor(Color.RED);
view.setTextSize(15.0F);
Context context = view.getContext(); // gets the context of the view
// measures the unconstrained size of the view
// before it is drawn in the layout
view.measure(View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);
// takes the unconstrained width of the view
float width = view.getMeasuredWidth();
float height = view.getMeasuredHeight();
// gets the screen width
float screenWidth = ((Activity) context).getWindowManager()
.getDefaultDisplay().getWidth();
view.setLayoutParams(new LinearLayout.LayoutParams((int) width,
(int) height, 1f));
System.out.println("width and screenwidth are" + width + "/"
+ screenWidth + "///" + view.getMeasuredWidth());
// performs the calculation
float toXDelta = width - (screenWidth - 0);
// sets toXDelta to -300 if the text width is smaller that the
// screen size
if (toXDelta < 0) {
toXDelta = 0 - screenWidth;// -300;
} else {
toXDelta = 0 - screenWidth - toXDelta;// -300 - toXDelta;
}
// Animation parameters
Animation mAnimation = new TranslateAnimation(screenWidth,
toXDelta, 0, 0);
mAnimation.setDuration(15000);
mAnimation.setRepeatMode(Animation.RESTART);
mAnimation.setRepeatCount(Animation.INFINITE);
view.setAnimation(mAnimation);
parent_layout.addView(view);
}
}
调用:
setticker("最新消息",context);
2、
webView = (WebView)findViewById(R.id.web);
String summary = "<html><FONT color='#fdb728' FACE='courier'><marquee behavior='scroll' direction='left' scrollamount=10>"
+ "Hello Droid" + "</marquee></FONT></html>";
webView.loadData(summary, "text/html", "utf-8");
3、这种是我自己想出来的,就是用动画方式,跟第一种类似,
translate_scroll.xml。
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="10000"
android:fromXDelta="-100%p"
android:interpolator="@android:anim/anticipate_interpolator"
android:toXDelta="100%p"
/>
调用:
Animation animation = AnimationUtils.loadAnimation(this.currentlyAssociatedActivity, R.anim.translate_scroll);
animation.setRepeatMode(Animation.RESTART);
animation.setRepeatCount(Animation.INFINITE);
titleView.setAnimation(animation);