关闭

《温故而知新》系列之自定义View基本知识

标签: Android
119人阅读 评论(0) 收藏 举报
分类:

 年末,工作闲暇之余写了这篇自定义View常用的基本知识,主要是为了巩固一下自己的基础。

提到自定义View,大家耳熟能详:自定义属性、onMeasure()、onDraw()。onMeaure比较简单,这里就不多介绍了。

自定义属性:attrs.xml文件  (参考format详解

1. reference:参考某一资源ID。

    (1)属性定义:

            <declare-styleable name = "名称">

                   <attr name = "background" format = "reference" />

            </declare-styleable>

     (2)属性使用:

             <ImageView

                     Android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:background = "@drawable/图片ID"

                     />

2. color:颜色值。

     (1)属性定义:

            <declare-styleable name = "名称">

                   <attr name = "textColor" format = "color" />

            </declare-styleable>

    (2)属性使用:

            <TextView

                     android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:textColor = "#00FF00"

                     />

3. boolean:布尔值。

    (1)属性定义:

            <declare-styleable name = "名称">

                   <attr name = "focusable" format = "boolean" />

            </declare-styleable>

     (2)属性使用:

            <Button

                    android:layout_width = "42dip"
                    android:layout_height = "42dip"

                    android:focusable = "true"

                    />

4. dimension:尺寸值。

    (1)属性定义:

            <declare-styleable name = "名称">

                   <attr name = "layout_width" format = "dimension" />

            </declare-styleable>

     (2)属性使用:

            <Button

                    android:layout_width = "42dip"
                    android:layout_height = "42dip"

                    />

5. float:浮点值。

    (1)属性定义:

            <declare-styleable name = "AlphaAnimation">

                   <attr name = "fromAlpha" format = "float" />
                   <attr name = "toAlpha" format = "float" />

            </declare-styleable>

     (2)属性使用:

            <alpha
                   android:fromAlpha = "1.0"
                   android:toAlpha = "0.7"

                   />

6. integer:整型值。

    (1)属性定义:

            <declare-styleable name = "AnimatedRotateDrawable">

                   <attr name = "visible" />
                   <attr name = "frameDuration" format="integer" />
                   <attr name = "framesCount" format="integer" />
                   <attr name = "pivotX" />
                   <attr name = "pivotY" />
                   <attr name = "drawable" />

            </declare-styleable>

     (2)属性使用:

            <animated-rotate

                   xmlns:android = "http://schemas.android.com/apk/res/android"  
                   android:drawable = "@drawable/图片ID"  
                   android:pivotX = "50%"  
                   android:pivotY = "50%"  
                   android:framesCount = "12"  
                   android:frameDuration = "100"

                   />

7. string:字符串。

    (1)属性定义:

            <declare-styleable name = "MapView">
                   <attr name = "apiKey" format = "string" />
            </declare-styleable>

     (2)属性使用:

            <com.google.android.maps.MapView
                    android:layout_width = "fill_parent"
                    android:layout_height = "fill_parent"
                    android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"

                    />

8. fraction:百分数。

    (1)属性定义:

            <declare-styleable name="RotateDrawable">
                   <attr name = "visible" />
                   <attr name = "fromDegrees" format = "float" />
                   <attr name = "toDegrees" format = "float" />
                   <attr name = "pivotX" format = "fraction" />
                   <attr name = "pivotY" format = "fraction" />
                   <attr name = "drawable" />
            </declare-styleable>

     (2)属性使用:

            <rotate

                   xmlns:android = "http://schemas.android.com/apk/res/android
               android:interpolator = "@anim/动画ID"

                   android:fromDegrees = "0" 
               android:toDegrees = "360"

                   android:pivotX = "200%"

                   android:pivotY = "300%
               android:duration = "5000"

                   android:repeatMode = "restart"

                   android:repeatCount = "infinite"

                   />

9. enum:枚举值。

    (1)属性定义:

            <declare-styleable name="名称">
                   <attr name="orientation">
                          <enum name="horizontal" value="0" />
                          <enum name="vertical" value="1" />
                   </attr>            

            </declare-styleable>

     (2)属性使用:

            <LinearLayout

                    xmlns:android = "http://schemas.android.com/apk/res/android"
                    android:orientation = "vertical"
                    android:layout_width = "fill_parent"
                    android:layout_height = "fill_parent"
                    >
            </LinearLayout>

10. flag:位或运算。

     (1)属性定义:

             <declare-styleable name="名称">
                    <attr name="windowSoftInputMode">
                            <flag name = "stateUnspecified" value = "0" />
                            <flag name = "stateUnchanged" value = "1" />
                            <flag name = "stateHidden" value = "2" />
                            <flag name = "stateAlwaysHidden" value = "3" />
                            <flag name = "stateVisible" value = "4" />
                            <flag name = "stateAlwaysVisible" value = "5" />
                            <flag name = "adjustUnspecified" value = "0x00" />
                            <flag name = "adjustResize" value = "0x10" />
                            <flag name = "adjustPan" value = "0x20" />
                            <flag name = "adjustNothing" value = "0x30" />
                     </attr>         

             </declare-styleable>

     (2)属性使用:

            <activity

                   android:name = ".StyleAndThemeActivity"
                   android:label = "@string/app_name"
                   android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
                   <intent-filter>
                          <action android:name = "android.intent.action.MAIN" />
                          <category android:name = "android.intent.category.LAUNCHER" />
                   </intent-filter>
             </activity>

      注意:

      属性定义时可以指定多种类型值。

    (1)属性定义:

            <declare-styleable name = "名称">

                   <attr name = "background" format = "reference|color" />

            </declare-styleable>

     (2)属性使用:

             <ImageView

                     android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:background = "@drawable/图片ID|#00FF00"

                     />

onDraw():

  在onDraw中一定要谨记尽可能的不要new 对象。onDraw中主要元素是Canvas,在Canvas上需要一个Paint,画笔的常用方法有哪些呢?

Paint:

Paint paint=new Paint();

//STROKE:描边 FILL_AND_STROKE:描边并填充 FILL:填充

paint.setStyle(Style style);

//设置抗锯齿,如果不设置,加载位图的时候可能会出现锯齿状的边界,如果设置,边界就会变的稍微有点模糊,锯齿就看不到了。

paint.setAntiAlias(boolean aa)

//设置是否抖动,如果不设置感觉就会有一些僵硬的线条,如果设置图像就会看的更柔和一些

paint.setDither(boolean dither)

//设置画笔颜色

paint.setColor(int color)

//设置画笔的透明度[0-255],0是完全透明,255是完全不透明

paint.setAlpha(int a)

//画笔样式为空心时,设置空心画笔的宽度

paint.setStrokeWidth(float width)

//设置阴影

paint.setShadowLayer(float radius, float dx, float dy, int shadowColor)

//笔触(笔头)风格 ROUND、SQUARE、BUTT

paint.setStrokeCap(Paint.Cap cap)

//接合处的形态
paint.setStrokeJoin(Paint.Join join)
  
//设置图形重叠时的显示方式
paint.setXfermode(Xfermode xfermode)
//测量字符串的长度  
paint.MeasureText("Hello World"); 
下面来说说Canvas:
线:绘制一条从0,0到100,100的线
canvas.drawLine(0,0,100,100,paint)

多边形及简单图形:使用path类去实现
Path path=new Path();
path.MoveTo(0,0);//给定path的起点  
path.LineTo(10,10);//往10,10绘制一条路径  
path.LineTo(1,2);//继续从10,10往1,2绘制一条路径  
path.close;//将绘制的线形成封闭空间  
canvas.drawPath(path,paint); 
矩形:画一个矩形,左上角的坐标为0,0   右下角的坐标为100,50 
canvas.drawRect(0,0,100,50,paint);  

圆角矩形: //画一个圆角矩形,大小为rect,30,30分别表示左边圆角的半径和右边圆角的半径  
RectF rect = new RectF(0,0,100,50);  
canvas.drawRoundRect(rect,20,20,paint);

圆形:圆心为50,50  半径为100  
canvas.drawCircle(50,50,100,paint); 
弧形:弧所在矩形为rectF  从270°开始,画90° 不经过圆心,0°为三点钟位置 
RectF rect = new RectF(0,0,100,50);  
canvas.drawArc(rect,270,90,false,p); 
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4919次
    • 积分:170
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条
    文章分类