-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from GautamChibde/feature/line_bar_visualizer
Feature/line bar visualizer
- Loading branch information
Showing
18 changed files
with
209 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
audiovisualizer/src/main/java/com/chibde/visualizer/LineBarVisualizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.chibde.visualizer; | ||
|
||
import android.content.Context; | ||
import android.graphics.Canvas; | ||
import android.graphics.Color; | ||
import android.graphics.Paint; | ||
import android.support.annotation.Nullable; | ||
import android.util.AttributeSet; | ||
|
||
import com.chibde.BaseVisualizer; | ||
|
||
/** | ||
* Custom view that creates a Line and Bar visualizer effect for | ||
* the android {@link android.media.MediaPlayer} | ||
* <p> | ||
* Created by gautam chibde on 22/11/17. | ||
*/ | ||
|
||
public class LineBarVisualizer extends BaseVisualizer { | ||
private Paint middleLine; | ||
private float density; | ||
private int gap; | ||
|
||
public LineBarVisualizer(Context context) { | ||
super(context); | ||
} | ||
|
||
public LineBarVisualizer(Context context, @Nullable AttributeSet attrs) { | ||
super(context, attrs); | ||
} | ||
|
||
public LineBarVisualizer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
} | ||
|
||
@Override | ||
protected void init() { | ||
density = 50; | ||
gap = 4; | ||
middleLine = new Paint(); | ||
middleLine.setColor(Color.BLUE); | ||
} | ||
|
||
/** | ||
* Sets the density to the Bar visualizer i.e the number of bars | ||
* to be displayed. Density can vary from 10 to 256. | ||
* by default the value is set to 50. | ||
* | ||
* @param density density of the bar visualizer | ||
*/ | ||
public void setDensity(float density) { | ||
if (this.density > 180) { | ||
this.middleLine.setStrokeWidth(1); | ||
this.gap = 1; | ||
} else { | ||
this.gap = 4; | ||
} | ||
this.density = density; | ||
if (density > 256) { | ||
this.density = 250; | ||
this.gap = 0; | ||
} else if (density <= 10) { | ||
this.density = 10; | ||
} | ||
} | ||
|
||
@Override | ||
protected void onDraw(Canvas canvas) { | ||
if (middleLine.getColor() != Color.BLUE) { | ||
middleLine.setColor(color); | ||
} | ||
if (bytes != null) { | ||
float barWidth = getWidth() / density; | ||
float div = bytes.length / density; | ||
canvas.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2, middleLine); | ||
paint.setStrokeWidth(barWidth - gap); | ||
|
||
for (int i = 0; i < density; i++) { | ||
int x = (int) Math.ceil(i * div); | ||
int top = canvas.getHeight() / 2 | ||
+ (128 - Math.abs(bytes[x])) | ||
* (canvas.getHeight() / 2) / 128; | ||
|
||
int bottom = canvas.getHeight() / 2 | ||
- (128 - Math.abs(bytes[x])) | ||
* (canvas.getHeight() / 2) / 128; | ||
|
||
canvas.drawLine(i * barWidth, bottom, i * barWidth, getHeight() / 2, paint); | ||
canvas.drawLine(i * barWidth, top, i * barWidth, getHeight() / 2, paint); | ||
} | ||
super.onDraw(canvas); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.