/*
* File: LineView.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: Nov, 2011
*
* Company: Universidad de Zaragoza, CPS, DIIS
*
* 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.RectF;
import android.view.View;
/**
* Implementation of a LineView
view.
*
* @author Eduardo Ferrer
* @version 2.0 May, 2012
*/
public class LineView extends View{
Paint paint=new Paint();
float startX;
float startY;
float stopX,stopY;
/**
* Creates a new LineView
for the specified params.
*
* @param startX The starting coordinate in X of the line
* @param startY The starting coordinate in Y of the line
* @param stopX The ending coordinate in X of the line
* @param stopY The ending coordinate in Y of the line
* @param context The Context the view is running in, through which it can access the current theme, resources, etc.
*/
public LineView(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;
}
/**
* Creates a new line
for the specified params based on a rectangle diagonal.
*
* @param startCorner The starting corner of the rectangle for the diagonal.
* 0
for top left.
* 2
for top right.
* 5
for bottom left.
* 7
for bottom right.
* @param left The starting coordinate in X of the rectangle which contents the line
* @param top The starting coordinate in Y of the rectangle which contents the line
* @param right The ending coordinate in X of the rectangle which contents the line
* @param bottom The ending coordinate in Y of the rectangle which contents the line
* @param border set the LineView
's stroke width of the line.
* @param context The Context the view is running in, through which it can access the current theme, resources, etc.
*/
public LineView(int startCorner,int left, int top, int right, int bottom, int border, Context context){
super(context);
paint.setColor(Color.WHITE);
RectF rect= new RectF();
rect.set(left, top, right, bottom);
setBorderWidth(border);
switch(startCorner){
case 0: //esquina superior izquierda
this.startX=rect.left;//+paint.getStrokeWidth()/2;
this.startY=rect.top;//+paint.getStrokeWidth()/2;
this.stopX=rect.right;//-paint.getStrokeWidth()/2;
this.stopY=rect.bottom;//-paint.getStrokeWidth()/2;
break;
case 2: //esquina superior derecha
this.startX=rect.right;//-paint.getStrokeWidth()/2;
this.startY=rect.top;//+paint.getStrokeWidth()/2;
this.stopX=rect.left;//+paint.getStrokeWidth()/2;
this.stopY=rect.bottom;//-paint.getStrokeWidth()/2;
break;
case 5: //esquina inferior izquierda
this.startX=rect.left;//+paint.getStrokeWidth()/2;
this.startY=rect.bottom;//-paint.getStrokeWidth()/2;
this.stopX=rect.right;//-paint.getStrokeWidth()/2;
this.stopY=rect.top;//+paint.getStrokeWidth()/2;
break;
case 7: //esquina inferior derecha
this.startX=rect.right;//-paint.getStrokeWidth()/2;
this.startY=rect.bottom;//-paint.getStrokeWidth()/2;
this.stopX=rect.left;//+paint.getStrokeWidth()/2;
this.stopY=rect.top;//+paint.getStrokeWidth()/2;
break;
}
}
public void onDraw(Canvas canvas){
//canvas.drawLine(0, 0, 20, 20, paint);
//canvas.drawLine(20, 0, 0, 20, paint);
canvas.drawLine(startX, startY, stopX, stopY, paint);
}
/**
* Set de LineView
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 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 LineView
's stroke width of the line.
*
*/
public void setBorderWidth(int border){
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(border);
}
}