http://blog.csdn.net/duguang77/article/details/19543693

http://blog.csdn.net/duguang77/article/details/19543693

http://blog.csdn.net/duguang77/article/details/19543693

http://blog.csdn.net/duguang77/article/details/19543693



Android自定义View之一:初探实例

分类:Android 946人阅读 评论(2) 收藏 举报 安卓自定义控件 android自定义控件 自定义View

Android自定义View实现很简单

继承View,重写构造函数、onDraw,(onMeasure)等函数。

如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性。

在使用到自定义View的xml布局文件中需要加入xmlns:前缀="http://schemas.android.com/apk/res/你的自定义View所在的包路径".

在使用自定义属性的时候,使用前缀:属性名,如my:textColor="#FFFFFFF"。

实例:

view plain copy to clipboard print ?
  1. packagedemo.view.my;
  2. importandroid.content.Context;
  3. importandroid.content.res.TypedArray;
  4. importandroid.graphics.Canvas;
  5. importandroid.graphics.Color;
  6. importandroid.graphics.Paint;
  7. importandroid.graphics.Paint.Style;
  8. importandroid.util.AttributeSet;
  9. importandroid.view.View;
  10. /**
  11. *这个是自定义的TextView.
  12. *至少需要重载构造方法和onDraw方法
  13. *对于自定义的View如果没有自己独特的属性,可以直接在xml文件中使用就可以了
  14. *如果含有自己独特的属性,那么就需要在构造函数中获取属性文件attrs.xml中自定义属性的名称
  15. *并根据需要设定默认值,放在在xml文件中没有定义。
  16. *如果使用自定义属性,那么在应用xml文件中需要加上新的schemas,
  17. *比如这里是xmlns:my="http://schemas.android.com/apk/res/demo.view.my"
  18. *其中xmlns后的“my”是自定义的属性的前缀,res后的是我们自定义View所在的包
  19. *@authorAdministrator
  20. *
  21. */
  22. publicclassMyViewextendsView{
  23. PaintmPaint;//画笔,包含了画几何图形、文本等的样式和颜色信息
  24. publicMyView(Contextcontext){
  25. super(context);
  26. }
  27. publicMyView(Contextcontext,AttributeSetattrs){
  28. super(context,attrs);
  29. mPaint=newPaint();
  30. //TypedArray是一个用来存放由context.obtainStyledAttributes获得的属性的数组
  31. //在使用完成后,一定要调用recycle方法
  32. //属性的名称是styleable中的名称+“_”+属性名称
  33. TypedArrayarray=context.obtainStyledAttributes(attrs,R.styleable.MyView);
  34. inttextColor=array.getColor(R.styleable.MyView_textColor,0XFF00FF00);//提供默认值,放置未指定
  35. floattextSize=array.getDimension(R.styleable.MyView_textSize,36);
  36. mPaint.setColor(textColor);
  37. mPaint.setTextSize(textSize);
  38. array.recycle();//一定要调用,否则这次的设定会对下次的使用造成影响
  39. }
  40. publicvoidonDraw(Canvascanvas){
  41. super.onDraw(canvas);
  42. //Canvas中含有很多画图的接口,利用这些接口,我们可以画出我们想要的图形
  43. //mPaint=newPaint();
  44. //mPaint.setColor(Color.RED);
  45. mPaint.setStyle(Style.FILL);//设置填充
  46. canvas.drawRect(10,10,100,100,mPaint);//绘制矩形
  47. mPaint.setColor(Color.BLUE);
  48. canvas.drawText("我是被画出来的",10,120,mPaint);
  49. }
  50. }

相应的属性文件:

view plain copy to clipboard print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <declare-styleablename="MyView">
  4. <attrname="textColor"format="color"/>
  5. <attrname="textSize"format="dimension"/>
  6. </declare-styleable>
  7. </resources>

在布局文件中的使用:

view plain copy to clipboard print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:my="http://schemas.android.com/apk/res/demo.view.my"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. >
  8. <demo.view.my.MyView
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. my:textColor="#FFFFFFFF"
  12. my:textSize="22dp"
  13. />
  14. </LinearLayout>

//定义一个矩形

RectF rf1 = new RectF(10, 130, 110, 230);


//画弧顺时针

canvas.drawArc(rf1, 0, 45, true, paint);


//画线

canvas.drawLine(150, 10, 250, 110, paint);


//定义一个矩形

RectF rf2 = new RectF(150, 130, 250, 230);


//画圆

canvas.drawOval(rf2, paint);





Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形)

分类:Android 319人阅读 评论(0) 收藏 举报 android自定义控件 安卓自定义控件

目录(?)[+]

1、首先说一下canvas类:

Class Overview

The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).

这个类相当于一个画布,你可以在里面画很多东西;

我们可以把这个Canvas理解成系统提供给我们的一块内存区域(但实际上它只是一套画图的API,真正的内存是下面的Bitmap),而且它还提供了一整套对这个内存区域进行操作的方法,所有的这些操作都是画图API。也就是说在这种方式下我们已经能一笔一划或者使用Graphic来画我们所需要的东西了,要画什么要显示什么都由我们自己控制。

这种方式根据环境还分为两种:一种就是使用普通View的canvas画图,还有一种就是使用专门的SurfaceView的canvas来画图。两种的主要是区别就是可以在SurfaceView中定义一个专门的线程来完成画图工作,应用程序不需要等待View的刷图,提高性能。前面一种适合处理量比较小,帧率比较小的动画,比如说象棋游戏之类的;而后一种主要用在游戏,高品质动画方面的画图。

下面是Canvas类常用的方法:

drawRect(RectF rect, Paint paint) //绘制区域,参数一为RectF一个区域

drawPath(Path path, Paint paint) //绘制一个路径,参数一为Path路径对象

drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)//贴图,参数一就是我们常规的Bitmap对象,参数二是源区域(这里是bitmap),参数三是目标区域(应该在canvas的位置和大小),参数四是Paint画刷对象,因为用到了缩放和拉伸的可能,当原始Rect不等于目标Rect时性能将会有大幅损失。

drawLine(float startX, float startY, float stopX, float stopY, Paintpaint) //画线,参数一起始点的x轴位置,参数二起始点的y轴位置,参数三终点的x轴水平位置,参数四y轴垂直位置,最后一个参数为Paint 画刷对象。

drawPoint(float x, float y, Paint paint) //画点,参数一水平x轴,参数二垂直y轴,第三个参数为Paint对象。

drawText(String text, float x, floaty, Paint paint)//渲染文本,Canvas类除了上面的还可以描绘文字,参数一是String类型的文本,参数二x轴,参数三y轴,参数四是Paint对象。

drawOval(RectFoval,Paintpaint)//画椭圆,参数一是扫描区域,参数二为paint对象;

drawCircle(float cx, float cy, float radius,Paintpaint)// 绘制圆,参数一是中心点的x轴,参数二是中心点的y轴,参数三是半径,参数四是paint对象;

drawArc(RectFoval, float startAngle, float sweepAngle, boolean useCenter,Paintpaint)//画弧,

参数一是RectF对象,一个矩形区域椭圆形的界限用于定义在形状、大小、电弧,参数二是起始角(度)在电弧的开始,

参数三扫描角(度)开始顺时针测量的,参数四是如果这是真的话,包括椭圆中心的电弧,并关闭它,如果它是假这将是一个弧线,参数五是Paint对象;

还要理解一个paint类:

Class Overview

The Paint class holds the style and color information about how to draw geometries, text and bitmaps.

paint类拥有风格和颜色信息如何绘制几何学,文本和位图。

Paint 代表了Canvas上的画笔、画刷、颜料等等;

Paint类常用方法:

setARGB(int a, int r, int g, int b) // 设置 Paint对象颜色,参数一为alpha透明值

setAlpha(int a) // 设置alpha不透明度,范围为0~255

setAntiAlias(boolean aa) // 是否抗锯齿

setColor(int color)// 设置颜色,这里Android内部定义的有Color类包含了一些常见颜色定义

setTextScaleX(float scaleX)// 设置文本缩放倍数,1.0f为原始

setTextSize(float textSize)// 设置字体大小

setUnderlineText(booleanunderlineText)// 设置下划线



更多相关文章

  1. Android启动过程的Zygote进程
  2. Android(安卓)RectF类的构造函数参数说明
  3. Android(安卓)- View的绘制流程一(measure)
  4. EditText属性详解
  5. 小记初学android过程中遇到的小问题(android 4.4)
  6. android:layout_weight的真实含义
  7. android中文api(89)――ViewManager
  8. mybatisplus的坑 insert标签insert into select无参数问题的解决
  9. Python技巧匿名函数、回调函数和高阶函数

随机推荐

  1. 如何在Android中实现全屏,去掉标题栏效果
  2. 从Android到React Native开发(四、打包流
  3. android分层学习笔记(三)
  4. View视图框架源码分析之一:android是如何
  5. android 增加物理按键关闭wifi实例讲解
  6. 多个Android客户端同步服务器端表中数据
  7. Android的消息机制——Handler的工作过程
  8. android 消息传递机制
  9. 如何进行android开发
  10. Android(安卓)Activity加载Fragment的一