Skip to content

Commit

Permalink
Randomness added.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-compton committed Jul 30, 2014
1 parent 291db12 commit 0f9b5dc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import android.widget.SeekBar;
import android.widget.TextView;

import com.ambergleam.visualizer.utils.StringFormat;
import com.ambergleam.visualizer.utils.StringUtils;

import java.lang.reflect.Field;

Expand Down Expand Up @@ -151,7 +151,7 @@ private void stopTime() {
}

private void updateTime() {
mCurrentTimeTextView.setText(StringFormat.getTimeFormatted(mCurrentPosition));
mCurrentTimeTextView.setText(StringUtils.getTimeFormatted(mCurrentPosition));
}

private void setAudio(String name) {
Expand All @@ -176,8 +176,8 @@ private void setAudio(String name) {

private void updateUI(String name) {
mScreen.setVisibility(View.VISIBLE);
mTitleTextView.setText(StringFormat.getFileNameFormatted(name));
mDurationTimeTextView.setText(StringFormat.getTimeFormatted(mMediaPlayer.getDuration()));
mTitleTextView.setText(StringUtils.getFileNameFormatted(name));
mDurationTimeTextView.setText(StringUtils.getTimeFormatted(mMediaPlayer.getDuration()));
mSeekBar.setMax(mMediaPlayer.getDuration());
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
Expand Down Expand Up @@ -278,15 +278,15 @@ private void showDialog() {
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.select_dialog_singlechoice);
Field[] fields = R.raw.class.getFields();
for (Field field : fields) {
adapter.add(StringFormat.getFileNameFormatted(field.getName()));
adapter.add(StringUtils.getFileNameFormatted(field.getName()));
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Select Audio:");
builder.setIcon(android.R.drawable.ic_menu_search);
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
setAudio(StringFormat.getFileNameRaw(adapter.getItem(position)));
setAudio(StringUtils.getFileNameRaw(adapter.getItem(position)));
}
}
);
Expand Down
29 changes: 22 additions & 7 deletions app/src/main/java/com/ambergleam/visualizer/VisualizerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
import android.util.AttributeSet;
import android.view.View;

import com.ambergleam.visualizer.utils.RandomUtils;

public class VisualizerView extends View {

private Context mContext;

private byte[] mBytes;
private float[] mPoints;
private Paint mForePaint = new Paint();
private Rect mRect = new Rect();
private byte[] mBytes;

private Paint mForePaint = new Paint();
private float[] mPoints;

public VisualizerView(Context context) {
super(context);
Expand All @@ -38,8 +40,8 @@ public VisualizerView(Context context, AttributeSet attrs, int defStyle) {
private void init() {
mBytes = null;
mForePaint.setAntiAlias(true);
mForePaint.setStrokeWidth(5f);
mForePaint.setColor(mContext.getResources().getColor(R.color.line_default));
mForePaint.setStrokeWidth(RandomUtils.getRandomLineWidth());
mForePaint.setColor(RandomUtils.getRandomFadedColor());
}

public void updateVisualizer(byte[] bytes) {
Expand All @@ -50,6 +52,7 @@ public void updateVisualizer(byte[] bytes) {
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBackground();

if (mBytes == null) {
return;
Expand All @@ -59,16 +62,28 @@ protected void onDraw(Canvas canvas) {
mPoints = new float[mBytes.length * 4];
}

// for (int i = 1; i < 8; i++) {
// drawLines(canvas, i);
// }
drawLines(canvas, 2);

}

private void drawLines(Canvas canvas, int scale) {
mRect.set(0, 0, getWidth(), getHeight());

for (int i = 0; i < mBytes.length - 1; i++) {
mPoints[i * 4] = mRect.width() * i / (mBytes.length - 1);
mPoints[i * 4 + 1] = mRect.height() / 2 + ((byte) (mBytes[i] + 128)) * (mRect.height() / 2) / 128;
mPoints[i * 4 + 1] = mRect.height() / scale + ((byte) (mBytes[i] + 128)) * (mRect.height() / scale) / 128;
mPoints[i * 4 + 2] = mRect.width() * (i + 1) / (mBytes.length - 1);
mPoints[i * 4 + 3] = mRect.height() / 2 + ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / 2) / 128;
mPoints[i * 4 + 3] = mRect.height() / scale + ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / scale) / 128;
}

canvas.drawLines(mPoints, mForePaint);
}

private void drawBackground() {
setBackgroundColor(mContext.getResources().getColor(android.R.color.white));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.apache.commons.lang.WordUtils;

public class StringFormat {
public class StringUtils {

public static String getTimeFormatted(int ms) {
StringBuilder sb = new StringBuilder();
Expand Down
6 changes: 0 additions & 6 deletions app/src/main/res/values/colors.xml

This file was deleted.

0 comments on commit 0f9b5dc

Please sign in to comment.