Android重写View并且自定义属性(二)
16lz
2021-01-26
通过自定义属性 实现如下效果:
第一步:在res\values的目录下新建一个文件attrs.xml
声明一些自定义属性
<?xmlversion="1.0"encoding="utf-8"?><resources><declare-styleablename="CustomViewStyle"><attrname="customText"format="string"/><attrname="customTextColor"format="color"/><attrname="customTextSize"format="dimension"/></declare-styleable></resources>
第二步:在layout目录下新建布局文件activity_main.xml
特别注意要在外层控件加上这个声明:
格式:xmlns:(你自定义名称)="http://schemas.android.com/apk/(你应用的包名)"
xmlns:xr="http://schemas.android.com/apk/res/com.rong.test"
或者
xmlns:xr="http://schemas.android.com/apk/res-auto"
推荐使用第二种
在布局文件中加入这些自定义的属性:
<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:xr="http://schemas.android.com/apk/res/com.rong.test"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/black"android:orientation="vertical"><com.rong.activity.CustomViewandroid:layout_width="300dp"android:layout_height="300dp"android:layout_centerInParent="true"android:background="#ff0000"xr:customText="自定义控件"xr:customTextColor="#000000"xr:customTextSize="40sp"/></RelativeLayout>
第三部继承View重写
packagecom.rong.activity;importcom.rong.test.R;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.graphics.Rect;importandroid.util.AttributeSet;importandroid.view.View;/***自定义控件**@author徐荣**/publicclassCustomViewextendsView{/***自定义画笔*/privatePaintmPaint;/***文字范围*/privateRectmBounds;/***自定义文字*/privateStringcustomText;/***自定义大小*/privateintcustomTextSize;/***自定义颜色*/privateintcustomTextColor;publicCustomView(Contextcontext,AttributeSetattrs){super(context,attrs);TypedArraytypedArray=context.obtainStyledAttributes(attrs,R.styleable.CustomViewStyle);//获取自定义文字customText=typedArray.getString(R.styleable.CustomViewStyle_customText);//获取自定义文字大小customTextSize=typedArray.getDimensionPixelSize(R.styleable.CustomViewStyle_customTextSize,28);//或者自定义文字颜色customTextColor=typedArray.getColor(R.styleable.CustomViewStyle_customTextColor,Color.WHITE);//要回收这个typedArray对象typedArray.recycle();initView();}publicvoidinitView(){//初始化画笔mPaint=newPaint();mPaint.setAntiAlias(true);mPaint.setStyle(Paint.Style.FILL);mPaint.setColor(customTextColor);mPaint.setTextSize(customTextSize);//生成文字区域mBounds=newRect();}@OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);//获取文字显示区域mBoundsmPaint.getTextBounds(customText,0,customText.length(),mBounds);//使文字宽居中显示=控件的宽度/2-文字的宽度/2floathelfWidth=getWidth()/2-mBounds.width()/2;//使文字高居中显示=控件的宽度/2+文字的宽度/2floathelfHeight=getHeight()/2+mBounds.height()/2;//绘制文字canvas.drawText(customText,helfWidth,helfHeight,mPaint);}}
Run
更多相关文章
- Android中测量文字的宽度和高度
- Android(安卓)Studio 常用控件和常用布局的介绍
- android播放网络或者本地视频(截取某帧做封面)VideoView原生控件
- Android(安卓)编程下 Touch 事件的分发和消费机制(一)
- 笔记6
- Android(安卓)API 中文 (51) —— ZoomButtonsController
- Android(安卓)API 中文 (52) —— ZoomButtonsController.OnZoomLi
- Android(安卓)KeyEvent分发机制
- Android(安卓)onTouch、OnLongClick、onClick及ScrollView滑动事