六:绘制视图时方法汇总
/**
* 得到一个随机颜色
* [@return](https://my.oschina.net/u/556800) 随机颜色
*/
public int randomColor() {
Random random = new Random();
int red = random.nextInt(256) ;
int green = random.nextInt(256) ;
int blue = random.nextInt(256);
return Color.rgb(red, green, blue);
}/**
* 通过透明度,算出对应颜色的浅色应当是什么效果
* @param color 颜色
* @param alpha 透明度
* @return 浅色
*/
public int getLightColor(int color,int alpha)
{
initPaint();
mPaint.setColor(color);
mPaint.setAlpha(alpha);
return mPaint.getColor();
}
/**
* 得到深色
* @param color 颜色
* @return 深色
*/
public int getDarkerColor(int color){
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[1] = hsv[1] + 0.1f;
hsv[2] = hsv[2] - 0.1f;
int darkerColor = Color.HSVToColor(hsv);
return darkerColor ;
} /**
* 得到单个字的高度
* @param paint 画笔
* @return 高度
*/
public float getPaintFontHeight(Paint paint)
{
FontMetrics fm = paint.getFontMetrics();
return (float) Math.ceil(fm.descent - fm.ascent);
}
/**
* 得到字符串的宽度
* @param paint 画笔
* @param str 字符串
* @return 宽度
*/
public float getTextWidth(Paint paint,String str)
{
if(str.length() == 0) return 0.0f;
//float width = Math.abs(paint.measureText(str, 0, str.length()));
return paint.measureText(str, 0, str.length());
}
/**
* 绘制旋转了指定角度的文字
* @param text 文字
* @param x X坐标
* @param y y坐标
* @param paint 画笔
* @param angle 角度
*/
public void drawRotateText( String text ,
float x ,float y,float angle,
Canvas canvas,
Paint paint
){
if("" == text|| text.length() == 0 ) return;
if(angle != 0){
canvas.rotate(angle, x, y);
//canvas.drawText(text, x, y, paint);
drawText(canvas,paint,text,x,y);
canvas.rotate(-1 * angle, x, y);
}else{
//canvas.drawText(text, x, y, paint);
drawText(canvas,paint,text,x,y);
}
}
public PathEffect getDotLineStyle()
{
return( new DashPathEffect(new float[] { 2, 2, 2, 2}, 1));
}
public PathEffect getDashLineStyle()
{
//虚实线
return(new DashPathEffect(new float[] { 4, 8, 5, 10}, 1));
}
/**
* 绘制点
* @param startX 起始点X坐标
* @param startY 起始点Y坐标
* @param stopX 终止点X坐标
* @param stopY 终止点Y坐标
* @param canvas 画布
* @param paint 画笔
*/
public void drawDotLine(float startX,float startY,
float stopX,float stopY,
Canvas canvas,
Paint paint)
{
//PathEffect effects = new DashPathEffect(new float[] { 2, 2, 2, 2}, 1);
paint.setPathEffect(getDotLineStyle());
canvas.drawLine(startX, startY, stopX, stopY, paint);
paint.setPathEffect(null);
}
/**
* 绘制虚实线
* @param startX 起始点X坐标
* @param startY 起始点Y坐标
* @param stopX 终止点X坐标
* @param stopY 终止点Y坐标
* @param canvas 画布
* @param paint 画笔
*/
public void drawDashLine(float startX,float startY,
float stopX,float stopY,
Canvas canvas,
Paint paint)
{
//虚实线
//PathEffect effects = new DashPathEffect(new float[] { 4, 8, 5, 10}, 1);
paint.setPathEffect(getDashLineStyle());
canvas.drawLine(startX, startY, stopX, stopY, paint);
paint.setPathEffect(null);
}//绘制有换行的文本
public float drawText(Canvas canvas,Paint paint,String text,float x,float y)
{
if(text.length() > 0 )
{
if( text.indexOf("/n") > 0 ){
float height = getPaintFontHeight(paint);
String[] arr = text.split("/n");
for(int i=0;i canvas.drawText(arr[i],x , y, paint);
y += height;
}
}else{
canvas.drawText(text,x , y, paint);
}
}
return y;
}