Bars View
/*
Amarino - A prototyping software toolkit for Android and Arduino
Copyright (c) 2010 Bonifaz Kaufmann. All right reserved.
This application and its library is free software; you can redistribute
it and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
import android.content.Context;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
class BarsView extends Visualizer {
private static final String TAG = "BarsView";
private static final int NUM_BARS = 3;
private int activeBars = 0;
private Bitmap mBitmap;
private Canvas mCanvas = new Canvas();
private int[] mColor = new int[3];
private float barHeight;
public BarsView(Context context) {
super(context);
init();
}
public BarsView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init(){
mColor[0] = Color.argb(255, 0, 200, 0); // g
mColor[1] = Color.argb(255, 200, 200, 0); // y
mColor[2] = Color.argb(255, 200, 0, 0); // r
activeBars = 1;
}
@Override
public void setBoundaries(float min, float max) {
super.setBoundaries(min, max);
}
@Override
public void setData(float value) {
mCanvas.drawColor(0xFF111111);
updateBar(value, 0);
invalidate();
}
@Override
public void setData(float[] values) {
mCanvas.drawColor(0xFF111111);
int length = values.length;
if (length > activeBars){
activeBars = length;
}
for (int i=0; i<length; i++){
updateBar(values[i], i);
}
invalidate();
}
private void updateBar(float value, final int pos){
value += minValue;
final Paint paint = mPaint;
final float v = value * mScaleX;
paint.setColor(mColor[pos]);
mCanvas.drawRect(minValue * mScaleX, barHeight*pos, v, barHeight*(pos+1), paint);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
barHeight = mYOffset / NUM_BARS;
// TODO when screen size changes sometimes w or h == 0 -> Exception
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
mCanvas.setBitmap(mBitmap);
mCanvas.drawColor(0xFF111111);
}
@Override
protected void onDraw(Canvas canvas) {
synchronized (this) {
if (mBitmap != null) {
// draw bars outline
final float v1 = minValue * mScaleX;
final Canvas cavas = mCanvas;
final Paint paint = mPaint;
paint.setColor(0x44996666);
for (int i=0; i<NUM_BARS;i++){
cavas.drawLine(0, barHeight*i, mWidth, barHeight*i, paint);
}
paint.setColor(0xaa996666);
cavas.drawLine(v1, mYOffset, v1, 0, paint);
cavas.drawText(min, 1, mYOffset-1, paint);
cavas.drawText(max, mWidth-textWidth-1, mYOffset-1, paint);
canvas.drawBitmap(mBitmap, 0, 0, null);
}
}
}
}
abstract class Visualizer extends View {
Paint mPaint = new Paint();
float maxValue = 1024f;
float minValue = 0f;
float mScaleX;
float mScaleY;
float mYOffset;
float mWidth;
String min, max;
float textWidth = 0;
float textHeight = 0;
public Visualizer(Context context) {
super(context);
}
public Visualizer(Context context, AttributeSet attrs) {
super(context, attrs);
}
public abstract void setData(float value);
public abstract void setData(float[] values);
public void setBoundaries(float min, float max){
this.min = String.valueOf(min);
this.max = String.valueOf(max);
minValue = -min;
maxValue = max - min;
mScaleY = - (mYOffset * (1.0f / maxValue));
mScaleX = (mWidth * (1.0f / maxValue));
float[] sizes = new float[this.max.length()];
textHeight = mPaint.getTextSize();
mPaint.getTextWidths(this.max, sizes);
for (float f : sizes)
textWidth += f;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mYOffset = h;
mWidth = w;
mScaleY = - (h * (1.0f / maxValue));
mScaleX = (w * (1.0f / maxValue));
super.onSizeChanged(w, h, oldw, oldh);
}
/* SIMPLE CAST FUNCTIONS */
public void setData(int value){
setData((float)value);
}
public void setData(double value){
setData((float)value);
}
public void setData(short value){
setData((float)value);
}
public void setData(long value){
setData((float)value);
}
public void setData(byte value){
setData((float)value);
}
public void setData(boolean value){
if (value)
setData(1f);
else
setData(0f);
}
public void setData(int[] values){
final int length = values.length;
float[] arr = new float[length];
for (int i=0;i<length;i++)
arr[i] = values[i];
setData(arr);
}
public void setData(double[] values){
final int length = values.length;
float[] arr = new float[length];
for (int i=0;i<length;i++)
arr[i] = (float)values[i];
setData(arr);
}
public void setData(short[] values){
final int length = values.length;
float[] arr = new float[length];
for (int i=0;i<length;i++)
arr[i] = values[i];
setData(arr);
}
public void setData(long[] values){
final int length = values.length;
float[] arr = new float[length];
for (int i=0;i<length;i++)
arr[i] = values[i];
setData(arr);
}
public void setData(byte[] values){
final int length = values.length;
float[] arr = new float[length];
for (int i=0;i<length;i++)
arr[i] = values[i];
setData(arr);
}
public void setData(boolean[] values){
final int length = values.length;
float[] arr = new float[length];
for (int i=0;i<length;i++)
arr[i] = values[i] ? 1 : 0;
setData(arr);
}
public void setData(String[] values){
final int length = values.length;
float[] arr = new float[length];
for (int i=0;i<length;i++)
arr[i] = Float.valueOf(values[i]);
setData(arr);
}
}
Related examples in the same category