Android自定义View(一)
2015-05-15 08:52
260 查看
笔者刚刚搞Android开发的时候对自定义View是比较恐惧,那时通常都是去网上找别人的资源修改,当要想成为高手就必须学会自定义View,所以决定花点时间边学变分享。先总结下自定义View的步骤:
1、自定义View的属性
2、在View的构造方法中获得我们自定义的属性
3、[ 重写onMesure ]----计算显示的高和宽
4、重写onDraw
----绘制View
(1) 自定义View的属性,首先在res/values/ 下建立一个MyView.xml , 在里面定义我们的属性和声明我们的整个样式。
格式有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;
2、在View的构造方法中,获得我们的自定义的样式并初始化 Paint
3、我们重写onDraw,onMesure方法:
4、使用自定义View
(1)在布局中添加自定义view
* 使用自定义属性,那么在应用xml文件中需要加上新的schemas,
* 比如这里是 xmlns:custom="http://schemas.android.com/apk/res/com.example.myview"
* 其中xmlns后的“custom”是自定义的属性的前缀,res后的是我们自定义View所在的包
附上效果图:
1、自定义View的属性
2、在View的构造方法中获得我们自定义的属性
3、[ 重写onMesure ]----计算显示的高和宽
4、重写onDraw
----绘制View
(1) 自定义View的属性,首先在res/values/ 下建立一个MyView.xml , 在里面定义我们的属性和声明我们的整个样式。
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyView"> <attr name="textColor" format="color"/> <attr name="textSize" format="dimension"/> </declare-styleable> </resources>我们定义了字体颜色,字体大小2个属性,format是值该属性的取值格式。
格式有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;
2、在View的构造方法中,获得我们的自定义的样式并初始化 Paint
private Paint mPaint; //画笔,包含了画几何图形、文本等的样式和颜色信息 private float textSize; private int textColor; public MyView(Context context) { super(context); } public MyView(Context context, AttributeSet attrs){ super(context, attrs); mPaint = new Paint(); //TypedArray是一个用来存放由context.obtainStyledAttributes获得的属性的数组 //在使用完成后,一定要调用recycle方法 //属性的名称是styleable中的名称+“_”+属性名称 TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView); textColor = array.getColor(R.styleable.MyView_textColor, 0XFF00FF00); //提供默认值,放置未指定 textSize = array.getDimension(R.styleable.MyView_textSize, 36); mPaint.setColor(textColor); mPaint.setTextSize(textSize); array.recycle(); //一定要调用,否则这次的设定会对下次的使用造成影响 }
3、我们重写onDraw,onMesure方法:
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO 自动生成的方法存根 super.onMeasure(widthMeasureSpec, heightMeasureSpec); } public void onDraw(Canvas canvas){ super.onDraw(canvas); //Canvas中含有很多画图的接口,利用这些接口,我们可以画出我们想要的图形 //mPaint = new Paint(); //mPaint.setColor(Color.RED); mPaint.setStyle(Style.FILL); //设置填充 canvas.drawRect(10, 10, 100, 100, mPaint); //绘制矩形 mPaint.setColor(textColor); canvas.drawText("我是被画出来的", 10, 100+textSize, mPaint); }这里重新onMesure方法并未做什么操作
4、使用自定义View
(1)在布局中添加自定义view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:custom="http://schemas.android.com/apk/res/com.example.myview" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.example.myview.MyView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" custom:textColor="#0099cc" custom:textSize="16dp" android:text="@string/hello_world" /> </RelativeLayout>
* 使用自定义属性,那么在应用xml文件中需要加上新的schemas,
* 比如这里是 xmlns:custom="http://schemas.android.com/apk/res/com.example.myview"
* 其中xmlns后的“custom”是自定义的属性的前缀,res后的是我们自定义View所在的包
附上效果图:
相关文章推荐
- 【android学习】自定义view属性
- android 自定义View
- android 自定义 view
- Android 自定义View (四) 视频音量调控
- Android 手把手教您自定义ViewGroup(二) 实战篇 -> 实现FlowLayout
- Android自定义View(基础篇),系统的学习
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
- android 自定义view
- Android自定义View绘制真正的居中文本
- android之自定义view 二
- Android自定义垂直方向的ViewPager
- android开发自定义View(一)
- Android 自定义Textview实现文字两端对齐功能和长按自由选择文字弹出自定义ActionMenu功能(二)自定义ActionMenu
- android 自定义Viewpager实现无限循环
- Android自定义View实现左右滑动选择出生年份
- Android 自定义View实现照片裁剪框与照片裁剪
- android--笔记--自定义View绘制圆环
- Android开发-自定义View-AndroidStudio(十五)快速索引(2)
- Android 自定义View学习之文字绘制
- Android 自定义圆形Imageview