Android 自定义View (二) 进阶

news/2023/9/25 17:13:55

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125

继续自定义View之旅,前面已经介绍过一个自定义View的基础的例子,Android 自定义View (一),如果你还对自定义View不了解可以去看看。今天给大家带来一个稍微复杂点的例子。

自定义View显示一张图片,下面包含图片的文本介绍,类似相片介绍什么的,不过不重要,主要是学习自定义View的用法么。

还记得上一篇讲的4个步骤么:

1、自定义View的属性
2、在View的构造方法中获得我们自定义的属性
[ 3、重写onMesure ]
4、重写onDraw

直接切入正题:

1、在res/values/attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources><attr name="titleText" format="string" /><attr name="titleTextSize" format="dimension" /><attr name="titleTextColor" format="color" /><attr name="image" format="reference" /><attr name="imageScaleType"><enum name="fillXY" value="0" /><enum name="center" value="1" /></attr><declare-styleable name="CustomImageView"><attr name="titleText" /><attr name="titleTextSize" /><attr name="titleTextColor" /><attr name="image" /><attr name="imageScaleType" /></declare-styleable></resources>

2、在构造中获得我们的自定义属性:

/*** 初始化所特有自定义类型* * @param context* @param attrs* @param defStyle*/public CustomImageView(Context context, AttributeSet attrs, int defStyle){super(context, attrs, defStyle);TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImageView, defStyle, 0);int n = a.getIndexCount();for (int i = 0; i < n; i++){int attr = a.getIndex(i);switch (attr){case R.styleable.CustomImageView_image:mImage = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, 0));break;case R.styleable.CustomImageView_imageScaleType:mImageScale = a.getInt(attr, 0);break;case R.styleable.CustomImageView_titleText:mTitle = a.getString(attr);break;case R.styleable.CustomImageView_titleTextColor:mTextColor = a.getColor(attr, Color.BLACK);break;case R.styleable.CustomImageView_titleTextSize:mTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,16, getResources().getDisplayMetrics()));break;}}a.recycle();rect = new Rect();mPaint = new Paint();mTextBound = new Rect();mPaint.setTextSize(mTextSize);// 计算了描绘字体需要的范围mPaint.getTextBounds(mTitle, 0, mTitle.length(), mTextBound);}

3、重写onMeasure

	@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){// super.onMeasure(widthMeasureSpec, heightMeasureSpec);/*** 设置宽度*/int specMode = MeasureSpec.getMode(widthMeasureSpec);int specSize = MeasureSpec.getSize(widthMeasureSpec);if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate{Log.e("xxx", "EXACTLY");mWidth = specSize;} else{// 由图片决定的宽int desireByImg = getPaddingLeft() + getPaddingRight() + mImage.getWidth();// 由字体决定的宽int desireByTitle = getPaddingLeft() + getPaddingRight() + mTextBound.width();if (specMode == MeasureSpec.AT_MOST)// wrap_content{int desire = Math.max(desireByImg, desireByTitle);mWidth = Math.min(desire, specSize);Log.e("xxx", "AT_MOST");}}/**** 设置高度*/specMode = MeasureSpec.getMode(heightMeasureSpec);specSize = MeasureSpec.getSize(heightMeasureSpec);if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate{mHeight = specSize;} else{int desire = getPaddingTop() + getPaddingBottom() + mImage.getHeight() + mTextBound.height();if (specMode == MeasureSpec.AT_MOST)// wrap_content{mHeight = Math.min(desire, specSize);}}setMeasuredDimension(mWidth, mHeight);}

4、重写onDraw

@Overrideprotected void onDraw(Canvas canvas){// super.onDraw(canvas);/*** 边框*/mPaint.setStrokeWidth(4);mPaint.setStyle(Paint.Style.STROKE);mPaint.setColor(Color.CYAN);canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);rect.left = getPaddingLeft();rect.right = mWidth - getPaddingRight();rect.top = getPaddingTop();rect.bottom = mHeight - getPaddingBottom();mPaint.setColor(mTextColor);mPaint.setStyle(Style.FILL);/*** 当前设置的宽度小于字体需要的宽度,将字体改为xxx...*/if (mTextBound.width() > mWidth){TextPaint paint = new TextPaint(mPaint);String msg = TextUtils.ellipsize(mTitle, paint, (float) mWidth - getPaddingLeft() - getPaddingRight(),TextUtils.TruncateAt.END).toString();canvas.drawText(msg, getPaddingLeft(), mHeight - getPaddingBottom(), mPaint);} else{//正常情况,将字体居中canvas.drawText(mTitle, mWidth / 2 - mTextBound.width() * 1.0f / 2, mHeight - getPaddingBottom(), mPaint);}//取消使用掉的快rect.bottom -= mTextBound.height();if (mImageScale == IMAGE_SCALE_FITXY){canvas.drawBitmap(mImage, null, rect, mPaint);} else{//计算居中的矩形范围rect.left = mWidth / 2 - mImage.getWidth() / 2;rect.right = mWidth / 2 + mImage.getWidth() / 2;rect.top = (mHeight - mTextBound.height()) / 2 - mImage.getHeight() / 2;rect.bottom = (mHeight - mTextBound.height()) / 2 + mImage.getHeight() / 2;canvas.drawBitmap(mImage, null, rect, mPaint);}}

代码,结合注释和第一篇View的使用,应该可以看懂,不明白的留言。下面我们引入我们的自定义View:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:zhy="http://schemas.android.com/apk/res/com.zhy.customview02"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><com.zhy.customview02.view.CustomImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="10dp"android:padding="10dp"zhy:image="@drawable/ic_launcher"zhy:imageScaleType="center"zhy:titleText="hello andorid ! "zhy:titleTextColor="#ff0000"zhy:titleTextSize="30sp" /><com.zhy.customview02.view.CustomImageViewandroid:layout_width="100dp"android:layout_height="wrap_content"android:layout_margin="10dp"android:padding="10dp"zhy:image="@drawable/ic_launcher"zhy:imageScaleType="center"zhy:titleText="helloworldwelcome"zhy:titleTextColor="#00ff00"zhy:titleTextSize="20sp" /><com.zhy.customview02.view.CustomImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="10dp"android:padding="10dp"zhy:image="@drawable/lmj"zhy:imageScaleType="center"zhy:titleText="妹子~"zhy:titleTextColor="#ff0000"zhy:titleTextSize="12sp" /></LinearLayout>

我特意让显示出现3中情况:

1、字体的宽度大于图片,且View宽度设置为wrap_content

2、View宽度设置为精确值,字体的长度大于此宽度

3、图片的宽度大于字体,且View宽度设置为wrap_content

看看显示效果:


怎么样,对于这三种情况所展示的效果都还不错吧。


好了,就到这里,各位看官,没事留个言,顶一个呗~


源码点击下载




转载于:https://www.cnblogs.com/oversea201405/p/3752018.html


https://dhexx.cn/news/show-17532.html

相关文章

mycat-垂直拆分和全局表使用案例

建库&#xff0c;建表的sql 文件&#xff0c;下载链接https://download.csdn.net/download/qq_41712271/15903634按照不同的表&#xff08;或者Schema&#xff09;来切分到不同的数据库&#xff08;主机&#xff09;之上&#xff0c;这种切分可以称之为数据 的垂直&#xff08;…

nacos-server-2.0.4 启动部署失败问题(windows系统)

基础准备 1.下载nacos-server-2.0.4.zip 地址&#xff1a;https://github.com/alibaba/nacos/releases/tag/2.0.4 2.执行sql文件nacos-mysql.sql&#xff0c;文件位于解压缩目录下nacos-server-2.0.4\nacos\conf 3.nacos-server-2.0.4\nacos\conf路径下配置文件application…

mysql 搭建主从复制,一主一从

MySQL主从复制原理 复制是指将主数据库的DDL 和 DML 操作通过二进制日志传到从库服务器中&#xff0c;然后在从库上对这些日志重新执行&#xff08;也叫重做&#xff09;&#xff0c;从而使得从库和主库的数据保持同步。 MySQL支持一台主库同时向多台从库进行复制&#xff0c;…

listview android:cacheColorHint,android:listSelector属性作用(转)

ListView是常用的显示控件&#xff0c;默认背景是和系统窗口一样的透明色&#xff0c;如果给ListView加上背景图片&#xff0c;或者背景颜色时&#xff0c;滚动时listView会黑掉&#xff0c;原因是&#xff0c;滚动时&#xff0c;列表里面的view重绘时&#xff0c;用的依旧是系…

java获取操作系统信息

Java代码 import java.util.Properties; Properties propsSystem.getProperties(); //获得系统属性集 String osName props.getProperty("os.name"); //操作系统名称 String osArch props.getProperty("os.arch"); //操作系统构架 String osVersi…

MyCat-web 可视化运维管理和监控平台

MyCat-web简介 Mycat-web 是 Mycat 可视化运维的管理和监控平台&#xff0c;弥补了 Mycat 在监控上的空白。 帮 Mycat 分担统计任务和配置管理任务。 Mycat-web 引入了 ZooKeeper 作为配置中心&#xff0c;可以管理多个节 点。 Mycat-web 主要管理和监控 Mycat 的流量、连接、…

mysql 搭建主从复制,双主双从

双主双从 架构 一个主机 Master1 用于处理所有写请求&#xff0c;它的从机 Slave1 和另一台主机 Master2 还有它的从 机 Slave2 负责所有读请求。当 Master1 主机宕机后&#xff0c;Master2 主机负责写请求&#xff0c;Master1 、 Master2 互为备机。架构图如下: 其实和 一主…

云计算之路-阿里云上:从ASP.NET线程角度对“黑色30秒”问题的全新分析

在这篇博文中&#xff0c;我们抛开对阿里云的怀疑&#xff0c;完全从ASP.NET的角度进行分析&#xff0c;看能不能找到针对问题现象的更合理的解释。 “黑色30秒”问题现象的主要特征是&#xff1a;排队的请求&#xff08;Requests Queued&#xff09;突增&#xff0c;到达HTTP.…

mycat 分片规则-取模分片 mod-long

为方便测试&#xff0c;本实例在以下案例中测试https://blog.csdn.net/qq_41712271/article/details/114984855?spm1001.2014.3001.5501 原理 取余分片方式&#xff1a;分片键&#xff08;一个列&#xff09;与节点数量进行取余&#xff0c;得到余数&#xff0c;将数据写入对应…

微信公众帐号开发之一(java)

闲来没事&#xff0c;就记录一下微信公众平台的开发吧~ 其实微信公众平台开发没有想象中的那么困难&#xff0c;因为注册了微信公众平台帐号登录之后在开发者模式里有详细的文档&#xff0c;个人感觉介绍还是比较详细的。 微信公众平台订阅号和服务号的区别网上已经有N多的介绍…