weknowhowyoufeel/AffdexMe/app/src/main/java/com/affectiva/affdexme/MetricView.java

116 lines
3.8 KiB
Java

package com.affectiva.affdexme;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;
import java.lang.Math;
/**
* The MetricView class is used to display metric scores on top of colored bars whose color depend on the score.
*/
public class MetricView extends View {
float midX = 0; //coordinates of the center of the view
float midY = 0;
float halfWidth = 50;//default width and height of view
float height = 10;
String text = ""; //score in text format
Paint textPaint;
Paint boxPaint;
float left = 0; //colored bar is drawn using left,right,top, and height variables
float right = 0;
float top = 0;
float textBottom = 0; //tells our view where to draw the baseline of the font
public MetricView(Context context) {
super(context);
initResources(context,null);
}
public MetricView(Context context, AttributeSet attrs) {
super(context,attrs);
initResources(context,attrs);
}
public MetricView(Context context, AttributeSet attrs, int styleID){
super(context, attrs, styleID);
initResources(context,attrs);
}
void initResources(Context context, AttributeSet attrs) {
boxPaint = new Paint();
boxPaint.setColor(Color.GREEN);
textPaint = new Paint();
textPaint.setStyle(Paint.Style.FILL);
textPaint.setTextAlign(Paint.Align.CENTER);
int textSize = 15; //default text size value
//load and parse XML attributes
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.custom_attributes,0,0);
textPaint.setColor(a.getColor(R.styleable.custom_attributes_textColor, Color.BLACK));
textSize = a.getDimensionPixelSize(R.styleable.custom_attributes_textSize, textSize);
textPaint.setTextSize(textSize);
halfWidth = a.getDimensionPixelSize(R.styleable.custom_attributes_barLength,100)/2;
a.recycle();
} else {
textPaint.setColor(Color.BLACK);
textPaint.setTextSize(textSize);
}
/**
* We set the desired height of the view to be as large as our text.
* We also offset the bottom line at which our text is drawn, to give the appearance
* that the text is centered vertically.
*/
height = textSize;
textBottom = height - 5;
}
public void setTypeface(Typeface face) {
textPaint.setTypeface(face);
}
public void setScore(float s){
text = String.format("%.0f%%", s); //change the text of the view
left = midX - (halfWidth * (s / 100)); //change the coordinates at which the colored bar will be drawn
right = midX + (halfWidth * (s / 100));
invalidate(); //instruct Android to re-draw our view, now that the text has changed
}
/**
* set our view to be the minimum of the sizes that Android will allow and our desired sizes
* **/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension((int)Math.min(MeasureSpec.getSize(widthMeasureSpec), halfWidth *2), (int)Math.min(MeasureSpec.getSize(heightMeasureSpec),height));
}
@Override
protected void onSizeChanged(int w, int h, int oldW, int oldH) {
super.onSizeChanged(w,h,oldW,oldH);
midX = w/2;
midY = h/2;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//draws the colored bar that appears behind our score
canvas.drawRect(left,top,right,height, boxPaint);
//draws the score
canvas.drawText(text,midX , textBottom, textPaint);
}
}