/* * File: RectangleView.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.RectF; import android.graphics.Paint.Style; import android.graphics.drawable.GradientDrawable; import android.view.View; /** * Implementation of a RectangleView view. * * @author Eduardo Ferrer * @version 1.0 May, 2012 */ public class RectangleView extends View{ Paint paint=new Paint(); Paint paintInterior=new Paint(); int startX,startY,stopX,stopY; GradientDrawable gradient_color;//borrar /** * Creates a new RectangleView 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 RectangleView(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; //borrar /*color=new GradientDrawable(); color.setShape(GradientDrawable.RECTANGLE); RectF rect=new RectF(); rect.set(startX-paint.getStrokeWidth()/2.0f, startY-paint.getStrokeWidth()/2, stopX+paint.getStrokeWidth()/2, stopY+paint.getStrokeWidth()/2.0f); rect.inset(paint.getStrokeWidth(), paint.getStrokeWidth()); color.setBounds((int)rect.left, (int)rect.top, (int)rect.right, (int)rect.bottom);*/ } public void onDraw(Canvas canvas){ RectF rect=new RectF(); rect.set(startX-paint.getStrokeWidth()/2.0f, startY-paint.getStrokeWidth()/2, stopX+paint.getStrokeWidth()/2, stopY+paint.getStrokeWidth()/2.0f); rect.inset(paint.getStrokeWidth(), paint.getStrokeWidth()); //borrar if(gradient_color!=null){ //pintamos fondo con gradiente gradient_color.setBounds((int)rect.left, (int)rect.top, (int)rect.right, (int)rect.bottom); gradient_color.draw(canvas); } else //pintamos color de fondo unico if(paintInterior.getColor()!=Color.TRANSPARENT) canvas.drawRect(rect, paintInterior); canvas.drawRect(rect, paint); } /** * Set the RectangleView 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 rectangle line. */ public void setColor(int color){ paint.setColor(color); this.invalidate(); } /** * 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 RectangleView's stroke width of the line. * */ public void setBorderWidth(int border){ paint.setStrokeWidth(border); this.invalidate();//force a redraw } /** * Set the RectangleView 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){ //quitamos fondo con gradiente gradient_color=null; paintInterior.setColor(color); this.invalidate();//force a redraw } /** * Set the background frame gradient color for the RectangleView * @param colorIn The gradient color of the RectangleView background */ public void setBackgroundDrawable(GradientDrawable colorIn){ gradient_color=colorIn; gradient_color.setShape(GradientDrawable.RECTANGLE); //color.draw(canvas); this.invalidate();//force a redraw } /** * Changes the RectangleView position and size * * @param startX The new starting coordinate in X of the rectangle * @param startY The new starting coordinate in Y of the rectangle * @param stopX The new ending coordinate in X of the rectangle * @param stopY The new ending coordinate in Y of the rectangle */ public void setRect(int startX,int startY,int stopX,int stopY){ this.startX=startX; this.startY=startY; this.stopX=stopX; this.stopY=stopY; this.invalidate();//force a redraw } }