Android 音乐播放器实现歌词显示(2)

int alphaValue = 25;
  float tempY = mMiddleY;
  for (int i = mIndex - 1; i >= 0; i--) {
   tempY -= DY;
   if (tempY < 0) {
    break;
   }
   p.setColor(Color.argb(255 - alphaValue, 245, 245, 245));
   canvas.drawText((String) mWordsList.get(i), mX, tempY, p);
   alphaValue += 25;
  }
  alphaValue = 25;
  tempY = mMiddleY;
  for (int i = mIndex + 1, len = mWordsList.size(); i < len; i++) {
   tempY += DY;
   if (tempY > mY) {
    break;
   }
   p.setColor(Color.argb(255 - alphaValue, 245, 245, 245));
   canvas.drawText((String) mWordsList.get(i), mX, tempY, p);
   alphaValue += 25;
  }
  mIndex++;
 }

@Override
 protected void onSizeChanged(int w, int h, int ow, int oh) {
  super.onSizeChanged(w, h, ow, oh);

mX = w * 0.5f;
  mY = h;
  mMiddleY = h * 0.3f;
 }

@SuppressLint("SdCardPath")
 private void init() throws IOException {
  setFocusable(true);

LrcHandle lrcHandler = new LrcHandle();
  lrcHandler.readLRC("/sdcard/aa.lrc");
  mWordsList = lrcHandler.getWords();

mLoseFocusPaint = new Paint();
  mLoseFocusPaint.setAntiAlias(true);
  mLoseFocusPaint.setTextSize(22);
  mLoseFocusPaint.setColor(Color.WHITE);
  mLoseFocusPaint.setTypeface(Typeface.MONOSPACE);

mOnFocusePaint = new Paint();
  mOnFocusePaint.setAntiAlias(true);
  mOnFocusePaint.setColor(Color.YELLOW);
  mOnFocusePaint.setTextSize(40);
  mOnFocusePaint.setTypeface(Typeface.SANS_SERIF);
 }
}

MainActivity.java:

package com.example.welcome;

import java.io.IOException;
import java.util.List;

import android.R.integer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
 private WordView mWordView;
 private List mTimeList;
 private MediaPlayer mPlayer;

@SuppressLint("SdCardPath")
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

Button button = (Button) findViewById(R.id.button);
  button.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    mPlayer.stop();
    finish();
   }
  });

mWordView = (WordView) findViewById(R.id.text);
  mPlayer = new MediaPlayer();
  mPlayer.reset();
  LrcHandle lrcHandler = new LrcHandle();
  try {
   lrcHandler.readLRC("/sdcard/aa.lrc");
   mTimeList = lrcHandler.getTime();
   mPlayer.setDataSource("/sdcard/e.mp3");
   mPlayer.prepare();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (IllegalArgumentException e) {
   e.printStackTrace();
  } catch (SecurityException e) {
   e.printStackTrace();
  } catch (IllegalStateException e) {
   e.printStackTrace();
  }
  final Handler handler = new Handler();
  mPlayer.start();
  new Thread(new Runnable() {
   int i = 0;

@Override
   public void run() {
    while (mPlayer.isPlaying()) {
     handler.post(new Runnable() {
      @Override
      public void run() {
       mWordView.invalidate();
      }
     });
     try {
      Thread.sleep(Integer.parseInt(mTimeList.get(i + 1)
        .toString())
        - Integer.parseInt(mTimeList.get(i).toString()));
     } catch (InterruptedException e) {
     }
     i++;
     if (i == mTimeList.size() - 1) {
      mPlayer.stop();
      break;
     }
    }
   }
  }).start();
 }
}

main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/c53efd41d462e19ab57e1a317c99fb62.html