android 自定义view 初识

来源:互联网 发布:对文化网络建设的看法 编辑:程序博客网 时间:2024/05/16 17:42

Android自定义View

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

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

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

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

package com.example.xuan.customview_demo0;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.util.AttributeSet;import android.view.View;/** *这个是自定义的TextView. * 至少需要重载构造方法和onDraw方法 * 对于自定义的View如果没有自己独特的属性,可以直接在xml文件中使用就可以了 * 如果含有自己独特的属性,那么就需要在构造函数中获取属性文件attrs.xml中自定义属性的名称 * 并根据需要设定默认值,放在在xml文件中没有定义。 * 如果使用自定义属性,那么在应用xml文件中需要加上新的schemas, * 比如这里是xmlns:my="http://schemas.android.com/apk/res/demo.view.my" * 其中xmlns后的“my”是自定义的属性的前缀,res后的是我们自定义View所在的包 * */public class MyView extends View {    Paint mPaint;//画笔,包含了几何图形,文本等的样式和颜色信息    public MyView(Context context) {        super(context);        initView(context,null);    }    public MyView(Context context, AttributeSet attrs) {        super(context, attrs);        initView(context,attrs);    }    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        initView(context,attrs);    }        private void initView(Context context, AttributeSet attrs) {        mPaint=new Paint();        //TypeArray是一个用来存放context.obtainStyleAttributes获得的属性的数组        //在使用完后一定要调用recycle方法        //属性的名称是sTyleable中的名称+""+属性名称        TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.MyView);        int textColor=array.getColor(R.styleable.MyView_textColor,0XFF00FF00);        float textSize=array.getDimension(R.styleable.MyView_textSize,36);        mPaint.setColor(textColor);        mPaint.setTextSize(textSize);        array.recycle();//一定要调用,否则这次的设定会对下次的使用造成影响    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        //Canvas中含有很多画图的接口,利用这些接口,可以画出我们想要的图形        mPaint.setStyle(Paint.Style.FILL);//设置填充        canvas.drawRect(10, 10, 100, 100, mPaint);        mPaint.setColor(Color.BLUE);        canvas.drawText("我是被画出来的",10,130,mPaint);        //定义一个矩形        RectF rf1=new RectF(10,160,100,230);        canvas.drawRect(rf1,mPaint);        //画弧顺时针        RectF rf2=new RectF(10,260,100,330);        canvas.drawArc(rf2,0,45,true,mPaint);        //画线        canvas.drawLine(150,150,250,250,mPaint);        //定义一个矩形        RectF rf3=new RectF(300,300,400,400);        //画圆        canvas.drawOval(rf3,mPaint);    }    @Override    public boolean isInEditMode() {       //return super.isInEditMode();       return  true;    }}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">   <com.example.xuan.customview_demo0.MyView       android:layout_width="match_parent"       android:layout_height="wrap_content"       app:textColor="#6cf"       app:textSize="20sp"       /></RelativeLayout>



0 0