add BarCharView, is not running yet -_-

This commit is contained in:
Jan Feemers 2020-06-12 12:01:15 +02:00
parent 2eaf328d05
commit b2972b054a
7 changed files with 32 additions and 192 deletions

1
.idea/gradle.xml generated
View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>

25
.idea/jarRepositories.xml generated Normal file
View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="BintrayJCenter" />
<option name="name" value="BintrayJCenter" />
<option name="url" value="https://jcenter.bintray.com/" />
</remote-repository>
<remote-repository>
<option name="id" value="Google" />
<option name="name" value="Google" />
<option name="url" value="https://dl.google.com/dl/android/maven2/" />
</remote-repository>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -1,8 +0,0 @@
package com.feemers.android.fftdrawer;
import android.graphics.Canvas;
public interface Animation {
void Draw(Canvas canvas);
void SetBackgroundColor(int c);
}

View File

@ -1,28 +0,0 @@
package com.feemers.android.fftdrawer;
import android.graphics.Color;
public class Bar {
private float value;
private int harmonics;
private float frequency;
private final int color;
public Bar(float value, int harmonics) {
this.value = value;
this.harmonics = harmonics;
color = Color.rgb(0,255,0);
}
public float getValue(){
return value;
}
public int getHarmonics() {
return harmonics;
}
public int getColor(){
return color;
}
}

View File

@ -1,62 +0,0 @@
package com.feemers.android.fftdrawer;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import java.util.ArrayList;
public class BarChart implements Animation {
private final int length;
private ArrayList<Bar> bars = null;
private Paint paint;
private int backgroundColor;
public BarChart(int length){
this.length = length;
bars = new ArrayList<>();
paint = new Paint();
paint.setAntiAlias(true);
backgroundColor = Color.rgb(0,0,0);
}
public void newValues(double[] values){
bars.clear();
for(int i = 0; i < length; i++){
float tmp = (float) values[i];
bars.add(new Bar(tmp, i));
}
}
@Override
public void Draw(Canvas canvas) {
paint.setStyle(Paint.Style.FILL);
canvas.drawColor(backgroundColor);
// find max
float maxValue = 0;
for(Bar b : bars){
maxValue = maxValue >= b.getValue() ? maxValue : b.getValue();
}
for(Bar b : bars){
paint.setColor(b.getColor());
float width = canvas.getWidth();
float heigth = canvas.getHeight();
float widthOfBar = width / length;
float left= b.getHarmonics() * widthOfBar;
float top = heigth - heigth * b.getValue() / maxValue;
float rigth = (b.getHarmonics()+1) * widthOfBar;
float buttom = heigth;
canvas.drawRect(left, top, rigth ,buttom, paint);
}
}
@Override
public void SetBackgroundColor(int c) {
backgroundColor = c;
}
}

View File

@ -1,94 +0,0 @@
package com.feemers.android.fftdrawer;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class BarChartView extends SurfaceView implements Runnable {
private Animation animation;
private SurfaceHolder surfaceHolder;
private Thread thread = null;
private volatile boolean running = false;
private int backgroudColor = 0;
private String name = "";
private int sleepTime = 0;
private volatile boolean update;
public BarChartView(Context context) {
super(context);
surfaceHolder = getHolder();
}
public BarChartView(Context context, AttributeSet attributeSet){
super(context, attributeSet);
surfaceHolder = getHolder();
ReadXML(attributeSet);
}
private void ReadXML(AttributeSet attributeSet){
android.content.res.TypedArray typedArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.BarChartView);
//String s = typedArray.getString(R.styleable.GrafikView_android_text);
//name = (s!=null) ? s : "";
backgroudColor = typedArray.getColor(R.styleable.BarChartView_backgroundColor, Color.BLACK);
typedArray.recycle();
log(", hintergrundFarbe=" + backgroudColor);
}
public void setAnimation(Animation animation){
this.animation = animation;
this.animation.SetBackgroundColor(backgroudColor);
}
private void log(String s) {
Log.d(this.getClass().getSimpleName(), s);
}
public void Start(){
running = true;
thread = new Thread(this);
thread.start();
log("Thread started");
}
public void Stop(){
running = false;
while(true){
try{
thread.join();
log("Thread stopped");
break;
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
public void Draw(){
update = true;
}
@Override
public void run() {
while (running) {
if (update) {
if (!surfaceHolder.getSurface().isValid()) {
continue;
}
Canvas canvas = surfaceHolder.lockCanvas();
if (animation != null) {
animation.Draw(canvas);
update = false;
}
surfaceHolder.unlockCanvasAndPost(canvas);
}
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}