網(wǎng)上android播放器雖然挺多,感覺提供的歌詞顯示功能比較死板,要么搜索給的條件死死的,要么放置sdcard內(nèi)部的歌詞格式需要統(tǒng)一,應(yīng)該提供類似文件夾瀏覽的功能。^_^,不過在這之前先搞定歌詞的現(xiàn)實(shí)界面:
成都創(chuàng)新互聯(lián)成立以來不斷整合自身及行業(yè)資源、不斷突破觀念以使企業(yè)策略得到完善和成熟,建立了一套“以技術(shù)為基點(diǎn),以客戶需求中心、市場為導(dǎo)向”的快速反應(yīng)體系。對公司的主營項(xiàng)目,如中高端企業(yè)網(wǎng)站企劃 / 設(shè)計(jì)、行業(yè) / 企業(yè)門戶設(shè)計(jì)推廣、行業(yè)門戶平臺運(yùn)營、app軟件開發(fā)、成都手機(jī)網(wǎng)站制作、微信網(wǎng)站制作、軟件開發(fā)、成都二樞機(jī)房等實(shí)行標(biāo)準(zhǔn)化操作,讓客戶可以直觀的預(yù)知到從成都創(chuàng)新互聯(lián)可以獲得的服務(wù)效果。
播放器的歌詞界面實(shí)現(xiàn)以下幾個功能
不多說了直接貼代碼,首先開啟一個線程每隔一段時(shí)間往view中送入一串字符
Java代碼
import android.os.Bundle; import android.os.Handler; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class TextAlign extends GraphicsActivity implements OnClickListener { private SampleView mView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Animation in = AnimationUtils.loadAnimation(this, R.anim.push_up_in); // mView.setAnimation(in); setContentView(R.layout.main); mView =(SampleView) findViewById(R.id.text01); Button bt = (Button) findViewById(R.id.Button01); bt.setOnClickListener(this); new Thread(new UIUpdateThread()).start(); } class UIUpdateThread implements Runnable { long time = 40000; long sleeptime = 100; public void run() { try { while (time < 200000) { Thread.sleep(sleeptime); mView.updateIndex(time);//.index = mLyric.getNowSentenceIndex(time); // Log.v("UIThread", mView.index + ":" + time); time += sleeptime; mHandler.post(mUpdateResults); } } catch (InterruptedException e) { e.printStackTrace(); } } } Handler mHandler = new Handler(); Runnable mUpdateResults = new Runnable() { public void run() { mView.invalidate(); // 更新視圖 } }; @Override public void onClick(View v) { mView.mTouchHistoryY -=30; mHandler.post(mUpdateResults); } }
這里將時(shí)間送到SampleView中,該類對此時(shí)間進(jìn)行加工得到一系列l(wèi)ist(該list是動態(tài)生成的),從而根據(jù)時(shí)間的推移遞增的得到一系列的字串。這個過程模擬了歌詞的顯示過程
接下來的SampleView繼承了TextView并重載了onDraw方法.注意,這里只給了個sample,里面歌詞怎么生成的見YOYOPlayer。這里就不給代碼了
Java代碼
import java.io.File; import java.util.List; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Typeface; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.widget.TextView; import android.widget.Toast; import com.android.lyric.Lyric; import com.android.lyric.PlayListItem; import com.android.lyric.Sentence; public class SampleView extends TextView { private Paint mPaint; private float mX; private static Lyric mLyric; private Path mPath; private Paint mPathPaint; public String test = "test"; public int index = 0; private List<Sentence> list; private float mTouchStartY; private float mTouchCurrY; public float mTouchHistoryY; private int mY; private long currentTime; private long currentDunringTime; private long sentenctTime; private float middleY; private String middleContent="Empty"; private static final int DY = 30; private static void makePath(Path p) { p.moveTo(10, 0); p.cubicTo(100, -50, 200, 50, 300, 0); } public SampleView(Context context) { super(context); init(); } public SampleView(Context context,AttributeSet attr) { super(context,attr); init(); } public SampleView(Context context,AttributeSet attr,int i) { super(context,attr,i); init(); } private void init() { setFocusable(true); PlayListItem pli = new PlayListItem("", "", 1000L, true); mLyric = new Lyric(new File("/sdcard/M0005044007.lrc"), pli); list = mLyric.list; mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setTextSize(20); mPaint.setTypeface(Typeface.SERIF); mPath = new Path(); makePath(mPath); mPathPaint = new Paint(); mPathPaint.setAntiAlias(true); mPathPaint.setColor(0x800000FF); mPathPaint.setStyle(Paint.Style.STROKE); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.WHITE); Paint p = mPaint; float x = mX; float plus =currentDunringTime==0?index*30: index*30 +(((float)currentTime - (float)sentenctTime)/(float)currentDunringTime)*(float)30; float y = mY- plus+mTouchCurrY - mTouchStartY+mTouchHistoryY; canvas.translate(0,y); for (int i = 0; i < index; i++) { String text = list.get(i).getContent(); if((y+i*30)<=middleY&&(y+i*30+30)>=middleY) middleContent = text; p.setTextAlign(Paint.Align.CENTER); canvas.drawText(text, x, 0, p); // mY- mY/lines*(index - i) canvas.translate(0, DY); } } @Override protected void onSizeChanged(int w, int h, int ow, int oh) { super.onSizeChanged(w, h, ow, oh); mX = w * 0.5f; // remember the center of the screen mY = h; middleY = h*0.5f; } @Override public boolean onTouchEvent(MotionEvent event) { float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mTouchHistoryY += mTouchCurrY - mTouchStartY; mTouchStartY =mTouchCurrY= y; invalidate(); break; case MotionEvent.ACTION_MOVE: mTouchCurrY = y; invalidate(); break; case MotionEvent.ACTION_UP: Log.v("Lyric content", middleContent.length()+""); CharSequence chars = new CharSequence(){ @Override public char charAt(int index) { // TODO Auto-generated method stub return middleContent.charAt(index); } @Override public int length() { // TODO Auto-generated method stub return middleContent.length(); } @Override public CharSequence subSequence(int start, int end) { // TODO Auto-generated method stub return middleContent.subSequence(start, end); } @Override public String toString(){ return middleContent; } }; Toast toast = Toast.makeText(SampleView.this.getContext(),chars, 1000); toast.show(); invalidate(); break; } return true; } public void updateIndex(long time) { this.currentTime = time; index = mLyric.getNowSentenceIndex(time); Sentence sen = list.get(index); currentDunringTime = sen.getDuring(); sentenctTime = sen.getFromTime(); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
本文題目:android播放器實(shí)現(xiàn)歌詞顯示功能
本文URL:http://www.rwnh.cn/article28/jepjcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、建站公司、微信公眾號、軟件開發(fā)、響應(yīng)式網(wǎng)站、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)