Android自定義View實(shí)現(xiàn)豎直跑馬燈效果案例解析
本文實(shí)例為大家分享了Android實(shí)現(xiàn)豎直跑馬燈效果的具體代碼,供大家參考,具體內(nèi)容如下
首先給出跑馬燈效果圖
中間的色塊是因?yàn)橐曨l轉(zhuǎn)成GIF造成的失真,自動(dòng)忽略哈。
大家知道,橫向的跑馬燈android自帶的TextView就可以實(shí)現(xiàn),詳情請(qǐng)百度【Android跑馬燈效果】。但是豎直的跑馬燈效果原生Android是不支持的。網(wǎng)上也有很多網(wǎng)友實(shí)現(xiàn)了自定義的效果,但是我一貫是不喜歡看別人的代碼,所以這篇博客的思路完全是我自己的想法哈。
首先,我們需要給自定義的控件梳理一下格局,如下圖所示:
1、首先我們將控件分為三個(gè)區(qū)塊,上面綠色部分為消失不可見的塊,中間黑色部分為可見區(qū)域,下面紅色部分為欲出現(xiàn)不可見區(qū)域。藍(lán)色的線代表的是整個(gè)控件的上線和下線。
2、首先我們只給出兩個(gè)文字塊在內(nèi)存中,分別是黑色部分的可見塊和紅色部分的欲出現(xiàn)塊。
3、求出這些塊的寬度、高度與中心點(diǎn)的坐標(biāo)值。
4、滾動(dòng)時(shí),動(dòng)態(tài)地改變每個(gè)塊的中心點(diǎn)y坐標(biāo),使之向上平移。
5、當(dāng)平移結(jié)束后,可見塊位于欲消失的不可見塊,欲出現(xiàn)的可見塊位于可見區(qū)域的文字塊。此時(shí)將欲消失的文字塊移除List,并重新設(shè)置后一個(gè)索引的Text和重心坐標(biāo)值,重新加入List中,刷新。
6、用一個(gè)Handler來處理動(dòng)畫的間隔時(shí)間。用屬性動(dòng)畫ValueAnimator來實(shí)現(xiàn)平移的動(dòng)畫效果。
下面開始代碼講解,首先是用鏈?zhǔn)皆O(shè)置法設(shè)置一些常規(guī)屬性:
<span style="font-size:14px;"> public VerticalMarqueeView color(int color){ this.color = color; return this; } public VerticalMarqueeView textSize(int textSize){ this.textSize = textSize; return this; } public VerticalMarqueeView datas(String[] datas){ this.datas = datas; return this; } public void commit(){ if(this.datas == null || datas.length == 0){ Log.e("VerticalMarqueeView", "the datas's length is illegal"); throw new IllegalStateException("may be not invoke the method named datas(String[])"); } paint.setColor(color); paint.setTextSize(textSize); }</span>
最后一定要調(diào)用commit方法進(jìn)行提交,通過代碼可以看出來這里除了有排空措施,還有最重要的一步:設(shè)置字體的大小。
然后我抽象出一個(gè)文字塊的bean類:
public class TextBlock { private int width; private int height; private String text; private int drawX; private int drawY; private Paint paint; private int position; public TextBlock(int width, int height, Paint paint){ this.width = width; this.height = height; this.paint = paint; } public void reset(int centerY){ reset(text, centerX, centerY, position); } public void reset(String text, int centerY){ reset(text, centerX, centerY, position); } public void reset(String text, int centerY, int position){ reset(text, centerX, centerY, position); } public void reset(String text, int centerX, int centerY, int position){ this.text = text; this.position = position; int measureWidth = (int)paint.measureText(text); drawX = (width - measureWidth) / 2; FontMetrics metrics = paint.getFontMetrics(); drawY = (int)(centerY + (metrics.bottom - metrics.top) / 2 - metrics.bottom); } public int getPosition(){ return position; } public void draw(Canvas canvas){ canvas.drawText(text, drawX, drawY, paint); } }
這個(gè)bean類,最重要的方法就是幾個(gè)重載的reset方法,通過改變centerY的值,來動(dòng)態(tài)得改變繪制文字的起點(diǎn)實(shí)現(xiàn)居中繪制。關(guān)于文字的居中繪制請(qǐng)參考百度【android canvas 居中繪制文本】。
然后是重寫onMeasure方法
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ super.onMeasure(widthMeasureSpec, heightMeasureSpec); if(this.datas == null || this.datas.length == 0){ Log.e("VerticalMarqueeView", "the datas's length is illegal"); return; } width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight(); height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom(); centerX = width / 2; centerY = height / 2; blocks.clear(); //添加顯示區(qū)域的文字塊 TextBlock block1 = new TextBlock(width, height, paint); block1.reset(datas[0], centerX, centerY, 0); blocks.add(block1); if(datas.length > 1){ TextBlock block2 = new TextBlock(width, height, paint); block2.reset(datas[1], centerX, centerY + height, 1); blocks.add(block2); } }
在這個(gè)方法中,首先進(jìn)行非空判斷以免出現(xiàn)致命邏輯錯(cuò)誤。然后得到整個(gè)控件的寬高和重心坐標(biāo)。然后實(shí)例化兩個(gè)文字塊TextBlock,第一個(gè)文字塊通過reset設(shè)置中點(diǎn)y坐標(biāo)為整個(gè)控件的中點(diǎn)y坐標(biāo),第二個(gè)文字塊通過reset設(shè)置中點(diǎn)y坐標(biāo)為centerY+height,意思就是置于下一個(gè)文字塊的不可見區(qū)域內(nèi)。
然后是onDraw方法,這個(gè)方法非常簡單,已經(jīng)將業(yè)務(wù)邏輯轉(zhuǎn)交給TextBlock的draw方法了。
@Override protected void onDraw(Canvas canvas){ for(int i = 0; i < blocks.size(); i++){ blocks.get(i).draw(canvas); } }
最關(guān)鍵的就是滾動(dòng)效果的實(shí)現(xiàn)了,首先我們給出兩個(gè)方法,開始滾動(dòng)和結(jié)束滾動(dòng)。
public void startScroll(){ isStopScroll = false; if(datas == null || datas.length == 0 || datas.length == 1){ Log.e("VerticalMarqueeView", "the datas's length is illegal"); return; } if(!isStopScroll){ handler.postDelayed(new Runnable(){ @Override public void run(){ scroll(); if(!isStopScroll){ handler.postDelayed(this, DURATION_SCROLL); } } }, DURATION_SCROLL); } } public void stopScroll(){ this.isStopScroll = true; }
原理很簡單,首先給出一個(gè)boolean標(biāo)志isStopScroll。然后用Handler的postDelayed方法進(jìn)行循環(huán)延遲得調(diào)用scroll方法進(jìn)行滾動(dòng)。接下來給出全文最重要的方法,scroll方法。
private void scroll(){ ValueAnimator animator = ValueAnimator.ofPropertyValuesHolder(PropertyValuesHolder.ofInt("scrollY", centerY, centerY - height)); animator.setDuration(DURATION_ANIMATOR); animator.addUpdateListener(new AnimatorUpdateListener(){ @Override public void onAnimationUpdate(ValueAnimator animation){ int scrollY = (int)animation.getAnimatedValue("scrollY"); blocks.get(0).reset(scrollY); blocks.get(1).reset(scrollY + height); invalidate(); } }); animator.addListener(new AnimatorListener(){ @Override public void onAnimationStart(Animator animation){ } @Override public void onAnimationRepeat(Animator animation){ } @Override public void onAnimationEnd(Animator animation){ //移除第一塊 int position = blocks.get(1).getPosition(); TextBlock textBlock = blocks.remove(0); //最后一個(gè) if(position == datas.length - 1){ position = 0; }else{ position ++; } textBlock.reset(datas[position], centerY + height, position); blocks.add(textBlock); invalidate(); } @Override public void onAnimationCancel(Animator animation){ } }); animator.start(); }
首先采用ValueAnimator類進(jìn)行屬性動(dòng)畫,開始值為控件的中點(diǎn)y坐標(biāo),結(jié)束值為centerY-height,意味著要從下往上移動(dòng)一個(gè)文字塊的距離。然后在動(dòng)畫更新回調(diào)方法中,進(jìn)行對(duì)文字塊的reset方法。當(dāng)動(dòng)畫結(jié)束后,得到第二個(gè)文字塊的position值,然后移除第一個(gè)文字塊,重新reset這個(gè)文字塊的索引值,再加入到List的容器中。如此循壞。
最后給一個(gè)get方法返回position吧。
public int getCurrentPosition(){ if(datas == null || datas.length == 0){ return -1; } if(datas.length == 1 && blocks.size() == 1){ return 0; } return blocks.get(0).getPosition(); }
上述就將這個(gè)自定義控件的所有代碼都呈現(xiàn)出來了,考慮到比較零散,這里講所有代碼都打印出來:
/** * @FileName: VerticalMarqueeView.java * @Author * @Description: * @Date 2016年7月13日 上午9:32:27 * @CopyRight CNP Corporation */ package cc.wxf.component; import android.animation.Animator; import android.animation.Animator.AnimatorListener; import android.animation.PropertyValuesHolder; import android.animation.ValueAnimator; import android.animation.ValueAnimator.AnimatorUpdateListener; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.FontMetrics; import android.os.Handler; import android.util.AttributeSet; import android.util.Log; import android.view.View; import java.util.ArrayList; import java.util.List; public class VerticalMarqueeView extends View{ public static final int DURATION_SCROLL = 3000; public static final int DURATION_ANIMATOR = 1000; private int color = Color.BLACK; private int textSize = 30; private String[] datas = null; private int width; private int height; private int centerX; private int centerY; private List<TextBlock> blocks = new ArrayList<TextBlock>(2); private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); private Handler handler = new Handler(); private boolean isStopScroll = false; public VerticalMarqueeView(Context context, AttributeSet attrs, int defStyleAttr){ super(context, attrs, defStyleAttr); } public VerticalMarqueeView(Context context, AttributeSet attrs){ super(context, attrs); } public VerticalMarqueeView(Context context){ super(context); } public VerticalMarqueeView color(int color){ this.color = color; return this; } public VerticalMarqueeView textSize(int textSize){ this.textSize = textSize; return this; } public VerticalMarqueeView datas(String[] datas){ this.datas = datas; return this; } public void commit(){ if(this.datas == null || datas.length == 0){ Log.e("VerticalMarqueeView", "the datas's length is illegal"); throw new IllegalStateException("may be not invoke the method named datas(String[])"); } paint.setColor(color); paint.setTextSize(textSize); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ super.onMeasure(widthMeasureSpec, heightMeasureSpec); if(this.datas == null || this.datas.length == 0){ Log.e("VerticalMarqueeView", "the datas's length is illegal"); return; } width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight(); height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom(); centerX = width / 2; centerY = height / 2; blocks.clear(); //添加顯示區(qū)域的文字塊 TextBlock block1 = new TextBlock(width, height, paint); block1.reset(datas[0], centerX, centerY, 0); blocks.add(block1); if(datas.length > 1){ TextBlock block2 = new TextBlock(width, height, paint); block2.reset(datas[1], centerX, centerY + height, 1); blocks.add(block2); } } @Override protected void onDraw(Canvas canvas){ for(int i = 0; i < blocks.size(); i++){ blocks.get(i).draw(canvas); } } public void startScroll(){ isStopScroll = false; if(datas == null || datas.length == 0 || datas.length == 1){ Log.e("VerticalMarqueeView", "the datas's length is illegal"); return; } if(!isStopScroll){ handler.postDelayed(new Runnable(){ @Override public void run(){ scroll(); if(!isStopScroll){ handler.postDelayed(this, DURATION_SCROLL); } } }, DURATION_SCROLL); } } public void stopScroll(){ this.isStopScroll = true; } private void scroll(){ ValueAnimator animator = ValueAnimator.ofPropertyValuesHolder(PropertyValuesHolder.ofInt("scrollY", centerY, centerY - height)); animator.setDuration(DURATION_ANIMATOR); animator.addUpdateListener(new AnimatorUpdateListener(){ @Override public void onAnimationUpdate(ValueAnimator animation){ int scrollY = (int)animation.getAnimatedValue("scrollY"); blocks.get(0).reset(scrollY); blocks.get(1).reset(scrollY + height); invalidate(); } }); animator.addListener(new AnimatorListener(){ @Override public void onAnimationStart(Animator animation){ } @Override public void onAnimationRepeat(Animator animation){ } @Override public void onAnimationEnd(Animator animation){ //移除第一塊 int position = blocks.get(1).getPosition(); TextBlock textBlock = blocks.remove(0); //最后一個(gè) if(position == datas.length - 1){ position = 0; }else{ position ++; } textBlock.reset(datas[position], centerY + height, position); blocks.add(textBlock); invalidate(); } @Override public void onAnimationCancel(Animator animation){ } }); animator.start(); } public int getCurrentPosition(){ if(datas == null || datas.length == 0){ return -1; } if(datas.length == 1 && blocks.size() == 1){ return 0; } return blocks.get(0).getPosition(); } public class TextBlock { private int width; private int height; private String text; private int drawX; private int drawY; private Paint paint; private int position; public TextBlock(int width, int height, Paint paint){ this.width = width; this.height = height; this.paint = paint; } public void reset(int centerY){ reset(text, centerX, centerY, position); } public void reset(String text, int centerY){ reset(text, centerX, centerY, position); } public void reset(String text, int centerY, int position){ reset(text, centerX, centerY, position); } public void reset(String text, int centerX, int centerY, int position){ this.text = text; this.position = position; int measureWidth = (int)paint.measureText(text); drawX = (width - measureWidth) / 2; FontMetrics metrics = paint.getFontMetrics(); drawY = (int)(centerY + (metrics.bottom - metrics.top) / 2 - metrics.bottom); } public int getPosition(){ return position; } public void draw(Canvas canvas){ canvas.drawText(text, drawX, drawY, paint); } } }
最后給出使用的方法,很簡單。
package cc.wxf.androiddemo; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.widget.Toast; import cc.wxf.component.VerticalMarqueeView; public class MainActivity extends Activity { private VerticalMarqueeView vmView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vmView = (VerticalMarqueeView)findViewById(R.id.vmView); String[] datas = new String[]{ "南海又開始動(dòng)蕩了","菲律賓到處都在肇事","這次為了一張審判廢紙,菲律賓投入了多少成本呢","測(cè)試數(shù)據(jù)4","測(cè)試數(shù)據(jù)5為了長度不一樣","就把這條當(dāng)做測(cè)試數(shù)據(jù)吧" }; vmView.color(getResources().getColor(android.R.color.black)) .textSize(sp2px(this, 15)) .datas(datas).commit(); vmView.startScroll(); vmView.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ Toast.makeText(MainActivity.this, "當(dāng)前的索引為:" + vmView.getCurrentPosition(), Toast.LENGTH_SHORT).show(); } }); } private int sp2px(Context context, int sp){ float density = context.getResources().getDisplayMetrics().scaledDensity; return (int) (sp * density + 0.5f); } @Override protected void onDestroy() { super.onDestroy(); //必須要調(diào)用,否則內(nèi)存中會(huì)一直無限循環(huán) vmView.stopScroll(); } }
demo就不提供了,自定義View就只有上面一個(gè)文件。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 中TextView中跑馬燈效果的實(shí)現(xiàn)方法
- Android TextView實(shí)現(xiàn)跑馬燈效果的方法
- Android實(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自定義View實(shí)現(xiàn)跑馬燈效果
- Android自定義可控制速度的跑馬燈
相關(guān)文章
Android實(shí)現(xiàn)在屏幕上移動(dòng)圖片的方法
這篇文章主要介紹了Android實(shí)現(xiàn)在屏幕上移動(dòng)圖片的方法,實(shí)例分析了Android操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-06-06Android開發(fā)之微信底部菜單欄實(shí)現(xiàn)的幾種方法匯總
這篇文章主要介紹了Android開發(fā)之微信底部菜單欄實(shí)現(xiàn)的幾種方法,下面小編把每種方法通過實(shí)例逐一給大家介紹,需要的朋友可以參考下2016-09-09Android中TextView顯示插入的圖片實(shí)現(xiàn)方法
這篇文章主要介紹了Android中TextView顯示插入的圖片實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了TextView三種顯示插入圖片的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08Android編程自定義搜索框?qū)崿F(xiàn)方法【附demo源碼下載】
這篇文章主要介紹了Android編程自定義搜索框?qū)崿F(xiàn)方法,涉及Android界面布局、數(shù)據(jù)加載、事件響應(yīng)等相關(guān)操作技巧,并附帶完整demo源碼供讀者下載參考,需要的朋友可以參考下2017-12-12Android編程實(shí)現(xiàn)圖片的上傳和下載功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)圖片的上傳和下載功能,涉及Android針對(duì)圖片的字節(jié)流轉(zhuǎn)換與傳輸操作相關(guān)技巧,需要的朋友可以參考下2016-10-10Android實(shí)現(xiàn)沉浸式導(dǎo)航欄實(shí)例代碼
通過本文給大家分享android實(shí)現(xiàn)沉浸式導(dǎo)航欄實(shí)例代碼,代碼非常實(shí)用,需要的朋友可以參考下2016-05-05Android繼承ViewGroup實(shí)現(xiàn)Scroll滑動(dòng)效果的方法示例
這篇文章主要介紹了Android繼承ViewGroup實(shí)現(xiàn)Scroll滑動(dòng)效果的方法,結(jié)合實(shí)例形式分析了Android滑動(dòng)效果的原理及擴(kuò)展ViewGroup實(shí)現(xiàn)滑動(dòng)功能的相關(guān)操作技巧,需要的朋友可以參考下2017-08-08