Skip to content

Commit

Permalink
make LyricView a runnable and usage outside easier
Browse files Browse the repository at this point in the history
  • Loading branch information
markzhai committed Jul 23, 2015
1 parent 6186f0c commit 73a4e5c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 121 deletions.
81 changes: 1 addition & 80 deletions app/src/main/java/cn/zhaiyifan/lyricview/demo/DemoActivity.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,14 @@
package cn.zhaiyifan.lyricview.demo;

import android.app.Activity;
import android.os.Handler;
import android.os.Bundle;
import android.view.WindowManager;

import cn.zhaiyifan.lyricview.LyricUtils;
import cn.zhaiyifan.lyricview.widget.LyricView;

public class DemoActivity extends Activity {

private LyricView mLyricView;
private boolean mIsForeground;
private Handler mHandler = new Handler();
private UIUpdateRunnable mUiUpdateRunnable = null;

private Runnable mUpdateResultsRunnable = new Runnable() {
public void run() {
mLyricView.invalidate();
}
};

private Runnable mClearScreenOnFlagRunnable = new Runnable() {
public void run() {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
};

@Override
protected void onResume() {
super.onResume();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mIsForeground = true;
}

@Override
public void onStop() {
super.onStop();
mIsForeground = false;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -48,55 +18,6 @@ protected void onCreate(Bundle savedInstanceState) {
mLyricView.setLyric(LyricUtils.parseLyric(
getResources().openRawResource(R.raw.testfile), "UTF-8"));
mLyricView.setLyricIndex(0);
}

private class UIUpdateRunnable implements Runnable {
private long mStartTime = -1;
private long mNextSentenceTime = -1;
private boolean mStopping = false;
private boolean mStopped = false;

public void reset() {
mStartTime = -1;
mNextSentenceTime = -1;
mStopping = false;
}

public void stop() {
mStopping = true;
}

public boolean isStopped() {
return mStopped;
}

// TODO: Improve user touch response
public void run() {
if (mStartTime == -1) {
mStartTime = System.currentTimeMillis();
}

while (mLyricView.getLyricIndex() != -2) {
if (mStopping) {
mStopped = true;
return;
}
long ts = System.currentTimeMillis() - mStartTime;
if (ts >= mNextSentenceTime || mLyricView.checkUpdate()) {
mNextSentenceTime = mLyricView.updateIndex(ts);

// Redraw only when window is visible
if (mIsForeground) {
mHandler.post(mUpdateResultsRunnable);
}
}
if (mNextSentenceTime == -1) {
mStopped = true;
// Clear KEEP_SCREEN_ON flag when finish playing
mHandler.post(mClearScreenOnFlagRunnable);
return;
}
}
}
mLyricView.play();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cn.zhaiyifan.lyricview.widget;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.preference.PreferenceManager;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.text.method.ScrollingMovementMethod;
import android.util.AttributeSet;
import android.view.MotionEvent;
Expand All @@ -16,17 +16,14 @@
import java.util.List;

import cn.zhaiyifan.lyricview.LyricUtils;
import cn.zhaiyifan.lyricview.R;
import cn.zhaiyifan.lyricview.model.Lyric;

/**
* A Scrollable TextView which use lyric stream as input and display it.
* <p/>
* Created by yifan on 5/13/14.
*/
public class LyricView extends TextView {
private static final String TAG = LyricView.class.getSimpleName();

public class LyricView extends TextView implements Runnable {
private static final int DY = 50;
private Lyric mLyric;
private Paint mCurrentPaint;
Expand All @@ -37,12 +34,9 @@ public class LyricView extends TextView {

private int mLyricIndex = 0;
private int mLyricSentenceLength;
private boolean mTouchActionMoving = false;
private boolean mIsNeedUpdate = false;
private float mLastEffectY = 0;

// Use duration of current line of lyric to sleep
// private long currentDuringTime;
private int mIsTouched = 0;

public LyricView(Context context) {
Expand All @@ -55,10 +49,10 @@ public LyricView(Context context, AttributeSet attrs) {

public LyricView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
init();
}

private void init(Context context) {
private void init() {
setFocusable(true);

int backgroundColor = Color.BLACK;
Expand Down Expand Up @@ -96,9 +90,8 @@ private int drawText(Canvas canvas, Paint paint, String text, float startY) {
int startIndex = 0;
int endIndex = Math.min((int) ((float) length * (width / textWidth)), length - 1);
int perLineLength = endIndex - startIndex;
//Log.d(TAG, String.valueOf(endIndex));

LinkedList<String> lines = new LinkedList<String>();
LinkedList<String> lines = new LinkedList<>();
lines.add(text.substring(startIndex, endIndex));
while (endIndex < length - 1) {
startIndex = endIndex;
Expand All @@ -118,11 +111,11 @@ private int drawText(Canvas canvas, Paint paint, String text, float startY) {
mPaint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(text, mMiddleX, startY, paint);
}
//Log.d(TAG, "Draw line: " + line);
return line;
}

protected void onDraw(Canvas canvas) {
@Override
protected void onDraw(@NonNull Canvas canvas) {
super.onDraw(canvas);
if (mLyric == null)
return;
Expand Down Expand Up @@ -189,7 +182,7 @@ protected void onSizeChanged(int w, int h, int ow, int oh) {
}

@Override
public boolean onTouchEvent(MotionEvent event) {
public boolean onTouchEvent(@NonNull MotionEvent event) {
final int action = event.getActionMasked();
final boolean superResult = super.onTouchEvent(event);
if (mLyric == null) {
Expand All @@ -202,21 +195,18 @@ public boolean onTouchEvent(MotionEvent event) {
switch (action) {
case MotionEvent.ACTION_MOVE:
mIsTouched = 3;
mTouchActionMoving = true;
if (mTouchActionMoving) {
float y = event.getY();
if (mLastEffectY != 0) {
if (mLastEffectY - y > 10) {
int times = (int) ((mLastEffectY - y) / 10);
mLastEffectY = y;
mLyric.offset += times * -100;
offsetChanged = true;
} else if (mLastEffectY - y < -10) {
int times = -(int) ((mLastEffectY - y) / 10);
mLastEffectY = y;
mLyric.offset += times * 100;
offsetChanged = true;
}
float y = event.getY();
if (mLastEffectY != 0) {
if (mLastEffectY - y > 10) {
int times = (int) ((mLastEffectY - y) / 10);
mLastEffectY = y;
mLyric.offset += times * -100;
offsetChanged = true;
} else if (mLastEffectY - y < -10) {
int times = -(int) ((mLastEffectY - y) / 10);
mLastEffectY = y;
mLyric.offset += times * 100;
offsetChanged = true;
}
}
handled = true;
Expand All @@ -228,7 +218,6 @@ public boolean onTouchEvent(MotionEvent event) {
break;
case MotionEvent.ACTION_UP:
System.currentTimeMillis();
mTouchActionMoving = false;
mLastEffectY = 0;
handled = true;
break;
Expand All @@ -240,7 +229,6 @@ public boolean onTouchEvent(MotionEvent event) {
if (offsetChanged) {
mIsNeedUpdate = true;
}
invalidate();
return true;
}

Expand Down Expand Up @@ -278,10 +266,6 @@ public synchronized void setLyric(Lyric lyric, boolean resetIndex) {
}
}

public int getLyricIndex() {
return mLyricIndex;
}

public void setLyricIndex(int index) {
mLyricIndex = index;
}
Expand All @@ -306,11 +290,54 @@ public boolean checkUpdate() {
return false;
}

public Lyric getLyric() {
return mLyric;
}

public synchronized void setLyric(Lyric lyric) {
setLyric(lyric, true);
}

public void play() {
mStop = false;
Thread thread = new Thread(this);
thread.start();
}

public void stop() {
mStop = true;
}

private long mStartTime = -1;
private boolean mStop = true;
private boolean mIsForeground = true;
long mNextSentenceTime = -1;

private Handler mHandler = new Handler();

@Override
public void run() {
if (mStartTime == -1) {
mStartTime = System.currentTimeMillis();
}

while (mLyricIndex != -2) {
if (mStop) {
return;
}
long ts = System.currentTimeMillis() - mStartTime;
if (ts >= mNextSentenceTime || checkUpdate()) {
mNextSentenceTime = updateIndex(ts);

// Redraw only when window is visible
if (mIsForeground) {
mHandler.post(new Runnable() {
@Override
public void run() {
invalidate();
}
});
}
}
if (mNextSentenceTime == -1) {
mStop = true;
}
}
}
}

0 comments on commit 73a4e5c

Please sign in to comment.