android textpaint中使用paint怎么画虚线

首先介绍Paint和Canvas类的用法:
&&&& Paint:就是一个画笔,使用之前首先要调整好画笔,然后就可以在画布上绘图了,这样就可以显示在手机屏幕上。
&&&& 主要方法有:setColor() 设置画笔的颜色
&&&&&&&&&&&&&&&& setTextSize() 设置字体大小
&&&&&&&&&&&&&&&& setStyle() 设置画笔的风格,空心还是实心
&&&&&&&&&&&&&&&& setStrokWidth() 设置空心的边框宽度
&&&&&&&&&&&&&&&& setTextAlign() 设置文字的对齐方式
&&&&&&&&&&&&&&&& setTypeface() 设置字体,如粗细、倾斜
&&&& 在设置画笔颜色的时候,使用到了Color类,这个类定义了一些颜色常量和颜色转换。如Color.RED、Color.GRENN等,还可以通过Color类的静态方法rgb(int,int,int)
来定一个颜色,这三个参数的的值范围是0~255。
&&&& Canvas:是一个画布,可以在上面画我们想要的任何东西,我们也可以设置画布的一些的属性,比如背景颜色,尺寸等。Canvas提供了一下一些方法:
&&&& 方法:Canvas(),创建一个空的画布,可以使用setBitmap()方法来设置绘制的具体画布;
&&&&&&&&&&&&& Canvas(Bitmap bitmap),以bitmap对象创建一个画布,此时则将内容绘制在bitmap上,bitmap不得为null.
&&&&&&&&&&&&& drawColor(),设置画布的背景颜色。
&&&&&&&&&&&&& drawRect(left,top,right,bottom,paint);画矩形,前四个是float,后一个是Paint类型。
&&&&&&&&&&&&& drawLine(startX,startY,stopX,stopY,paint),画线,前四个参数是float,后一个是Paint类型。
&&&&&&&&&&&&& drawText(text,x,y,paint);在画布上画指定的文本,x,y两个参数是float,后一个是Paint类型。
*********************************看下面的代码****************************************
/*空矩形*/
&& Paint paintRect = new Paint(); && paintRect.setColor(Color.rgb(79, 129, 189)); && paintRect.setStrokeWidth(2);
&& paintRect.setStyle(Style.STROKE); && canvas.drawRect(20.0,20.0,100.0,100.0, paintRect);
/*实矩形*/
&& Paint paintRect = new Paint();&&
&& paintRect.setColor(Color.rgb(79, 129, 189));&&
&& paintRect.setStrokeWidth(2);
&& paintRect.setStyle(Style.STROKE);&&
&& canvas.drawRect(20.0,20.0,100.0,100.0, paintRect);
/*画直线*/
&& Paint paintRect = new Paint();&
&& paintRect.setColor(Color.rgb(79, 129, 189));&
&& paintRect.setStrokeWidth(1);
&& paintRect.setStyle(Style.STROKE);
&& canvas.drawLine(10.0,80.0,100.0,80.0, paintRect);
&& Paint paint = new Paint(); && paint.setColor(Color.rgb(79, 129, 189)); && paint.setStyle(Style.STROKE); && paint.setTextAlign(Align.CENTER);//字水平居中
& // FontMetrics fontMetrics = paint.getFontMetrics();计算字的高度
&& canvas.drawText(text ,20.0,40.0, paint);
***********画表格******************
public class TableView extends View { &&& // 表格的行数和列数 &&& private int row, &&& // 表格定位的左上角X和右上角Y &&& private final static int STARTX = 25; &&& private final static int STARTY = 25; &&& // 表格的宽度 &&& private static float gridW &&& private static float gridH
&&& public TableView(Context context, int row, int col) {    super(context);    this.row =    this.col = &   if (this.row == 0 || this.col == 0) { &    assert false : "行数和列数为0,不符合";    } &   this.setFocusable(true);    this.setFocusableInTouchMode(true);   }
&& @Override && protected void onSizeChanged(int w, int h, int oldw, int oldh) {    gridWidth = (w - 50) / (this.col * 1.0f);    if (this.row & this.col) { &&   gridHeight = (h - 100) / (this.row * 1.0f);    } else { &    gridHeight = gridW    }    super.onSizeChanged(w, h, oldw, oldh); &&& }
&& @Override && protected void onDraw(Canvas canvas) { &   super.onDraw(canvas);    // 填充表格颜色    Paint paintColor = new Paint(); &   paintColor.setStyle(Style.FILL); &   paintColor.setColor(Color.rgb(235, 241, 221));    canvas.drawRect(STARTX, STARTY, STARTX + gridWidth * this.col, STARTY &&&   + gridHeight * this.row, paintColor); &   paintColor.setColor(Color.rgb(219, 238, 243));    for (int i = 0; i & this. i++) { &      if ((i + 1) % 2 == 1) { &&        canvas.drawRect(STARTX, i * gridHeight + STARTY, STARTX &&&&        + this.col * gridWidth, STARTY + (i + 1) * gridHeight, &&&&&       paintColor); &&     } &&  && }
&&&&&&& // 画表格最外层边框    Paint paintRect = new Paint();    paintRect.setColor(Color.rgb(79, 129, 189));   & paintRect.setStrokeWidth(2);   & paintRect.setStyle(Style.STROKE);   & canvas.drawRect(STARTX, STARTY, STARTX + gridWidth * this.col, STARTY &   & + gridHeight * this.row, paintRect);    // 画表格的行和列,先画行后画列 &   paintRect.setStrokeWidth(1);   & for (int i = 0; i & this.row - 1; i++) {       canvas.drawLine(STARTX, STARTY + (i + 1) * gridHeight, STARTX &&&      + this.col * gridWidth, STARTY + (i + 1) * gridHeight, &&&      paintRect);    }    for (int j = 0; j & this.col - 1; j++) { &&     canvas.drawLine(STARTX + (j + 1) * gridWidth, STARTY, STARTX &&&      + (j + 1) * gridWidth, STARTY + this.row * gridHeight, &&     & paintRect); &   }
  // 在单元格填充数字—如果行数大于60并且列数大于30,就不显示数字;大于10,就改变字大小   & if (this.row &= 50 && this.col &= 30) {     & Paint paint = new Paint(); &      paint.setColor(Color.rgb(79, 129, 189)); &      paint.setStyle(Style.STROKE); &&     paint.setTextAlign(Align.CENTER); &    if (this.row & 40 || this.col & 25) { &&      paint.setTextSize(7); &&   } else if (this.row & 30 || this.col & 20) { &&&     paint.setTextSize(8); &&   } else if (this.row & 20 || this.col & 15) { &&&     paint.setTextSize(9); &    } else if (this.row & 10 || this.col & 10) { &&&     paint.setTextSize(10); &&   }
  FontMetrics fontMetrics = paint.getFontMetrics();  & float fontHeight = fontMetrics.bottom - fontMetrics. &   int text = 0; &&& for (int i = 0; i & this. i++) { &&&&   for (int j = 0; j & this. j++) { &&&      float mLeft = j * gridWidth + STARTX; &&&&     float mTop = i * gridHeight + STARTY; &&     & float mRight = mLeft + gridW &&&      text++; &&&      float textBaseY = (int) (gridHeight + fontHeight) && 1; &&&&     canvas.drawText(text + "", (int) (mLeft + mRight) && 1, &&&&&      textBaseY + mTop, paint); &&    } &&   } & } } }
-----------------------------------------------------------------------------------------------------------------
在写以上代码的时候,出现了代码执行效率的问题。比如上面代码中,除法运算如果不使用移位运算而使用BigDecimal大数字运算,那么生成表格的速度就非常的慢,
而使用移位运算速度就快了好几倍。所以在写代码的时候,不仅仅是代码写出来了达到这个效果就可以了,还要考虑效率问题,让我们写的代码在执行的时候,用的时间最短。
& 开源中国(OSChina.NET) |
开源中国社区(OSChina.net)是工信部
指定的官方社区Android 画图设置paint的颜色
---- 求助
[问题点数:20分,结帖人WOSHICAIXIANFENG]
Android 画图设置paint的颜色
---- 求助
[问题点数:20分,结帖人WOSHICAIXIANFENG]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
本帖子已过去太久远了,不再提供回复功能。Android Paint Canvar 画几何图形
最近在研究自定义控件,遇到了几何图形的绘制,这里就贴出来常见的例子:
①首先在主Activity中代码:
package com.example.
import android.app.A
import android.content.C
import android.graphics.C
import android.graphics.C
import android.graphics.P
import android.graphics.RectF;
import android.os.B
import android.util.AttributeS
import android.view.V
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyCustomView myView = new MyCustomView(MainActivity.this);
setContentView(myView);
②MyCustomView中代码:
package com.example.
import android.content.C
import android.graphics.C
import android.graphics.C
import android.graphics.P
import android.graphics.R
import android.graphics.RectF;
import android.util.AttributeS
import android.view.V
public class MyCustomView extends View{
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
public MyCustomView(Context context) {
super(context);
// TODO Auto-generated constructor stub
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//设置画布的颜色
canvas.drawColor(Color.BLUE);
//-----------------------------①画一个空心正方形--------------------------------
//创建一个画笔
Paint mPaint = new Paint();
//设置画笔为空心
mPaint.setStyle(Paint.Style.STROKE);
//设置画颜色
mPaint.setColor(Color.RED);
//设置画笔宽度
mPaint.setStrokeWidth(10);
* 创建一个矩形
* 第一个参数:矩形左顶点的X轴坐标
* 第二个参数:矩形左顶点的Y轴坐标
* 第三个参数:矩形右下角顶点的X轴坐标
* 第四个参数:矩形右下角顶点的Y轴坐标
Rect mRect = new Rect(10, 10, 50, 50);
//开始画矩形了
canvas.drawRect(mRect, mPaint);
//-----------------------------②画一个实心正方形--------------------------------
//设置画笔为实心
mPaint.setStyle(Paint.Style.FILL);
//重新设置矩形的大小
mRect.set(60, 60, 100, 100);
canvas.drawRect(mRect, mPaint);
//-----------------------------③画一个空心长方形--------------------------------
//设置画笔的宽度
mPaint.setStrokeWidth(2);
mPaint.setStyle(Paint.Style.STROKE);
mRect.set(70, 110, 140, 140);
canvas.drawRect(mRect, mPaint);
//-----------------------------④画一个空心圆--------------------------------
//消除锯齿
mPaint.setAntiAlias(true);
* 画一个空心圆
* 第一个参数:圆心的X轴坐标
* 第二个参数:圆心的Y轴坐标
* 第三个参数:圆的半径
* 第四个参数:画笔
canvas.drawCircle(180, 180, 20, mPaint);
//-----------------------------⑤画一个实心圆--------------------------------
mPaint.setStyle(Paint.Style.FILL);
canvas.drawCircle(230, 220, 20, mPaint);
//-----------------------------⑥画一段实心扇形--------------------------------
//先画一个矩形
RectF oval = new RectF(240, 240, 300, 300);
* 画一段实心扇形
* 第一个参数:一个矩形,用户定义扇形的大小
* 第二个参数:画扇形的起始角度
* 第三个参数:要画的角度
* 第四个参数是否包括圆形(true为包括,一般用于画扇形;false为不包括,一般用于画弧形)
* 第四个参数:画笔
canvas.drawArc(oval, 0, 40, true, mPaint);
//-----------------------------⑦画一段空心扇形--------------------------------
mPaint.setStyle(Paint.Style.STROKE);
oval.set(290, 290, 350, 350);
canvas.drawArc(oval, 0, 60, true,mPaint);
//-----------------------------⑧画一段弧--------------------------------
oval.set(360, 360, 400, 400);
canvas.drawArc(oval, 0, 70, false, mPaint);
//-----------------------------⑨画一个内切圆--------------------------------
oval.set(360, 360, 400,400);
canvas.drawRect(oval, mPaint);
canvas.drawArc(oval, 0, 360, false, mPaint);
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'1099人阅读
android(79)
很多时候,画虚线都是使用美工切图(一个实点,一个虚点),然后使用Bitmap的repeat属性
&?xml version=&1.0& encoding=&utf-8&?&
&bitmap xmlns:android=&/apk/res/android&
android:src=&@drawable/repeat_bg&
android:tileMode=&repeat& /&
下面我们来画一条虚线,首先定义一个 dashedline类,继承于View,重写Ondraw()方法
public class DashedLine extends View {
private final String namespace = &http://www./&;
private float startX;
private float startY;
private float endX;
private float endY;
private Rect mR
public DashedLine(Context context, AttributeSet attrs) {
super(context, attrs);
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);//空心
paint.setColor(Color.DKGRAY);
Path path = new Path();
//通过moveto,lineto的x,y坐标确定虚线实横,纵,还是倾斜
path.moveTo(0, 10);//Set the beginning of the next contour to the point (x,y)
path.lineTo(480, 10);//Add a line from the last point to the specified point (x,y).
//DashPathEffect
可以使用DashPathEffect来创建一个虚线的轮廓(短横线/小圆点),而不是使用实线
//float[] { 5, 5, 5, 5 }值控制虚线间距,密度
PathEffect effects = new DashPathEffect(new float[] { 5, 5, 5, 5 }, 1);
paint.setPathEffect(effects);
canvas.drawPath(path, paint);
定义完View,就可以在XML中使用了
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:74600次
积分:2002
积分:2002
排名:第13639名
原创:123篇
转载:13篇
评论:39条
(18)(4)(6)(1)(2)(1)(2)(1)(2)(2)(1)(1)(3)(1)(5)(2)(7)(7)(11)(7)(6)(11)(3)(9)(7)(6)(12)只需一步,快速开始
只需一步, 快速开始
后使用快捷导航没有帐号?
总共766条微博动态微博:
查看: 219152|回复: 13
积分威望金钱
package com.
import android.content.C
import android.graphics.C
import android.graphics.C
import android.graphics.DashPathE
import android.graphics.P
import android.graphics.P
import android.graphics.PathE
import android.graphics.R
import android.util.AttributeS
import android.util.DisplayM
import android.util.L
import android.view.V
* 画虚线组件
* @author linweidong
* @Date& &
*/
public class DashedLine extends View {
& & private final String namespace = &http://com.smart.driverbook&;
& & private float startX;
& & private float startY;
& & private float endX;
& & private float endY;
& & private Rect mR
&&
& & public DashedLine(Context context, AttributeSet attrs) {
& && &&&super(context, attrs);& && && &
& && &&&
& & }
& & @Override
& & protected void onDraw(Canvas canvas) {
& && &&&// TODO Auto-generated method stub
& && &&&super.onDraw(canvas);& && &&&
& && &&&Paint paint = new Paint();
& && &&&paint.setStyle(Paint.Style.STROKE);
& && &&&paint.setColor(Color.DKGRAY);
& && &&&Path path = new Path();& &&&
& && &&&path.moveTo(0, 10);
& && &&&path.lineTo(480,10);& && &
& && &&&PathEffect effects = new DashPathEffect(new float[]{5,5,5,5},1);
& && &&&paint.setPathEffect(effects);
& && &&&canvas.drawPath(path, paint);
& & }
}复制代码在layout文件夹下的xml引用这个控件
&com.custom.DashedLine
& && &&&android:id=&@+id/dashedLine&
& && &&&android:layout_width=&wrap_content&& && && &
& && &&&android:layout_height=&20px&& && && && &&&
& && && && && && && &&&
& && && &/&复制代码
今天,我又来了
积分威望金钱
我只是路过,不发表意见
积分威望金钱
高级会员, 积分 2876, 距离下一级还需 124 积分
高级会员, 积分 2876, 距离下一级还需 124 积分
非常好,顶一下
积分威望金钱
高级会员, 积分 2963, 距离下一级还需 37 积分
高级会员, 积分 2963, 距离下一级还需 37 积分
楼下的接上
积分威望金钱
高级会员, 积分 1674, 距离下一级还需 1326 积分
高级会员, 积分 1674, 距离下一级还需 1326 积分
围观 围观 沙发在哪里!!!
积分威望金钱
发发呆,回回帖,工作结束~
积分威望金钱
支持你哈...................................
积分威望金钱
高级会员, 积分 1791, 距离下一级还需 1209 积分
高级会员, 积分 1791, 距离下一级还需 1209 积分
好帖必须得顶起
积分威望金钱
高级会员, 积分 1664, 距离下一级还需 1336 积分
高级会员, 积分 1664, 距离下一级还需 1336 积分
顶起顶起顶起
积分威望金钱
高级会员, 积分 2047, 距离下一级还需 953 积分
高级会员, 积分 2047, 距离下一级还需 953 积分
我也顶起出售广告位
社区QQ达人
使用QQ帐号登录论坛的用户
经常参与各类话题的讨论,发帖内容较有主见
长期对论坛的繁荣而不断努力,或多次提出建设性意见
活跃且尽责职守的版主
曾经为论坛做出突出贡献目前已离职的版主
为论坛做出突出贡献的会员
经常帮助其他会员答疑
积极宣传本站,为本站带来更多注册会员
积极宣传本站,为本站带来更多的用户访问量
Powered by

我要回帖

更多关于 android paint详解 的文章

 

随机推荐