关闭

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

标签: 重写RadioButton类获得屏幕宽度xml布局组件宽度为屏宽Android跑马灯横滑HorizontalScorll类
203人阅读 评论(0) 收藏 举报
分类:


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

   找了很多都是,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技术(个人)

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:12657次
    • 积分:422
    • 等级:
    • 排名:千里之外
    • 原创:27篇
    • 转载:26篇
    • 译文:0篇
    • 评论:0条
    文章存档