自定义View(1)
来源:互联网 发布:python matlab eval 编辑:程序博客网 时间:2024/05/16 07:18
步骤
- 自定义属性
- 重写ondraw()
- 重写onMeasure()
带着问题看代码
- 在不重写onMeasure方法时, wrap_content与match_parent对布局影响的区别
- 测量之前怎么去获取View的大小
- setMeasuredDimension(…)方法的作用
效果图
自定义属性
布局文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:test="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="simplifyreader.nobita.com.customview.MainActivity"> <simplifyreader.nobita.com.customview.MyView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" test:backColor="#ff0" test:text="这里" test:textColor="#f00" test:textSize="50sp"/></RelativeLayout>
分析:
1、 添加xmlns:test=”http://schemas.android.com/apk/res-auto”,其实test为自定义属性前缀
attrs文件
在values文件目录下创建attrs.xml文件
<resources> <declare-styleable name="MyView"> <attr name="textColor" format="color"/> <attr name="text" format="string"/> <attr name="backColor" format="color"/> <attr name="textSize" format="dimension"/> </declare-styleable></resources>
自定义View代码
在代码中有关键注释
public class MyView extends View { Paint mPaint; Paint mBackPaint; String mText; int mTextColor; float mTextSize; int defalutTextColor = Color.RED; float defalutTextSize = Color.RED; int mBackColor; int defalutBackColor = Color.BLACK; private Rect mBound; public MyView(Context context) { super(context); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); mPaint = new Paint(); mBackPaint = new Paint(); //获取xml文件中自定义属性集合 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView); mText = a.getString(R.styleable.MyView_text); mTextColor = a.getColor(R.styleable.MyView_textColor, defalutTextColor); mBackColor = a.getColor(R.styleable.MyView_backColor, defalutBackColor); mTextSize = a.getDimension(R.styleable.MyView_textSize, 36); a.recycle(); mPaint.setColor(mTextColor); mPaint.setTextSize(mTextSize); mBackPaint.setColor(mBackColor); //获取mText的长宽,并封装进mBound mBound = new Rect(); mPaint.getTextBounds(mText, 0, mText.length(), mBound); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int width; int height; if (widthMode == MeasureSpec.EXACTLY) { width = widthSize; } else { width = mBound.width() + getPaddingLeft() + getPaddingRight(); } if (heightMode == MeasureSpec.EXACTLY) { height = heightSize; } else { height = mBound.height() + getPaddingBottom() + getPaddingTop(); } //必须调用,设置该View的长宽 setMeasuredDimension(width, height); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //绘制背景 canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mBackPaint); //绘制文本,第二个和第三个参数为x_y坐标(文本的左下角) canvas.drawText(mText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint); }}
1 0
- 自定义View(1)
- 自定义view(1)
- 自定义View(1)
- 自定义View(1)
- 自定义View(1)
- 自定义View(1):自定义文本显示
- 自定义View(1)---Topbar
- Android自定义View(1)
- android 自定义view(1)
- 初识自定义View(1)
- android 自定义 View(1)
- Android自定义View(1)
- 自定义View(一) 初识自定义view
- Android自定义View之View详解(1)
- 自定义View(1)------------自定义属性
- 自定义view(自定义属性)
- 牛刀小试-Android自定义view(1)
- Android自定义View基础(1)
- 使用JQUERY封装Form和input进行提交
- 实现大小字母转换
- PAT A1030
- 监控一个方法的执行时间(不用抛出InterruptedException异常)
- ActiveMQ安装部署(Windows)
- 自定义View(1)
- java数组定义方法
- php使用qrcode 生成带logo的二维码
- 【JavaEE】经典JAVA EE企业应用实战-读书笔记13
- hdu1010 Tempter of the Bone dfs搜索 剪枝
- linux命令之crontab定时执行任务
- DllImportAttribute.EntryPoint 字段
- 全排列 回溯
- 关于虚拟机kali 装搜狗输入法的二三事