您的位置:首页 > 移动开发 > Android开发

Android自定义View进阶《一》

2016-08-10 14:15 323 查看
楼主第一次写博客,写的不好请不要喷。本人自定义view在学校学的不是很好,所以花点时间学习一下。写个博客记录下来,哈哈。好了,废话不多说。

1.在xml布局加上你自定义的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zs.androidview.MainActivity" >
<com.zs.view.MyView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


2、就是你的自定义view的文件了 继承View 同时注意必须要实现里面的几个构造方法

public MyView(Context context) {
// TODO Auto-generated constructor stub
this(context,null);
}

public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}

public MyView(Context context, AttributeSet attrs) {
this(context,attrs,0);
}


3、就是在onDraw中绘制了楼主就只是画了个圆

protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint p=new Paint();//初始化画笔
p.setColor(Color.YELLOW);//给画笔设置颜色
////        cx:圆心的x坐标。
////        cy:圆心的y坐标。
////        radius:圆的半径。
////        paint:绘制时所使用的画笔。
//
canvas.drawCircle(150, 150, 100, p);//画圆
}


重写onMesure 这个方法可写可不写 不过一般大部分都还是写的。

好了,第一篇就写到这里了,毕竟是第一次,请大家勿怪。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 自定义View
相关文章推荐