RectF类android.graphics.RectF和Rect类android.graphics.Rect

理解Android图形界面布局中的RectF类
本文详细介绍了Android图形界面布局中的RectF类,包括其构造方法、属性和常用方法,帮助开发者更好地理解和使用该类进行界面元素定位。
RectF 这个类包含一个矩形的四个单精度浮点坐标。矩形通过上下左右4个边的坐标来表示一个矩形。这些坐标值属性可以被直接访问,用width()和 height()方法可以获取矩形的宽和高。注意:大多数方法不会检查这些坐标分类是否错误(也就是left<=right和top& lt;=bottom). 

RectF一共有四个构造方法: 

RectF()构造一个无参的矩形 

RectF(float left,float top,float right,float bottom)构造一个指定了4个参数的矩形 

RectF(Rect F r)根据指定的RectF对象来构造一个RectF对象(对象的左边坐标不变) 

RectF(Rect r)根据给定的Rect对象来构造一个RectF对象 

RectF提供了很多方法,下面介绍几个方法: 

Public Boolean contain(RectF r);判断一个矩形是否在此矩形内,如果在这个矩形内或者和这个矩形等价则返回true,同样类似的方法还有public Boolean contain(float left,float top,float right,float bottom)和public Boolean contain(float x,float y)。 

Public void union(float x,float y)更新这个矩形,使它包含矩形自己和(x,y)这个点。 

RectF类提供的方法都比较简单,容易理解,再此就不再一一赘述 

Android.graphics.Rect类,这个类同android.graphics.RectF很相似,不同的地方是Rect类的坐标是用整形表示的,而RectF的坐标是用单精度浮点型表示的。这里大家一定要注意 啊。


转载自:http://byandby.iteye.com/blog/826304

package com.example.tapobulb.view import android.R.attr.height import android.R.attr.width import android.content.Context import android.graphics.Canvas import android.graphics.Paint import android.graphics.PorterDuff import android.graphics.PorterDuffXfermode import android.graphics.Rect import android.graphics.RectF import android.view.MotionEvent import android.view.View /** * @Author: admin * @CreateDate: 2025/8/20 12:08 * @Description: */ class GuideView(context: Context, private val targetView: View) : View(context) { private val paint = Paint() private val rectF = RectF() init { // 使用你的颜色 #0E1017,透明度 50% paint.color = 0x500E1017.toInt() // 50% 透明度 paint.isAntiAlias = true } override fun onDraw(canvas: Canvas) { super.onDraw(canvas) // 绘制半透明遮罩层 canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint) // 创建图层,以便 CLEAR 模式生效 val sc = canvas.saveLayer(0f, 0f, width.toFloat(), height.toFloat(), null) // 绘制遮罩层(深灰色) canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint) // 设置 CLEAR 模式 paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR) // 获取目标 View 的位置并转换为 GuideView 坐标系 val targetRect = Rect() targetView.getGlobalVisibleRect(targetRect) val guideRect = Rect() this.getGlobalVisibleRect(guideRect) val relativeLeft = targetRect.left - guideRect.left val relativeTop = targetRect.top - guideRect.top val relativeRight = targetRect.right - guideRect.left val relativeBottom = targetRect.bottom - guideRect.top val centerX = (relativeLeft + relativeRight) / 2f val centerY = (relativeTop + relativeBottom) / 2f val radius = maxOf(targetView.width, targetView.height) / 2f + 30 // 绘制透明圆形洞 canvas.drawCircle(centerX, centerY, radius, paint) // 恢复图层,应用 CLEAR 效果 paint.xfermode = null canvas.restoreToCount(sc) } override fun onTouchEvent(event: MotionEvent): Boolean { // 拦截点击事件,不传递到底层 return true } } 这是我报错的guideview
08-21
解读一下这个的代码package com.heytap.common.gradientstroke import android.content.res.ColorStateList import android.graphics.BlendMode import android.graphics.Canvas import android.graphics.Color import android.graphics.ColorFilter import android.graphics.Paint import android.graphics.Path import android.graphics.PixelFormat import android.graphics.Rect import android.graphics.RectF import android.graphics.drawable.Drawable import androidx.core.graphics.toRectF import com.heytap.commonbiz.BuildConfig import com.oplus.graphics.OplusPath /** * @author WangXinQiang * @date 2025/11/26 */ @Suppress("UnusedDataClassCopyResult") class GradientStrokeDrawable( backgroundColor: ColorStateList, private val strokeParams: GradientStrokeParams = GradientStrokeParams(), private val debug: Boolean = true ) : Drawable() { companion object { private const val TAG = "GradientStrokeDrawable" } init { if (isDebug()) { strokeParams.copy(GradientStrokeManager.debugParams()) } } private val strokePath: Path = Path() private val strokeShader: GradientStrokeShader = GradientStrokeShader().apply { initStrokeShader(this) } private val strokePaint: Paint = Paint().apply { initStrokePaint(this) } private val backgroundPaint = Paint().apply { isAntiAlias = true style = Paint.Style.FILL color = backgroundColor.getColorForState(state, backgroundColor.defaultColor) } private val boundsF = RectF() private val backgroundPath = Path() override fun onBoundsChange(bounds: Rect) { boundsF.set(bounds) strokeShader.apply { setResolution(bounds.width(), bounds.height()) setPadding(0, 0) } strokePath.apply { reset() addRoundRect( bounds.toRectF(), 20f, 20f, Path.Direction.CW ) } updateBackgroundPath() } override fun draw(canvas: Canvas) { checkUpdateParams() canvas.drawPath(backgroundPath, backgroundPaint) //平滑圆角 canvas.drawPath(strokePath, strokePaint) //光感描边 } override fun setAlpha(alpha: Int) { //do nothing } override fun setColorFilter(colorFilter: ColorFilter?) { //do nothing } @Deprecated("Deprecated in Java", ReplaceWith("")) override fun getOpacity(): Int { return PixelFormat.TRANSLUCENT } fun update(params: GradientStrokeParams) { if (strokeParams != params) { updateParams(params) invalidateSelf() } } private fun checkUpdateParams() { if (isDebug() && strokeParams != GradientStrokeManager.debugParams()) { updateParams(GradientStrokeManager.debugParams()) } } private fun updateParams(params: GradientStrokeParams) { strokeParams.copy(params) initStrokeShader(strokeShader) initStrokePaint(strokePaint) updateBackgroundPath() } private fun initStrokeShader(shader: GradientStrokeShader) { shader.apply { color = Color.WHITE strokeParams.let { setCorner(it.cornerType, it.radius, it.weight) ratio = it.ratio nearLineWidth = it.nearLineWidth //对应demo参数Line_1_W nearLineAlpha = it.nearLineAlpha //对应demo参数Line_1_A nearLineFadeToSides1 = it.nearLineFadeToSides1 //对应demo参数Line_1_横实 nearLineFadeToSides2 = it.nearLineFadeToSides2 //对应demo参数Line_1_横虚 nearLineFadeTowardCenter1 = it.nearLineFadeTowardCenter1 //对应demo参数Line_1_纵虚 nearLineFadeTowardCenter2 = it.nearLineFadeTowardCenter2 //对应demo参数Line_1_纵实 farLineWidth = it.farLineWidth //对应demo参数Line_2_W farLineAlpha = it.farLineAlpha //对应demo参数Line_2_A farLineFadeToSides1 = it.farLineFadeToSides1 //对应demo参数Line_2_横实 farLineFadeToSides2 = it.farLineFadeToSides2 //对应demo参数Line_2_横虚 farLineFadeTowardCenter1 = it.farLineFadeTowardCenter1 //对应demo参数Line_2_纵虚 farLineFadeTowardCenter2 = it.farLineFadeTowardCenter2 //对应demo参数Line_2_纵实 } } } private fun initStrokePaint(paint: Paint) { paint.apply { isAntiAlias = true isDither = true style = Paint.Style.FILL alpha = (255 * strokeParams.alpha).toInt() blendMode = BlendMode.SRC_OVER shader = strokeShader.shader } } private fun updateBackgroundPath() { backgroundPath.apply { reset() OplusPath(this).addSmoothRoundRect( 0f, 0f, boundsF.right - boundsF.left, boundsF.bottom - boundsF.top, strokeParams.radius, strokeParams.radius, strokeParams.weight, Path.Direction.CCW ) } } private fun isDebug(): Boolean { return debug && BuildConfig.DEBUG } }
最新发布
12-09
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值