Android 实现跑马灯显示数条标题

原创 2015年07月08日 17:35:59


    在网速很少数条标题以跑马灯形式的文档,

   找了很多都是,TextView的如下:代码

<TextView
    android:id="@+id/test"
    android:layout_width="68dp"
    android:layout_height="wrap_content"
    android:background="#339320"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text="这才是真正的文字跑马灯效果"
    android:textColor="#000000"
    android:textSize="20dp"
    />

    以为公司要实现一个功能: 取  快讯直播 前5条最新数据 从右往左滚动(以跑马灯形式滑动),展现在首页
   进入首页和板块切换时 实现数据更新)

   弄了一天,而且还利用udp实时更新内容,不过有的内容属于公司的隐私的代码就省略了一些,

    也不影响跑马灯功能的使用,不YY了,先贴出一张效果图吧


先贴出一个类HSVRadioButton.java(重写RadioButton类,也可以是其他的组件)


<span style="font-size:14px;">package com.fx678.finace.activitys;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.RadioButton;

/**
 * Created by lobin on 2015/7/8.
 */
public class HSVRadioButton extends RadioButton {

    public HSVRadioButton(Context context) {
        super(context);
    }

    public HSVRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HSVRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public HSVRadioButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        init_screenpara();
        setWidth( screenWidth );//每个组件宽度都是屏宽,xml布局里面不要定义宽度,这个类就此一个用处
    }
    /**
     * 屏幕宽度,
     */
    private int screenWidth;
    /**
     * 获得屏幕宽度
     */
    private void init_screenpara() {
        DisplayMetrics dm = new DisplayMetrics();
        dm = getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
    }
}
</span>

现在该activity出场了,不YY了,一看就明白的代码如下:

<span style="font-size:14px;">package com.fx678.finace.activitys;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.fx678.finace.R;
import com.fx678.finace.data.Const;
import com.fx678.finace.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lobin on 2015/7/7.
 */

public class HorizontalScrollTest extends ActionBarActivity {


    private List<FreshNew> msgdata;

    @Override
    protected void onPause() {
        super.onPause();
        handler.removeMessages(MSG_WHAT.MARQUEE_MOVE);
    }
    protected void onResume() {
        super.onResume();
        handler.sendEmptyMessageDelayed(MSG_WHAT.MARQUEE_MOVE, 2000);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.horizont_scroll_test);

        init_screenpara();
        init_marketlist();
        isFristScroll = true;

        currentScrollX = 0;
        scorllTextButton[ 0 ] = (RadioButton) findViewById(scorllTextId[ 0 ]);
        scorllTextButton[ 0 ].setText( scorllTextTitle[ 0 ]);
    }

    private boolean isFristScroll = false;
    int waitTime = 0;
    int waitTimeChang = 0;
    int scorllTextTitleNum = -1;
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case MSG_WHAT.MARQUEE_MOVE:
                    if(isFristScroll){
                        isFristScroll = false;
                        thireadSleep(2000);
                        waitTime += 1;
                    }
                    if ( currentScrollX / screenWidth  == waitTime ) {
                        thireadSleep(2000);
                        waitTime += 1;

                    }
                    if( currentScrollX * 2 / screenWidth == 9) {
                        isFristScroll = true;
                        waitTime = 0;
                        currentScrollX = 0;
                    }
                    currentScrollX += 2;// 滚动速度
                    marketlist_hsv.scrollTo(currentScrollX, 0);
                    handler.sendEmptyMessage(MSG_WHAT.MARQUEE_MOVE);
                    break;
                default:
                    break;
            }
        }
    };
    private RadioButton scorll_text1,scorll_text2,scorll_text3,scorll_text4,scorll_text5;
    private RadioButton[] scorllTextButton = { scorll_text1, scorll_text2, scorll_text3, scorll_text4, scorll_text5 };
    private int[] scorllTextId = { R.id.scorll_text1, R.id.scorll_text2, R.id.scorll_text3, R.id.scorll_text4, R.id.scorll_text5 };
    private String[] scorllTextTitle = { "scorll_text1","scorll_text2","scorll_text3","scorll_text4","scorll_text5" };
    private HorizontalScrollView marketlist_hsv;
    private RadioGroup marklist_rg;
    private int currentScrollX = 0;// 当前滚动的位置
    int setTextTime = 0;
    private void thireadSleep(long timeSleep){
        try {
            Thread.sleep(timeSleep);
            if( waitTime == 4){
                setTextTime = 0;
            }else {
                setTextTime = waitTime + 1;
            }

            scorllTextButton[ setTextTime ] = (RadioButton) findViewById(scorllTextId[ setTextTime ]);
            scorllTextButton[ setTextTime ].setText( scorllTextTitle[ setTextTime ]);
            //scorllTextButton[ setTextTime ].setChecked(true);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    private void init_marketlist() {
        marketlist_hsv = (HorizontalScrollView) findViewById(R.id.marklist_hsv);
        marketlist_hsv.setOnTouchListener(new View.OnTouchListener() {
            int mPosX,mPosY,mCurrentPosX,mCurrentPosY;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                /*if(MotionEvent.ACTION_DOWN==event.getAction()){
                    mPosX = (int)event.getX();
                    mPosY = (int)event.getY();
                }
                if (MotionEvent.ACTION_MOVE == event.getAction()) {
                    mCurrentPosX = (int)event.getX()-mPosX;
                    mCurrentPosY = (int)event.getY()-mPosY;
                    mPosX = (int)event.getX();
                    mPosY = (int)event.getY();
                }
                if (mCurrentPosX - mPosX > 0 && Math.abs(mCurrentPosY - mPosY) < 10){
                    //Log.e("", "向右的按下位置"+mPosX+"移动位置"+mCurrentPosX);
                }else if (mCurrentPosX - mPosX < 0 && Math.abs(mCurrentPosY - mPosY) < 10 ){
                    //Log.e("", "向左的按下位置"+mPosX+"移动位置"+mCurrentPosX);
                }else if (mCurrentPosY - mPosY > 0 && Math.abs(mCurrentPosX - mPosX) < 10){
                    //Log.e("", "向下的按下位置"+mPosX+"移动位置"+mCurrentPosX);
                }else if (mCurrentPosY - mPosY < 0 && Math.abs(mCurrentPosX - mPosX) < 10){
                    //Log.e("", "向上的按下位置"+mPosX+"移动位置"+mCurrentPosX);
                }*/
                return false;//不让用户滑动跑马灯
            }
        });
        marklist_rg = (RadioGroup) findViewById(R.id.marklist_rg);
        marklist_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.scorll_text1:
                        Toast.makeText(HorizontalScrollTest.this,"waitTime:"+waitTime+",id:scorll_text1",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.scorll_text2:
                        Toast.makeText(HorizontalScrollTest.this,"waitTime:"+waitTime+",id:scorll_text2",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.scorll_text3:
                        Toast.makeText(HorizontalScrollTest.this,"waitTime:"+waitTime+",id:scorll_text3",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.scorll_text4:
                        Toast.makeText(HorizontalScrollTest.this,"waitTime:"+waitTime+",id:scorll_text4",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.scorll_text5:
                        Toast.makeText(HorizontalScrollTest.this,"waitTime:"+waitTime+",id:scorll_text5",Toast.LENGTH_LONG).show();
                        break;
                    default:
                        break;
                }
            }
        });
    }
    /**
     * 屏幕宽度
     */
    private int screenWidth;
    /**
     * 获得屏幕宽度
     */
    private void init_screenpara() {
        WindowManager manage = getWindowManager();
        Display display = manage.getDefaultDisplay();
        screenWidth = display.getWidth();
    }
    /**
     * 根据当前点击的位置,计算需要滑动的距离
     */
    private int gotoX(int position) {
        float unit = DensityUtil.dip2px(this, 120);
        float dx = position * unit - unit / 2 - screenWidth / 2;
        if (dx > 0) {
            return (int) dx;
        } else {
            return 0;
        }
    }
}</span>
剩下的就是布局了xml文件如下:
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:background="@color/activity_dark"
                android:layout_height="match_parent">


    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        style="@style/my_awesome_toolbar"
        android:id="@+id/my_awesome_toolbar"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <TextView
            android:id="@+id/title"
            style="@style/titlebar_title"
            android:layout_gravity="center"
            android:text="汇通财经"/>

    </android.support.v7.widget.Toolbar>

    <HorizontalScrollView
        android:id="@+id/marklist_hsv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/my_awesome_toolbar"
        android:layout_marginTop="120dp"
        android:scrollbars="none">

        <RadioGroup
            android:id="@+id/marklist_rg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="5"
            android:orientation="horizontal">

            <com.fx678.finace.activitys.HSVRadioButton
                android:id="@+id/scorll_text1"
                style="@style/horizont_scroll_test2"
                android:background="@drawable/quate01"
                android:text="@string/marketlist_01"/><!-- quate01 和 quate02 是两张背景图片-->

            <com.fx678.finace.activitys.HSVRadioButton
                android:id="@+id/scorll_text2"
                style="@style/horizont_scroll_test2"
                android:background="@drawable/quate02"
                android:text="@string/marketlist_02"/>

            <com.fx678.finace.activitys.HSVRadioButton
                android:id="@+id/scorll_text3"
                style="@style/horizont_scroll_test2"
                android:background="@drawable/quate01"
                android:text="@string/marketlist_03"/>

            <com.fx678.finace.activitys.HSVRadioButton
                android:id="@+id/scorll_text4"
                style="@style/horizont_scroll_test2"
                android:background="@drawable/quate02"
                android:text="@string/marketlist_04"/>

            <com.fx678.finace.activitys.HSVRadioButton
                android:id="@+id/scorll_text5"
                style="@style/horizont_scroll_test2"
                android:background="@drawable/quate01"
                android:text="@string/marketlist_05"/>
            <com.fx678.finace.activitys.HSVRadioButton
                android:id="@+id/scorll_text6"
                style="@style/horizont_scroll_test2"
                android:background="@drawable/quate02"
                android:text=""/>
        </RadioGroup>
    </HorizontalScrollView>

</RelativeLayout></span>

还有一个style样式,差点忘了

<span style="font-size:14px;">    <style name="horizont_scroll_test2">
        <item name="android:layout_height">wrap_content</item>
        <item name="android:button">@null</item>
        <item name="android:gravity">center</item>
        <item name="android:textColor">#ffffff</item>
        <item name="android:paddingLeft">16</item>
        <item name="android:paddingRight">16</item>
        <item name="android:maxLines">1</item>
        <item name="android:paddingTop">6dp</item>
        <item name="android:paddingBottom">6dp</item>
        <item name="android:textSize">14sp</item>
    </style></span>


怎么样,实现了吧,那就收藏吧  ^_^。

本文结束,来自汇通财经Android技术(个人)

listview实现跑马灯效果,和焦点获取

ListView中经常需要让条目出现动画效果,跑马灯就是一种。 listview可以基于万能适配器开发,地址: http://blog.csdn.net/lmj623565791/article/...

android自定义进度值可拖动的seekbar

最近忙找实习,加上实验室在推新项目,需要学习新知识。所以很长一段时间没去整理了官博客了,github也蛮久没更新,很惭愧。接下来还是要坚持写。今天就简单的写一下我在项目中用到的算自定义seekbar的...

Android listview 表格显示和自动循环显示

在Android中,有时候也需要使用如HTML的表格一样显示数据。 Android没有直接提供表格控件,但可通过其他方式实现,一样可以达到预期的效果。 数据量固定、单元格等宽的可以使用GridVi...

Android仿淘宝头条竖直跑马灯式新闻标题及“分页思想”总结

在淘宝App的首页中间位置,有一块小小的地方在不知疲倦地循坏滚动着头条标题(见下图的红框区域),这样的设计无疑能够在有限的手机屏幕上展示更丰富的内容。而实现这一功能需要用到的控件就是我在上一篇文章中提...

Android实现多个TextView同时显示跑马灯效果

摘rt  最近被一个页面弄得很蛋疼,这个页面比较小,需要显示较长的文案时无法显示完全,于是很自然地想到了TextView中的marquee —— 跑马灯效果,可是Android执行跑马灯效果需要...
  • mxbhxx
  • mxbhxx
  • 2014年06月21日 16:13
  • 1081

Android实现文字显示跑马灯效果

当个TextView实现跑马灯效果

Android实现多个TextView同时显示跑马灯效果

转自:http://www.cnblogs.com/zealotrouge/archive/2013/04/19/3031373.html 最近被一个页面弄得很蛋疼,这个页面比较小,需要显示较长...

标题文字跑马灯效果实现

自定义控件达到跑马灯效果 自定义控件编写流程 创建一个默认就能获取焦点的TextView 1.创建一个类继承至TextView,FocusTextView 2.重写其构造方法 //使用在通过jav...

遮罩clippingNode实现标题闪亮特效、类似机关枪瞄准效果、跑马灯效果

参考:http://blog.csdn.net/star530/article/details/20851263 /* 可以用ClippingNode做游戏的新手引导、实现标题闪亮...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android 实现跑马灯显示数条标题
举报原因:
原因补充:

(最多只允许输入30个字)