TextView跑馬燈的兩種實現
普通的TextView可以實現跑馬燈,但是隻有當焦點在它上面時才有效。
如何做一個自動的跑馬燈呢?第一種:繼承TextView,然後重寫isFocused()方法就可以了,簡單!
Java程式碼
- import android.content.Context;
- import android.util.AttributeSet;
- import android.widget.TextView;
- publicclass ScrollForeverTextView extends TextView {
-
public ScrollForeverTextView(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- }
- public ScrollForeverTextView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public ScrollForeverTextView(Context context, AttributeSet attrs,
- int defStyle) {
-
super
- }
- @Override
- publicboolean isFocused() {
- returntrue;
- }
- }
使用時同TextView一樣:
Xml程式碼
- <com.ql.view.ScrollForeverTextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
-
android:textSize="30px"
- android:singleLine="true"
- android:ellipsize="marquee"
- android:marqueeRepeatLimit="marquee_forever"
- android:textColor="@color/red"
- android:text="1234567890wwwwwwwwwwwwwwwwwwwwww1234567890"
- android:focusable="true"
- />
第2種:還是繼承TextView,重寫onDraw(),在onDraw中不停的重繪。
Java程式碼
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.os.Parcel;
- import android.os.Parcelable;
- import android.util.AttributeSet;
- import android.view.Display;
- import android.view.View;
- import android.view.WindowManager;
- import android.view.View.OnClickListener;
- import android.widget.TextView;
- publicclass AutoScrollTextView extends TextView implements OnClickListener {
- publicfinalstatic String TAG = AutoScrollTextView.class.getSimpleName();
- privatefloat textLength = 0f;// 文字長度
- privatefloat viewWidth = 0f;
- privatefloat step = 0f;// 文字的橫座標
- privatefloat y = 0f;// 文字的縱座標
- privatefloat temp_view_plus_text_length = 0.0f;// 用於計算的臨時變數
- privatefloat temp_view_plus_two_text_length = 0.0f;// 用於計算的臨時變數
- publicboolean isStarting = false;// 是否開始滾動
- private Paint paint = null;// 繪圖樣式
- private CharSequence text = "";// 文字內容
- privatefloat speed = 0.5f;
- privateint textColor=0xFF000000;
- publicint getTextColor() {
- return textColor;
- }
- publicvoid setTextColor(int color) {
- this.textColor = color;
- }
- publicfloat getSpeed() {
- return speed;
- }
- publicvoid setSpeed(float speed) {
- this.speed = speed;
- }
- public AutoScrollTextView(Context context) {
- super(context);
- initView();
- }
- public AutoScrollTextView(Context context, AttributeSet attrs) {
- super(context, attrs);
- initView();
- }
- public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- initView();
- }
- privatevoid initView() {
- setOnClickListener(this);
- }
- publicvoid init(float width) {
- text=super.getText();
- paint = super.getPaint();
- // Paint paint=new Paint();
- text = getText().toString();
- textLength = paint.measureText(text.toString());
- // viewWidth = getWidth();
- // if (viewWidth == 0) {
- // if (windowManager != null) {
- // Display display = windowManager.getDefaultDisplay();
- // viewWidth = display.getWidth();
- // }
- // }
- viewWidth=width;
- step = textLength;
- temp_view_plus_text_length = viewWidth + textLength;
- temp_view_plus_two_text_length = viewWidth + textLength * 2;
- y = getTextSize() + getPaddingTop();
- paint.setColor(textColor);
- }
- @Override
- public Parcelable onSaveInstanceState() {
- Parcelable superState = super.onSaveInstanceState();
- SavedState ss = new SavedState(superState);
- ss.step = step;
- ss.isStarting = isStarting;
- return ss;
- }
- @Override
- publicvoid onRestoreInstanceState(Parcelable state) {
- if (!(state instanceof SavedState)) {
- super.onRestoreInstanceState(state);
- return;
- }
- SavedState ss = (SavedState) state;
- super.onRestoreInstanceState(ss.getSuperState());
- step = ss.step;
- isStarting = ss.isStarting;
- }
- publicstaticclass SavedState extends BaseSavedState {
- publicboolean isStarting = false;
- publicfloat step = 0.0f;
- SavedState(Parcelable superState) {
- super(superState);
- }
- @Override
- publicvoid writeToParcel(Parcel out, int flags) {
- super.writeToParcel(out, flags);
- out.writeBooleanArray(newboolean[] { isStarting });
- out.writeFloat(step);
- }
- publicstaticfinal Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
- public SavedState[] newArray(int size) {
- returnnew SavedState[size];
- }
- @Override
- public SavedState createFromParcel(Parcel in) {
- returnnew SavedState(in);
- }
- };
- private SavedState(Parcel in) {
- super(in);
- boolean[] b = null;
- in.readBooleanArray(b);
- if (b != null && b.length > 0)
- isStarting = b[0];
- step = in.readFloat();
- }
- }
- publicvoid startScroll() {
- isStarting = true;
- invalidate();
- }
- publicvoid stopScroll() {
- isStarting = false;
- invalidate();
- }
- @Override
- publicvoid onDraw(Canvas canvas) {
- // super.onDraw(canvas);
- canvas.drawText(text,0,text.length(), temp_view_plus_text_length - step, y, paint);
- if (!isStarting) {
- return;
- }
- step += speed;
- if (step > temp_view_plus_two_text_length)
- step = textLength;
- invalidate();
- }
- @Override
- publicvoid onClick(View v) {
- if (isStarting)
- stopScroll();
- else
- startScroll();
- }
- }
使用:
Java程式碼
- marquee = (AutoScrollTextView) findViewById(R.id.marquee);
- // marquee.setText(String.format(getResources().getString(R.string.marquee0),Consts.termno,"2010-12-28"));
- marquee.setText("上證指數3000.15 6.81(0.37%)深圳成指3000.15 6.81(0.37%)");
- // marquee.setTextColor(0xffff0000);//注意:顏色必須在這裡設定,xml中設定無效!預設黑色。
- //如果想改變跑馬燈的文字內容或者文字效果,則在呼叫完setText方法之後,需要再呼叫一下init(width)方法,重新進行初始化和相關引數的計算。
- marquee.setSpeed(1.5f);
- marquee.init(width);//width通常就是螢幕寬!
- marquee.startScroll();
這2種跑馬燈稍微有點區別,需要掂量著使用。
第1種跑馬燈不能設定速度,並且要超過一行才會滾動。
第2種跑馬燈只能設定單一顏色,且需要在程式碼中設定,不能同時設定多種顏色。速度設的不要過大(<= 2.0f),否則停頓現象比較明顯。
相關推薦
TextView跑馬燈-兩種實現方式
Android中TextView跑馬燈有多種實現方式; 一種是自定義控制元件,另外一種是寫一個工具類 自定義控制元件 /** * Created by iblade.Wang on 2018/10/24 10:29 */ public class Marqu
TextView跑馬燈的兩種實現
普通的TextView可以實現跑馬燈,但是隻有當焦點在它上面時才有效。 如何做一個自動的跑馬燈呢? 第一種:繼承TextView,然後重寫isFocused()方法就可以了,簡單! Java程式碼 import android.content.Context;
一、Android 實現兩個TextView跑馬燈效果
超長的文字在有限的佈局中,實現一行迴圈顯示?如果是隻有一個TextView實現跑馬燈效果可以簡單使用以下方式來實現: <TextView android:layout_width="wrap_content" android:layout
Android 文字自動滾動(跑馬燈)效果的兩種實現方法[特別好使]
public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(); }
Android 文字自動滾動(跑馬燈)效果的兩種實現方法
public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(); }
簡單實現TextVIew跑馬燈效果
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="莫聽穿林打葉聲,何妨吟嘯且徐行。竹杖芒鞋輕勝馬,誰怕?一蓑煙雨任平生。 "
Android TextView文字鏤空效果的兩種實現
一圖勝千言 文字鏤空效果主要有兩種實現方式: 1,自動義View,在canvas中繪製圓角矩形作為背景,然後繪製文字,通過PorterDuff.Mode.DST_OUT把背景擦除,實現鏤空效果。如上圖中的第一個。 2,自定義TextView,定義兩Bitmap,分別在Bitma
關於Android實現TextView跑馬燈效果
在xml屬性中設定 <TextView android:width="wrap_content" android:height="wrap_content" android:singleLine="true" andr
android學習:TextView 跑馬燈的實現
最近無意間看到了涉及到跑馬燈效果的程式碼,於是在網上查閱了很多資料,在這裡對自己看的一些文章進行一下總結,順便加上自己的一些體會。 讓我們一步步逐漸向下。 首先我們要實現走馬燈這樣一個效果,通常來說都是在TextView這個控制元件中來實現的,而且其中的文
左邊固定,右邊自適應布局的兩種實現
定位 abs red logs light 正常 idt blue mar html結構: <body> <div class="left"></div> <div class="right"></div
easyui combobox 三級級聯 input 兩種實現
data edit pat adc inpu idt cts wid req /**<img src=‘${pageContext.request.contextPath}/images/block.png‘/> * @param 默認載入 省市 */
CSS3實現五子棋Web小遊戲,Canvas畫布和DOM兩種實現,並且具有悔棋和撤銷悔棋功能。
posit oct padding 角色 sar pac osi fse ech 用Canvas實現五子棋的思路: 1、點擊棋盤,獲取坐標x,y,計算出棋子的二維數組坐標i和j, 2、棋子的實現,先arc一個圓,再填充漸變色。 3、下完一步棋後切換畫筆和角色。 4、贏法算法
[轉]Web APi之認證(Authentication)兩種實現方式【二】(十三)
用戶數 ted das 客戶 元素 基礎 目標 開始 net 本文轉自:http://www.cnblogs.com/CreateMyself/p/4857799.html 前言 上一節我們詳細講解了認證及其基本信息,這一節我們通過兩種不同方式來實現認證,並且分析如
android縮放動畫的兩種實現方法
get odi omx rac tor Coding eight rpo odin 在android開發。我們會常常使用到縮放動畫,普通情況下縮放動畫有兩種實現方式。一種是直接通過java代碼去實現,第二種是通過配置文件實現動畫,以下是兩種動畫的基本是用法: Ja
多線程兩種實現方式的區別
http [] tick 避免 main 單繼承 style 區別 tar 請解釋Thread類與Runnable接口實現多線程的區別?(請解釋多線程兩種實現方式的區別?) 1. Thread類時Runnable接口的子類,使用Runnable接口實現多線程可以避免單繼承局
JPA 派生標識符的兩種實現方式
string column public tid man pri one embed page 方法一:@Entity@IdClass(ModuleId.class)public class Module { @Id private Integer index;
斐波那契數列的兩種實現(遞歸和非遞歸)
result 數列 == 非遞歸 fib color 效率 i++ 思想 查找斐波納契數列中第 N 個數。 所謂的斐波納契數列是指: 前2個數是 0 和 1 。 第 i 個數是第 i-1 個數和第i-2 個數的和。 斐波納契數列的前10個數字是: 0, 1, 1, 2,
14、Fibonacci的兩種實現方式
等於 cheng pos art log ref clas gpo tar 斐波納契數列,又稱黃金分割數列,指的是這樣一個數列:1、1、2、3、5、8、13、21、……在數學上,斐波納契數列以如下被以遞歸的方法定義:F0=0,F1=1,Fn=F(n-1)+F(n-2)(n&
Web APi之認證(Authentication)兩種實現方式【二】(十三)
基於web 推薦 zed {0} scheme sage https 函數 ges 原文:Web APi之認證(Authentication)兩種實現方式【二】(十三)前言 上一節我們詳細講解了認證及其基本信息,這一節我們通過兩種不同方式來實現認證,並且分析如何合理的利用
spring ----> aop的兩種實現方式
select imp ack exe readv expr gpo for public 實現1:基於xml 1 package com.rr.spring3.interf; //接口 2 3 public interface SayHello { 4 5