20190109
2019-01-09 本文已影响0人
糖_分
看看这本书 -《Android游戏编程之从零开始》
Day_2 2019/01/09
Canvas 画布
画布类Canvas的一些常用函数如下显示
{
try
{
canvas = holder.lockCanvas();
if( canvas != null )
{
//----利用填充画布,刷屏
canvas.drawColor( Color.BLUE );
//----绘制文本
canvas.drawText( "Game" , 0 , 0 , paint );
//----绘制像素点
canvas.drawPoint( 10 , 20 , paint );
//----绘制多个像素点
canvas.drawPoints( new float[]{ 10 , 30 , 30 , 30 } , paint );
//----绘制直线
canvas.drawLine( 10 , 40 , 50 , 40 , paint );
//----绘制多条直线
canvas.drawLines( new float[]{ 10 , 50 , 50 , 50 , 70 , 50 , 110 , 50 } , paint );
//----绘制矩形
canvas.drawRect( 10 , 60 , 40 , 100 , paint );
//----绘制矩形2
Rect rect = new Rect( 10 , 100 , 60 , 130 );
canvas.drawRect( rect , paint );
//----绘制圆角矩形
RectF rectF = new RectF( 10 , 140 , 60 , 170 );
canvas.drawRoundRect( rectF , 20 , 20 , paint );
//----绘制圆形
canvas.drawCircle( 20 , 200 , 20 , paint );
//----绘制弧线
canvas.drawArc( new RectF( 150 , 20 , 200 , 70 ) , 10 , 230 , true , paint );
//----绘制椭圆
canvas.drawOval( new RectF( 150 , 80 , 180 , 100 ) , paint );
//----绘制指定路径图形
Path path = new Path();
//设置起点
path.moveTo( 160 , 150 );
path.lineTo( 200 , 150 );
path.lineTo( 180 , 200 );
//路径结束
path.close();
canvas.drawPath( path , paint );
//----绘制指定路径图形
Path pathCircle = new Path();
//添加一个圆形的路径
pathCircle.addCircle( 130 , 260 , 20 , Path.Direction.CCW );
//----绘制带圆形的路径文本
canvas.drawTextOnPath( "PathText" , pathCircle , 10 , 20 , paint );
}
}
catch( Exception e )
{
e.printStackTrace();
}
finally
{
if( canvas != null )
{
holder.unlockCanvasAndPost( canvas );
}
}
}
Paint 画笔
paint是绘画辅助类,其中包含文字与位图的样式、颜色等属性信息。
使用api如下:
try
{
canvas = holder.lockCanvas();
if( canvas != null )
{
canvas.drawColor( Color.WHITE );
//-----设置画笔无锯齿
Paint paint1 = new Paint();
canvas.drawCircle( 40 , 30 , 20 , paint1 );
paint1.setAntiAlias( true );
canvas.drawCircle( 100 , 30 , 20 , paint1 );
//-----设置画笔的透明度
canvas.drawText( "无透明度" , 100 , 70 , new Paint() );
Paint paint2 = new Paint();
paint2.setAlpha( 0x77 );
canvas.drawText( "半透明度" , 20 , 70 , paint2 );
//-----设置绘制文本的锚点
canvas.drawText( "锚点" , 20 , 90 , new Paint() );
Paint paint3 = new Paint();
//设置以文本的中心点绘制
paint3.setTextAlign( Paint.Align.CENTER );
canvas.drawText( "锚点" , 20 , 105 , paint3 );
//-----获取文本长度
Paint paint4 = new Paint();
float len = paint4.measureText( "文本宽度:" );
canvas.drawText( "文本长度:" + len , 20 , 130 , new Paint() );
//-----设置画笔样式
canvas.drawRect( new Rect( 20 , 140 , 40 , 160 ) , new Paint() );
Paint paint5 = new Paint();
//设置画笔不填充
paint5.setStyle( Paint.Style.STROKE );
canvas.drawRect( new Rect( 60 , 140 , 80 , 160 ) , paint5 );
//-----设置画笔颜色
Paint paint6 = new Paint();
paint6.setColor( Color.GRAY );
canvas.drawText( "灰色" , 30 , 180 , paint6 );
//-----设置画笔粗细程度
canvas.drawLine( 20 , 200 , 70 , 200 , new Paint() );
Paint paint7 = new Paint();
paint7.setStrokeWidth( 7 );
canvas.drawLine( 20 , 220 , 70 , 220 , paint7 );
//-----设置画笔绘制文本的字体粗细
Paint paint8 = new Paint();
paint8.setTextSize( 20 );
canvas.drawText( "文字尺寸" , 20 , 260 , paint8 );
//-----设置画笔的ARGB分量
Paint paint9 = new Paint();
paint9.setARGB( 0x77 , 0xff , 0x00 , 0x00 );
canvas.drawText( "红色半透明" , 20 , 290 , paint9 );
}
}
catch( Exception e )
{
e.printStackTrace();
}
finally
{
if( canvas != null )
{
holder.unlockCanvasAndPost( canvas );
}
}
165/406