這篇文章主要介紹Android怎么實現(xiàn)歌詞滾動效果,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)專注于謝通門網站建設服務及定制,我們擁有豐富的企業(yè)做網站經驗。 熱誠為您提供謝通門營銷型網站建設,謝通門網站制作、謝通門網頁設計、謝通門網站官網定制、重慶小程序開發(fā)服務,打造謝通門網絡公司原創(chuàng)品牌,更為您提供謝通門網站排名全網營銷落地服務。
本文實例為大家分享了Android實現(xiàn)歌詞滾動效果的具體代碼,供大家參考,具體內容如下
自定義TextView
public class VerticalScrollTextView extends TextView { private Paint mPaint; private float mX; private Paint mPathPaint; public int index = 0; private List<Sentence> list; public float mTouchHistoryY; private int mY; private float middleY;// private static final int DY = 40; // public VerticalScrollTextView(Context context) { super(context); init(); } public VerticalScrollTextView(Context context, AttributeSet attr) { super(context, attr); init(); } public VerticalScrollTextView(Context context, AttributeSet attr, int i) { super(context, attr, i); init(); } private void init() { setFocusable(true); if(list==null){ list=new ArrayList<Sentence>(); Sentence sen=new Sentence(0," "); list.add(0, sen); } // mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setTextSize(24); mPaint.setColor(Color.BLACK); mPaint.setAlpha(80); mPaint.setTypeface(Typeface.SERIF); // mPathPaint = new Paint(); mPathPaint.setAntiAlias(true); mPathPaint.setColor(Color.RED); mPathPaint.setTextSize(24); mPathPaint.setTypeface(Typeface.SANS_SERIF); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(0xEFeffff); Paint p = mPaint; Paint p2 = mPathPaint; p.setTextAlign(Paint.Align.LEFT); if (index == -1) return; p2.setTextAlign(Paint.Align.LEFT); // canvas.drawText(list.get(index).getName(), mX, middleY, p2); float tempY = middleY; // for (int i = index - 1; i >= 0; i--) { tempY = tempY - DY; if (tempY < 0) { break; } canvas.drawText(list.get(i).getName(), mX, tempY, p); } tempY = middleY; // for (int i = index + 1; i < list.size(); i++) { // tempY = tempY + DY; if (tempY > mY) { break; } canvas.drawText(list.get(i).getName(), mX, tempY, p); } } protected void onSizeChanged(int w, int h, int ow, int oh) { super.onSizeChanged(w, h, ow, oh); mX = w * 0.3f; mY = h; middleY = h * 0.5f; } public long updateIndex(int index) { if (index == -1) return -1; this.index=index; return index; } public List<Sentence> getList() { return list; } public void setList(List<Sentence> list) { this.list = list; } public void updateUI(){ new Thread(new updateThread()).start(); } class updateThread implements Runnable { long time = 300; int i=0; public void run() { while (true) { long sleeptime = updateIndex(i); time += sleeptime; mHandler.post(mUpdateResults); if (sleeptime == -1) return; try { Thread.sleep(time); i++; if(i==getList().size()) { i=0; time = 300; } } catch (InterruptedException e) { e.printStackTrace(); } } } } Handler mHandler = new Handler(); Runnable mUpdateResults = new Runnable() { public void run() { invalidate(); // } }; }
數(shù)據封裝類
public class Sentence { private String name; private int index; public Sentence(int index,String name){ this.name=name; this.index=index; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } }
布局
<com.mypackager.ui.VerticalScrollTextView android:id="@+id/scoll_textView" android:layout_width="500dp" android:layout_height="500dp" android:text="@string/company_intrduce_text" android:visibility="gone" ></com.mypackager.VerticalScrollTextView>
Activity代碼
List lst=new ArrayList<Sentence>(); for(int i=0;i<8;i++){ if(i%2==0){ Sentence sen=new Sentence(i,i+1+"NanJINGXIXI"); lst.add(i, sen); }else{ Sentence sen=new Sentence(i,i+1+"Hello world!"); lst.add(i, sen); } } play_textView.setList(lst); play_textView.updateUI();
以上是“Android怎么實現(xiàn)歌詞滾動效果”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章名稱:Android怎么實現(xiàn)歌詞滾動效果
分享URL:http://jinyejixie.com/article36/jjippg.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供微信小程序、標簽優(yōu)化、微信公眾號、品牌網站設計、、網站導航
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)