生活随笔
收集整理的這篇文章主要介紹了
Android 滚动字幕实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
轉(zhuǎn)自:http://blog.csdn.net/cupidove/article/details/38316215
最近項目需求做個循環(huán)滾動字幕功能,自己找了相關(guān)資料,根據(jù)自己的風(fēng)格用兩種方法實現(xiàn)了該功能;
(備注:本人只實現(xiàn)了滾動效果,對于文字的格式排版沒做處理,格式可能會亂,文字排版還在研究中)
效果圖:
具體如下;
方法一:橫向滾動字幕繼承TextView
[java]?view plaincopy
package?com.example.playpic;???? ???? import?com.example.playpic.AutoScrollTextView.SavedState;???? ???? import?android.content.Context;???? import?android.graphics.Canvas;???? import?android.graphics.Color;???? import?android.graphics.Paint;???? import?android.graphics.Typeface;???? import?android.graphics.Paint.FontMetrics;???? import?android.os.Parcel;???? import?android.os.Parcelable;???? import?android.util.AttributeSet;???? import?android.util.DisplayMetrics;???? import?android.util.Log;???? import?android.view.WindowManager;???? import?android.view.View.BaseSavedState;???? import?android.widget.TextView;???? ???? public?class?AutoText?extends?TextView?{???? ???? ????private?int?width,height;???? ????private?Paint?paintText;???? ????private?float?posx,posy;???? ????private?float?speed=0.0f;???? ????private?String?text="hello?haha";???? ????private?float?textWidth=0;???? ????private?float?moveDistance=0.0f;???? ????private?boolean?isStarting=false;???? ???????? ????public?AutoText(Context?context)?{???? ????????super(context);???? ???????????? ????}???? ???????? ????public?AutoText(Context?context,?AttributeSet?attrs)?{???? ????????super(context,?attrs);???? ???????????? ????}???? ???????? ????private?void?initView(){???? ????????paintText=new?Paint();???? ????????paintText.setTextSize(50.0f);???? ????????paintText.setColor(Color.BLACK);???? ????????paintText.setTypeface(Typeface.DEFAULT_BOLD);???? ????????paintText.setAntiAlias(true);???? ????????text=getText().toString();???? ????????textWidth=paintText.measureText(text);Log.e("msg",?"textWidth=?"+textWidth);???? ????????this.speed=textWidth;???? ????????moveDistance=textWidth*2+width;???? ????}???? ???? ????public?void?initDisplayMetrics(WindowManager?windowManager){???? ??????????????? ?????????DisplayMetrics?dm=new?DisplayMetrics();???? ?????????windowManager.getDefaultDisplay().getMetrics(dm);???? ?????????this.width=dm.widthPixels;???? ?????????this.height=dm.heightPixels;???? ????????????? ?????????initView();???? ?????????this.posx=width+textWidth;???? ?????????FontMetrics?fm?=?paintText.getFontMetrics();?????? ?????????float?baseline?=?fm.descent?-?fm.ascent;???? ?????????this.posy=height/2-baseline;????? ????}???? ???????? ????public?void?startScroll()?{???? ????????isStarting?=?true;???? ????????invalidate();???? ????}???? ????public?void?stopScroll()?{???? ????????isStarting?=?false;???? ????????invalidate();???? ????}???? ???????? ????@Override???? ????protected?void?onDraw(Canvas?canvas)?{???? ?????? ????????canvas.drawText(text,?posx-speed,?posy,?paintText);???? ????????if?(!isStarting)?{???? ????????????return;???? ????????}???? ????????speed?+=?2.0f;???? ????????if?(speed?>?moveDistance)???? ????????????speed?=?textWidth;???? ????????invalidate();???? ????}???? ???????? }????
布局文件;
[java]?view plaincopy
<?xml?version="1.0"?encoding="utf-8"?>???? <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"???? ????android:layout_width="match_parent"???? ????android:layout_height="match_parent"???? ????android:orientation="vertical"?>???? ???????? ???? ????<com.example.playpic.AutoText???? ????????android:id="@+id/autoTxt"???? ????????android:layout_width="match_parent"???? ????????android:layout_height="wrap_content"???? ????????android:background="#ffffff"???? ????????android:textColor="#00ff00"???? ????????android:textSize="35sp"?/>???? ???? </LinearLayout>????
方法二:繼承sufaceView實現(xiàn)縱向文字滾動功能
[java]?view plaincopy
package?com.example.playpic;???? ???? import?java.util.ArrayList;???? import?java.util.List;???? ???? import?android.content.Context;???? import?android.graphics.Canvas;???? import?android.graphics.Color;???? import?android.graphics.Paint;???? import?android.graphics.Paint.FontMetrics;???? import?android.graphics.Rect;???? import?android.graphics.Typeface;???? import?android.util.AttributeSet;???? import?android.util.DisplayMetrics;???? import?android.view.SurfaceHolder;???? import?android.view.SurfaceView;???? import?android.view.WindowManager;???? ???? public?class?ScrollText?extends?SurfaceView?implements?SurfaceHolder.Callback?,Runnable{???? ???? ????private?int?width,height;???? ????private?SurfaceHolder?sfh;???? ????private?Thread?th;???? ????private?boolean?flag;???? ????private?Paint?backPaint,textPaint;???? ???????? ????private?float?posx,posy;???? ???????? ????private?Float[]?step,stepBack;???? ????private?String?txtContent;???? ???????? ????private?Float[]?tposy;???? ???????? ????private?String[]?texts;???? ???????? ????public?ScrollText(Context?context)?{???? ????????super(context);???? ????????initView();???? ????}???? ???????? ????public?ScrollText(Context?context,?AttributeSet?attrs)?{???? ????????super(context,?attrs);???? ????????initView();???? ????}???? ???? ????private?void?initView(){???? ????????sfh=this.getHolder();???? ????????sfh.addCallback(this);?????? ????????this.setKeepScreenOn(true);???? ????????this.setFocusable(true);???? ?????? ????????backPaint=new?Paint();???? ????????backPaint.setColor(Color.BLACK);???? ????????textPaint=new?Paint();???? ???????????? ????????textPaint.setTextSize(30.0f);???? ????????textPaint.setColor(Color.BLUE);???? ????????textPaint.setTypeface(Typeface.DEFAULT_BOLD);???? ????????textPaint.setTextAlign(Paint.Align.LEFT);???? ????????textPaint.setAntiAlias(true);???? ????}???? ???????? ????public?void?initDisplayMetrics(WindowManager?windowManager){???? ??????????????? ????????DisplayMetrics?dm=new?DisplayMetrics();???? ????????windowManager.getDefaultDisplay().getMetrics(dm);???? ????????this.width=dm.widthPixels;???? ????????this.height=dm.heightPixels;???? ????????this.posx=width/6;???? ??????? ???? ????????this.posy=height-100;???? ???????????? ????}???? ???????? ????public?void?setTxtContent(String?txt){???? ????????this.txtContent=txt;???? ????}???? ???????? ????@Override???? ????public?void?surfaceCreated(SurfaceHolder?holder)?{???? ????????this.flag=true;???? ???????????? ????????if(th==null||!th.isAlive()){???? ????????????th=new?Thread(this);???? ????????????th.start();???? ????????}???? ????}???? ???????? ????@Override???? ????public?void?surfaceChanged(SurfaceHolder?holder,?int?format,?int?width,???? ????????????int?height)?{???? ???????????? ????}???? ???? ????@Override???? ????public?void?surfaceDestroyed(SurfaceHolder?holder)?{???? ???????????? ????????this.flag=false;???? ????}???? ????? ? ???? ????private?void?drawAll(){???? ????????Canvas?canvas=null;???? ????????try{???? ????????????canvas=sfh.lockCanvas();???? ????????????drawText(canvas,?txtContent);???? ???????????????? ????????}catch(Exception?e){???? ????????????e.printStackTrace();???? ????????}finally{???? ????????????if(canvas!=null){???? ????????????????sfh.unlockCanvasAndPost(canvas);???? ????????????}???? ????????}???? ????}???? ???????? ????private?void?drawText(Canvas?canvas,String?text){???? ?????????? ????????canvas.drawRect(new?Rect(0,0,getWidth(),getHeight()),?backPaint);???? ???????????? ????????initDrawText(text);???? ???????????????? ????????int?len=tposy.length;???? ?????????? ????????for(int?n=0;n<len;n++){???? ????????????if(texts[n]==null)???? ????????????????return;???? ????????????float?ty=tposy[n]-step[n];???? ???????????????? ????????????canvas.drawText(texts[n],?posx,?ty,?textPaint);???? ????????????step[n]+=5.0f;???? ???????????????? ????????????if(n==len-1&&ty<0){???? ????????????????step=stepBack.clone();???? ????????????}???? ????????????? ? ???? ????????}???? ????????postInvalidate();???? ????}???? ????? ? ? ???? ????private?void?initDrawText(String?text){???? ????????if(texts==null){???? ????????????texts=getTexts(text);???? ????????}???? ????????if(tposy==null){???? ????????????tposy=getTextLinePosy();???? ????????}???? ????????if(stepBack==null){???? ????????????stepBack=new?Float[tposy.length];???? ????????????int?i=0;???? ????????????float?interval=0.0f;???? ????????????FontMetrics?fm?=?textPaint.getFontMetrics();?????? ????????????float?baseline?=?fm.descent?-?fm.ascent;???? ????????????while(i<stepBack.length){???? ????????????????stepBack[i]=interval;???? ????????????????interval-=baseline;???? ????????????????i++;???? ????????????}???? ????????}???? ????????if(step==null){???? ????????????step=stepBack.clone();???? ????????}???? ????}???? ????? ? ? ? ???? ????private?String[]?getTexts(String?text){???? ????????List<String>?totalList=new?ArrayList<String>(10);???? ????????String[]?str=text.split("\n");???? ????????int?len=str.length;???? ???????????? ????????for(int?i=0;i<len;i++){???? ????????????String[]?ss=autoSplit(str[i],?textPaint,?getWidth()/3*2);???? ????????????for(String?s:ss){???? ????????????????totalList.add(s);???? ????????????}???? ????????}???? ????????if(texts==null)???? ????????????texts=(String[])?totalList.toArray(new?String[0]);???? ????????? ???? ????????return?texts;???? ????}???? ????? ? ? ???? ????private?Float[]?getTextLinePosy(){???? ?????????FontMetrics?fm?=?textPaint.getFontMetrics();?????? ?????????float?baseline?=?fm.descent?-?fm.ascent;??????? ?????????float?y?=??posy+baseline;???? ?????????????????? ?????????int?len=texts.length;???? ?????????Float[]?groups=new?Float[len];???? ??????????????????? ????????????for(int?i=0;i<len;i++)?{??????? ????????????????groups[i]=y;???? ????????????????y?=y+?baseline?+?fm.leading;??? ????????????}?????? ????????????return?groups;???? ????}???? ????? ? ? ? ? ? ???? ????private?String[]?autoSplit(String?content,?Paint?p,?float?width)?{???? ???????? ??????????????? ???????? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ???? ???????????? ??????????? ????????float?textWidth?=?p.measureText(content);???? ????????if(textWidth?<=?width)?{???? ????????????return?new?String[]{content};???? ????????}???? ???????????? ????????int?length?=?content.length();???? ????????int?start?=?0,?end?=?1,?i?=?0;???? ????????int?lines?=?(int)?Math.ceil(textWidth?/?width);??? ????????String[]?lineTexts?=?new?String[lines];???? ???????????? ????????while(start?<?length)?{???? ????????????if(p.measureText(content,?start,?end)?>?width)?{??? ????????????????????lineTexts[i++]?=?content.substring(start,?end);?? ????????????????????start?=?end;???? ????????????????}???? ????????????if(end?==?length)?{??? ????????????????lineTexts[i]?=?content.substring(start,?end);?? ????????????????break;???? ????????????}???? ????????????end?+=?1;???? ????????}???? ???????????????? ????????return?lineTexts;???? ????}???? ???????? ????@Override???? ????public?void?run()?{???? ????????while(flag){???? ????????????drawAll();???? ????????????try?{???? ????????????????Thread.sleep(200);???? ????????????}?catch?(InterruptedException?e)?{???? ????????????????e.printStackTrace();???? ????????????}???? ????????}???? ????}???? ???????? ???? }????
[java]?view plaincopy
package?com.example.playpic;???? ???? import?java.io.BufferedReader;???? import?java.io.File;???? import?java.io.FileInputStream;???? import?java.io.FileNotFoundException;???? import?java.io.FileOutputStream;???? import?java.io.FileReader;???? import?java.io.IOException;???? import?java.io.InputStreamReader;???? ???? import?android.app.Activity;???? import?android.os.Bundle;???? import?android.os.Environment;???? import?android.text.method.ScrollingMovementMethod;???? import?android.util.Log;???? import?android.widget.TextView;???? ???? import?com.sample.fun.R;???? ???? public?class?ScrollTextActivity?extends?Activity{???? ???? ???? ????String?str="";???? ???? ????String?str11="促進青年教師全面發(fā)展,\n引導(dǎo)廣大高校青年教師為實現(xiàn)中華民族偉大復(fù)興的中國夢貢獻力"?+"\n"+"促進青年教師全面發(fā)展,\n引導(dǎo)廣大高校青年教師為實現(xiàn)中華民族偉大復(fù)興的中國夢貢獻力"+"\n"+???? ????????????"??djsdnh???kshdfjks?????\n\r\t?";???? ???????? ????@Override???? ????protected?void?onCreate(Bundle?savedInstanceState)?{???? ????????super.onCreate(savedInstanceState);???? ???????? ????????scroll3();???? ????}???? ???????? ????void?scroll2(){???? ????????ScrollText?v=new?ScrollText(getApplicationContext());???? ????????setContentView(v);???? ????????v.setTxtContent(str11);???? ????????v.initDisplayMetrics(getWindowManager());???? ????}???? ???????? ????void?scroll3(){???? ????????setContentView(R.layout.scrollview1);???? ????????AutoText?auto=(AutoText)findViewById(R.id.autoTxt);???? ????????auto.setText(str11);???? ????????auto.initDisplayMetrics(getWindowManager());???? ????????auto.startScroll();???? ????}???? ???????? }?
總結(jié)
以上是生活随笔為你收集整理的Android 滚动字幕实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。