/*
* File: RoundRectView.java
* This file is part of Tico, an application to create and perform
* interactive communication boards to be used by people with
* severe motor disabilities.
*
* Author: Eduardo Ferrer
*
* Date: May-2012
*
* Company: Dept. of Computer Sciences and Systems Engineering, Universidad de Zaragoza, Spain
*
*
* License:
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package android.TICO.Views;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.GradientDrawable;
import android.graphics.RectF;
import android.view.View;
/**
* Implementation of a RoundRectView
view.
*
* @author Eduardo Ferrer
* @version 1.0 May, 2012
*/
public class RoundRectView extends View{
Paint paint=new Paint();
Paint paintInterior=new Paint();
int startX,startY,stopX,stopY;
//int borderWidth;
GradientDrawable gradient_color;
/**
* Creates a new RoundRectView
for the specified params.
*
* @param startX The starting coordinate in X of the rectangle
* @param startY The starting coordinate in Y of the rectangle
* @param stopX The ending coordinate in X of the rectangle
* @param stopY The ending coordinate in Y of the rectangle
* @param context The Context the view is running in, through which it can access the current theme, resources, etc.
*/
public RoundRectView(int startX,int startY,int stopX,int stopY,Context context){
super(context);
paint.setColor(Color.WHITE);
this.startX=startX;
this.startY=startY;
this.stopX=stopX;
this.stopY=stopY;
paint.setStyle(Style.STROKE);
paintInterior.setStyle(Style.FILL);
paintInterior.setColor(Color.TRANSPARENT);
gradient_color=null;
}
public void onDraw(Canvas canvas){
RectF rect=new RectF();
rect.set(startX, startY, stopX, stopY);
rect.inset(paint.getStrokeWidth(), paint.getStrokeWidth());
int arcSize=getArcSize((int)(rect.width()+paint.getStrokeWidth()), (int)(rect.height()+paint.getStrokeWidth()));
//interior
if(gradient_color!=null){
gradient_color.setBounds((int)rect.left, (int)rect.top, (int)rect.right, (int)rect.bottom);
gradient_color.setCornerRadius(arcSize/2);
gradient_color.draw(canvas);
}
if(paintInterior.getColor()!=Color.TRANSPARENT)
canvas.drawRoundRect(rect, arcSize/2, arcSize/2, paintInterior);
canvas.drawRoundRect(rect, arcSize/2, arcSize/2, paint);
}
/**
* Set the RoundRectView
color of the line. Note that the color is an int containing alpha as well as r,g,b.
* This 32bit value is not premultiplied, meaning that its alpha can be any value, regardless of the values of r,g,b.
* See the Color class for more details.
*
* @param color The new color (including alpha) to set as color of the round rectangle line.
*/
public void setColor(int color){
paint.setColor(color);
}
/**
* Set the width for stroking. Pass 0 to stroke in hairline mode. Hairlines always draws a single pixel independent of the canva's matrix.
*
* @param border set the RoundRectView
's stroke width of the line.
*
*/
public void setBorderWidth(int border){
paint.setStrokeWidth(border);
}
/**
* Set the RoundRectView
color of the rectangle interior. Note that the color is an int containing alpha as well as r,g,b.
* This 32bit value is not premultiplied, meaning that its alpha can be any value, regardless of the values of r,g,b.
* See the Color class for more details.
*
* @param color The new color (including alpha) to set as color of the rectangle interior.
*/
public void setBackgroundColor(int color){
gradient_color=null;
paintInterior.setColor(color);
this.invalidate();
}
/**
* Set the background frame gradient color for the RoundRectView
* @param colorIn The gradient color
of the RoundRectView background
*/
public void setBackgroundDrawable(GradientDrawable colorIn){
gradient_color=colorIn;
gradient_color.setShape(GradientDrawable.RECTANGLE);
//color.draw(canvas);
this.invalidate();//force a redraw
}
/**
* Calculates an appropriate arc for the corners of the rectangle
* for boundary size cases of width and height.
*
* @return The calculated arc size
*/
private static int getArcSize(int width, int height) {
int arcSize;
// The arc width of a activity rectangle is 1/5th of the larger
// of the two of the dimensions passed in, but at most 1/2
// of the smaller of the two. 1/5 because it looks nice and 1/2
// so the arc can complete in the given dimension
if (width <= height) {
arcSize = height / 5;
if (arcSize > (width / 2)) {
arcSize = width / 2;
}
} else {
arcSize = width / 5;
if (arcSize > (height / 2)) {
arcSize = height / 2;
}
}
// Log.d("arcsize",""+arcSize);
return arcSize;
}
}