declare styleableandroid swiperefreshlayout 为什么是圆形的

declare styleable  时间:2022-02-27  阅读:()

GridView 的item高度很高,快崩溃了,该怎么处理

自定义GridView以解决ScrollView嵌套Android自身GridView出现的疑难问题 Android开发中偶尔会遇到ScrollView嵌套GridView情景,但是谷歌官网是不推荐这种方式的,因为他们都有滚动条,嵌套使用会有冲突,无奈只能另谋他路,自定义个仿GridView的控件 1.定义attrs.xml文件 <declare-styleable name="

android swiperefreshlayout 为什么是圆形的

其实主要靠:paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));这行代码,为什么呢,我给大家解释下,SRC_IN这种模式,两个绘制的效果叠加后取交集展现后图,怎么说呢,咱们第一个绘制的是个圆形,第二个绘制的是个Bitmap,于是交集为圆形,展现的是BItmap,就实现了圆形图片效果。圆角,其实就是先绘制圆角矩形,是不是很简单,以后别人再说实现圆角,你就把这一行代码给他就行了。 从Android的示例中,给大家证明一下: 下面有一张PorterDuff.Mode的16中效果图,咱们的只是其一: 源码咱们只关心谁先谁后绘制的: [java] view plaincopy canvas.translate(x, y); canvas.drawBitmap(mDstB, 0, 0, paint); paint.setXfermode(sModes[i]); canvas.drawBitmap(mSrcB, 0, 0, paint); paint.setXfermode(null); canvas.restoreToCount(sc); 可以看出先绘制的Dst,再绘制的Src,最后的展示是SrcIn那个图:显示的区域是二者交集,展示的是Src(后者)。和咱们前面结论一致。效果16种,大家可以自由组合展示不同的效果。 好了,原理和核心代码解释完成。下面开始写自定义View。 1、自定义属性: [html] view plaincopy <?xml version="1.0" encoding="utf-8"?> <resources> <attr name="borderRadius" format="dimension" /> <attr name="type"> <enum name="circle" value="0" /> <enum name="round" value="1" /> </attr> <attr name="src" format="reference"></attr> <declare-styleable name="CustomImageView"> <attr name="borderRadius" /> <attr name="type" /> <attr name="src" /> </declare-styleable> </resources> 2、构造中获取自定义的属性: [java] view plaincopy /** * TYPE_CIRCLE / TYPE_ROUND */ private int type; private static final int TYPE_CIRCLE = 0; private static final int TYPE_ROUND = 1; /** * 图片 */ private Bitmap mSrc; /** * 圆角的大小 */ private int mRadius; /** * 控件的宽度 */ private int mWidth; /** * 控件的高度 */ private int mHeight; public CustomImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomImageView(Context context) { this(context, null); } /** * 初始化一些自定义的参数 * * @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_src: mSrc = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, 0)); break; case R.styleable.CustomImageView_type: type = a.getInt(attr, 0);// 默认为Circle break; case R.styleable.CustomImageView_borderRadius: mRadius= a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, getResources().getDisplayMetrics()));// 默认为10DP break; } } a.recycle(); } 3、onMeasure中获取控件宽高: [java] view plaincopy /** * 计算控件的高度和宽度 */ @Override protected 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 , urate { mWidth = specSize; } else { // 由图片决定的宽 int desireByImg = getPaddingLeft() + getPaddingRight() + mSrc.getWidth(); if (specMode == MeasureSpec.AT_MOST)// wrap_content { mWidth = Math.min(desireByImg, specSize); } else mWidth = desireByImg; } /*** * 设置高度 */ specMode = MeasureSpec.getMode(heightMeasureSpec); specSize = MeasureSpec.getSize(heightMeasureSpec); if (specMode == MeasureSpec.EXACTLY)// match_parent , urate { mHeight = specSize; } else { int desire = getPaddingTop() + getPaddingBottom() + mSrc.getHeight(); if (specMode == MeasureSpec.AT_MOST)// wrap_content { mHeight = Math.min(desire, specSize); } else mHeight = desire; } setMeasuredDimension(mWidth, mHeight); } 4、根据Type绘制: [java] view plaincopy /** * 绘制 */ @Override protected void onDraw(Canvas canvas) { switch (type) { // 如果是TYPE_CIRCLE绘制圆形 case TYPE_CIRCLE: int min = Math.min(mWidth, mHeight); /** * 长度如果不一致,按小的值进行压缩 */ mSrc = Bitmap.createScaledBitmap(mSrc, min, min, false); canvas.drawBitmap(createCircleImage(mSrc, min), 0, 0, null); break; case TYPE_ROUND: canvas.drawBitmap(createRoundConerImage(mSrc), 0, 0, null); break; } } /** * 根据原图和变长绘制圆形图片 * * @param source * @param min * @return */ private Bitmap createCircleImage(Bitmap source, int min) { final Paint paint = new Paint(); paint.setAntiAlias(true); Bitmap target = Bitmap.createBitmap(min, min, Config.ARGB_8888); /** * 产生一个同样大小的画布 */ Canvas canvas = new Canvas(target); /** * 首先绘制圆形 */ canvas.drawCircle(min / 2, min / 2, min / 2, paint); /** * 使用SRC_IN,参考上面的说明 */ paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); /** * 绘制图片 */ canvas.drawBitmap(source, 0, 0, paint); return target; } /** * 根据原图添加圆角 * * @param source * @return */ private Bitmap createRoundConerImage(Bitmap source) { final Paint paint = new Paint(); paint.setAntiAlias(true); Bitmap target = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888); Canvas canvas = new Canvas(target); RectF rect = new RectF(0, 0, source.getWidth(), source.getHeight()); canvas.drawRoundRect(rect, mRadius, mRadius, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(source, 0, 0, paint); return target; }

Hostodo:$19.99/年KVM-1GB/12GB/4TB/拉斯维加斯

Hostodo发布了几款采用NVMe磁盘的促销套餐,从512MB内存起,最低年付14.99美元,基于KVM架构,开设在拉斯维加斯机房。这是一家成立于2014年的国外VPS主机商,主打低价VPS套餐且年付为主,基于OpenVZ和KVM架构,产品性能一般,数据中心目前在拉斯维加斯和迈阿密,支持使用PayPal或者支付宝等付款方式。下面列出几款NVMe硬盘套餐配置信息。CPU:1core内存:512MB...

PQ.hosting全线9折,1Gbps带宽不限流量VPS/€3/月,全球11大机房可选

Hostadvice主机目录对我们的服务进行了测试,然后给PQ.hosting颁发了十大WordPress托管奖。为此,宣布PQ.Hosting将在一周内进行折扣优惠,购买和续订虚拟服务器使用优惠码:Hostadvice ,全部优惠10%。PQ.hosting,国外商家,成天于2019年,正规公司,是全球互联网注册商协会 RIPE 的成员。主要是因为提供1Gbps带宽、不限流量的基于KVM虚拟的V...

新网,域名7月盛夏1核心2G内存.COM域名仅19.9元/首年,主机9.9元/月,企业邮箱0元体验

新网好不好?新网域名便宜吗?新网怎么样?新网是国内老牌知名域名注册商,企业正规化运营,资质齐全,与阿里云万网和腾讯云DNSPOD同为国内服务商巨头。近日新网发布了最新的七月放价季优惠活动,主要针对域名、云主机、企业邮箱、SSL证书等多款云产品推送了超值的优惠,其中.com顶级域名仅19.9元/首年,.cn域名仅16元/首年,云主机1核心2G内存3Mbps带宽仅9.9元/月,企业邮箱更是免费送1年,...

declare styleable为你推荐
bftBFT、什么東西噢、什么意思、解釋下。免费erp最方便使用的免费erp软件有哪些电视蚂蚁电视机里进蚂蚁怎么处理cs躲猫猫CS GO怎么玩躲猫猫diskgenius免费版diskgenius专业版破解怎么用实数的定义实数的定义局域网ip扫描工具局域网的IP段怎么扫描rs485协议HART modbus profibus 这三种协议有什么区别?这几种协议都是干什么用的?qsv视频格式转换器爱奇艺QSV转换工具怎么将qsv格式转换mp4视频数据管理制度网络管理制度.
免费cn域名 息壤主机 便宜域名 全球付 parseerror 云鼎网络 qq数据库下载 促正网秒杀 国外免费全能空间 合租空间 softbank邮箱 赞助 世界测速 360云服务 免费ftp 全能空间 永久免费空间 lamp怎么读 阿里云邮箱登陆 测速电信 更多