textview 跑马灯

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);




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值