android自定義View實(shí)現(xiàn)跑馬燈效果
android自帶的TextView可以實(shí)現(xiàn)跑馬燈效果,但是有很多的局限性;比如需要設(shè)置ellipsize=”marquee”,獲取 focusable=”true”,設(shè)置singleLine=”true”,控件里的內(nèi)容需要超過(guò)控件本身的長(zhǎng)度,無(wú)法控制滾動(dòng)速度和滾動(dòng)暫停和繼續(xù)滾動(dòng)功能,各種限制導(dǎo)致用起來(lái)特別不順手,幾乎無(wú)法使用到生產(chǎn)環(huán)境中,在此背景下,需要自定義View實(shí)現(xiàn)跑馬燈效果。
使用主要方法:自定義View重寫onDraw方法,通過(guò)canvas.drawText()方法來(lái)顯示文字,利用Handler不斷地繪制文字,并控制文字開(kāi)始繪制的X軸的位置,來(lái)實(shí)現(xiàn)連續(xù)滾動(dòng)的效果。
實(shí)現(xiàn)步驟:
1、自定義view的class:
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
public class MarqueenView extends View {
/*
* 1、自定義View的屬性 2、在View的構(gòu)造方法中獲得我們自定義的屬性 [ 3、重寫onMesure ] 4、重寫onDraw
*/
/*
* 文本
*/
private String mTitleText;
/**
* 文本的顏色
*/
private int mTitleTextColor;
/**
* 文本的大小
*/
private int mTitleTextSize;
/**
* 繪制時(shí)控制文本繪制的范圍
*/
private Rect mBound, usualBound;
// 畫筆
private Paint mPaint;
private int spead = 15;
private int length = 3;
private int currentLength;
@SuppressLint("HandlerLeak")
private Handler handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
if (mBound.width() <= getWidth()) {
if (currentLength >= 120 + (getWidth() / length) * length) {
currentLength = length;
} else {
currentLength = currentLength + length;
}
} else {
if (currentLength >= (mBound.width() + 120)) {
currentLength = length;
} else {
currentLength = currentLength + length;
}
}
invalidate();
handler.sendEmptyMessageDelayed(0, spead);
};
};
public MarqueenView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
currentLength = length;
}
public MarqueenView(Context context) {
this(context, null);
}
public MarqueenView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defStyle, 0);
int n = a.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.CustomTitleView_titleText:
mTitleText = a.getString(attr);
break;
case R.styleable.CustomTitleView_titleTextColor:
mTitleTextColor = a.getColor(attr, Color.BLACK);
break;
case R.styleable.CustomTitleView_titleTextSize:
mTitleTextSize = a.getDimensionPixelSize(attr,
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
break;
}
}
a.recycle();
mPaint = new Paint();
mPaint.setTextSize(mTitleTextSize);
mBound = new Rect();
usualBound = new Rect();
mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
mPaint.getTextBounds("1234567890QqYy你好", 0, 16, usualBound);
}
/*
* EXACTLY:一般是設(shè)置了明確的值或者是MATCH_PARENT AT_MOST:表示子布局限制在一個(gè)最大值內(nèi),一般為WARP_CONTENT
* UNSPECIFIED:表示子布局想要多大就多大,很少使用
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height;
if (widthMode == MeasureSpec.EXACTLY) {
width = widthSize;
} else {
mPaint.setTextSize(mTitleTextSize);
mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
float textWidth = mBound.width();// 字體寬度
// 控件padding
int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());
width = desired;
}
if (heightMode == MeasureSpec.EXACTLY) {
height = heightSize;
} else {
mPaint.setTextSize(mTitleTextSize);
mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
float textHeight = mBound.height();
int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());
height = desired;
}
// setMeasuredDimension((width / length) * length, height);
setMeasuredDimension(width, height + usualBound.bottom);
}
@Override
protected void onDraw(Canvas canvas) {
mPaint.setColor(mTitleTextColor);
if (mBound.width() <= getWidth()) {
if (currentLength < mBound.width()) {
canvas.drawText(mTitleText, getPaddingLeft() - currentLength, getHeight() - usualBound.bottom, mPaint);
}
if (currentLength >= 120) {
canvas.drawText(mTitleText, getWidth() - currentLength + 120, getHeight() - usualBound.bottom, mPaint);
}
} else {
if (currentLength < mBound.width()) {
canvas.drawText(mTitleText, getPaddingLeft() - currentLength, getHeight() - usualBound.bottom, mPaint);
}
if (currentLength >= mBound.width() - getWidth() + 120) {
canvas.drawText(mTitleText, 120 - currentLength + mBound.width(), getHeight() - usualBound.bottom, mPaint);
}
}
}
public static int getFontHeight(Integer textSize) {
Paint paint = new Paint();
if (textSize != null) {
paint.setTextSize(textSize);
}
FontMetrics fm = paint.getFontMetrics();
return (int) (fm.descent - fm.ascent);
}
public void startScroll() {
handler.removeMessages(0);
handler.sendEmptyMessage(0);
}
public void stopScroll() {
handler.removeMessages(0);
currentLength = 0;
invalidate();
}
public void setScrollLength(int length) {
this.length = length;
currentLength = length;
}
public void setSpead(int spead) {
this.spead = spead;
}
public void setText(String msg) {
mTitleText = msg;
mBound = new Rect();
mPaint.getTextBounds(msg, 0, mTitleText.length(), mBound);
}
}
2、在values的attrs.xml文件中需要加入自定義的屬性,字體大小、顏色、和初始內(nèi)容,這里有待改進(jìn)
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="titleText" format="string" /> <attr name="titleTextColor" format="color" /> <attr name="titleTextSize" format="dimension" /> <declare-styleable name="CustomTitleView"> <attr name="titleText" /> <attr name="titleTextColor" /> <attr name="titleTextSize" /> </declare-styleable> </resources>
3、在layout的布局文件中引用此控件
<?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/***這里寫上自己的包名***" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.example.MarqueenView android:id="@+id/main_marquee_view" android:layout_width="fill_parent" android:layout_height="wrap_content" custom:titleText=" " custom:titleTextColor="#666666" custom:titleTextSize="14sp"/> </LinearLayout>
4、上面準(zhǔn)備好后,就可以在activity中設(shè)置跑馬燈并開(kāi)始滾動(dòng)了
mv = (MarqueenView) findViewById(R.id.main_marquee_view);
mv.stopScroll();
mv.setText("滾動(dòng)內(nèi)容");// 設(shè)置顯示內(nèi)容
//mv.setSpead(15);//滾動(dòng)頻率,默認(rèn)15毫秒一次,數(shù)值太小會(huì)影響效率
//mv.setScrollLength(3);//默認(rèn)每次左移3px,數(shù)值太大會(huì)有停頓感,數(shù)值太小滾動(dòng)會(huì)變慢
mv.startScroll();
//記得在Ondestroy中停止?jié)L動(dòng),方便回收
@Override
protected void onDestroy() {
try {
mv.stopScroll();
mv = null;
} catch (Throwable e) {
e.printStackTrace();
}
super.onDestroy();
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 中TextView中跑馬燈效果的實(shí)現(xiàn)方法
- Android TextView實(shí)現(xiàn)跑馬燈效果的方法
- Android實(shí)現(xiàn)跑馬燈效果的方法
- Android自定義View實(shí)現(xiàn)豎直跑馬燈效果案例解析
- Android實(shí)現(xiàn)圖文垂直跑馬燈效果
- Android自定義textview實(shí)現(xiàn)豎直滾動(dòng)跑馬燈效果
- Android中使用TextView實(shí)現(xiàn)文字跑馬燈效果
- Android基于TextView不獲取焦點(diǎn)實(shí)現(xiàn)跑馬燈效果
- Android跑馬燈MarqueeView源碼解析
- Android自定義可控制速度的跑馬燈
相關(guān)文章
Android 將網(wǎng)絡(luò)的Url資源轉(zhuǎn)換為Drawable資源方式
這篇文章主要介紹了Android 將網(wǎng)絡(luò)的Url資源轉(zhuǎn)換為Drawable資源方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android編程使WebView支持HTML5 Video全屏播放的解決方法
這篇文章主要介紹了Android編程使WebView支持HTML5 Video全屏播放的解決方法,較為詳細(xì)的分析了全屏播放所涉及的相關(guān)技巧,并給出了完整代碼下載地址供讀者參考,需要的朋友可以參考下2015-10-10
Android后臺(tái)啟動(dòng)Activity的實(shí)現(xiàn)示例
這篇文章主要介紹了Android后臺(tái)啟動(dòng)Activity的實(shí)現(xiàn)示例,幫助大家更好的理解和學(xué)習(xí)使用Android開(kāi)發(fā),感興趣的朋友可以了解下2021-04-04
Android仿QQ好友列表實(shí)現(xiàn)列表收縮與展開(kāi)
這篇文章主要介紹了Android仿QQ好友列表實(shí)現(xiàn)列表收縮與展開(kāi),感興趣的小伙伴們可以參考一下2015-12-12
Android Studio實(shí)現(xiàn)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
Android 監(jiān)聽(tīng)手機(jī)GPS打開(kāi)狀態(tài)實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 監(jiān)聽(tīng)手機(jī)GPS打開(kāi)狀態(tài)實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android超詳細(xì)講解組件LinearLayout的使用
LinearLayout又稱作線性布局,是一種非常常用的布局。正如它的名字所描述的一樣,這個(gè)布局會(huì)將它所包含的控件在線性方向上依次排列。既然是線性排列,肯定就不僅只有一個(gè)方向,這里一般只有兩個(gè)方向:水平方向和垂直方向2022-03-03
DialogFragment運(yùn)行原理及使用方法詳解
這篇文章主要介紹了DialogFragment運(yùn)行原理及使用方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10

