You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BarChartView.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package com.feemers.android.fftdrawer;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.util.AttributeSet;
  6. import android.util.Log;
  7. import android.view.SurfaceHolder;
  8. import android.view.SurfaceView;
  9. public class BarChartView extends SurfaceView implements Runnable {
  10. private Animation animation;
  11. private SurfaceHolder surfaceHolder;
  12. private Thread thread = null;
  13. private volatile boolean running = false;
  14. private int backgroudColor = 0;
  15. private String name = "";
  16. private int sleepTime = 0;
  17. private volatile boolean update;
  18. public BarChartView(Context context) {
  19. super(context);
  20. surfaceHolder = getHolder();
  21. }
  22. public BarChartView(Context context, AttributeSet attributeSet){
  23. super(context, attributeSet);
  24. surfaceHolder = getHolder();
  25. ReadXML(attributeSet);
  26. }
  27. private void ReadXML(AttributeSet attributeSet){
  28. android.content.res.TypedArray typedArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.BarChartView);
  29. //String s = typedArray.getString(R.styleable.GrafikView_android_text);
  30. //name = (s!=null) ? s : "";
  31. backgroudColor = typedArray.getColor(R.styleable.BarChartView_backgroundColor, Color.BLACK);
  32. typedArray.recycle();
  33. log(", hintergrundFarbe=" + backgroudColor);
  34. }
  35. public void setAnimation(Animation animation){
  36. this.animation = animation;
  37. this.animation.SetBackgroundColor(backgroudColor);
  38. }
  39. private void log(String s) {
  40. Log.d(this.getClass().getSimpleName(), s);
  41. }
  42. public void Start(){
  43. running = true;
  44. thread = new Thread(this);
  45. thread.start();
  46. log("Thread started");
  47. }
  48. public void Stop(){
  49. running = false;
  50. while(true){
  51. try{
  52. thread.join();
  53. log("Thread stopped");
  54. break;
  55. }catch (InterruptedException e){
  56. e.printStackTrace();
  57. }
  58. }
  59. }
  60. public void Draw(){
  61. update = true;
  62. }
  63. @Override
  64. public void run() {
  65. while (running) {
  66. if (update) {
  67. if (!surfaceHolder.getSurface().isValid()) {
  68. continue;
  69. }
  70. Canvas canvas = surfaceHolder.lockCanvas();
  71. if (animation != null) {
  72. animation.Draw(canvas);
  73. update = false;
  74. }
  75. surfaceHolder.unlockCanvasAndPost(canvas);
  76. }
  77. try {
  78. Thread.sleep(5);
  79. } catch (InterruptedException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. }
  84. }