当前位置: 首页 > news >正文

Android 控件背景实现发光效果

主要实现的那种光晕效果:中间亮,四周逐渐变淡的。

这边有三种发光效果,先上效果图。

第一种、圆形发光体

实现代码:新建shape_light.xml,导入以下代码。使用时,直接给view设置为background。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval"><gradientandroid:centerColor="@color/transparent"android:centerX="0.5"android:centerY="0.5"android:gradientRadius="180dp"android:startColor="@color/yellow"android:type="radial" />
</shape>

第二种、矩形发光体

代码实现:通过自定义view实现。

package com.fht.testprojectimport android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View/*** @author fenghaitao* @time 2023/11/1 16:40*/
class RectLightView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int = 0
) : View(context, attributeSet, defStyleAttr) {private val paint: Paint = Paint()private val corner = 50fprivate val count = 200init {paint.isAntiAlias = falsepaint.style = Paint.Style.FILLpaint.color = Color.YELLOW}@SuppressLint("DrawAllocation")override fun onDraw(canvas: Canvas?) {super.onDraw(canvas)val w = width / countval h = height / countfor (i in 0..count) {paint.alpha = (255 / count) * iif (((width - 2 * (w * i)) > 0) && ((height - 2 * (h * i)) > 0)) {val rectF = RectF().apply {left = (w * i).toFloat()top = (h * i).toFloat()right = (width - w * i).toFloat()bottom = (height - h * i).toFloat()}canvas?.drawRoundRect(rectF, corner, corner, paint)}}}
}

第三种、矩形发光体,比上一种更透明

这种有点瑕疵,中间有一点空白,不过稍微修改一下代码也可以去掉,这里就不做修改了。

代码实现:通过自定义view实现。

package com.fht.testprojectimport android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View/*** @author fenghaitao* @time 2023/11/1 16:40*/
class LightView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int = 0
) : View(context, attributeSet, defStyleAttr) {private val paint: Paint = Paint()private val corner = 1fprivate val count = 100init {paint.isAntiAlias = falsepaint.style = Paint.Style.STROKEpaint.color = Color.YELLOW}@SuppressLint("DrawAllocation")override fun onDraw(canvas: Canvas?) {super.onDraw(canvas)val w = width / countval h = height / countpaint.strokeWidth = w.toFloat()for (i in 0..count) {paint.alpha = (255 / count) * iif (((width - 2 * (w * i)) > 0) && ((height - 2 * (w * i)) > 0)) {val rectF = RectF().apply {left = (w * i).toFloat()top = (w * i).toFloat()right = (width - w * i).toFloat()bottom = (height - w * i).toFloat()}canvas?.drawRect(rectF, paint)}}}
}

http://www.lryc.cn/news/215948.html

相关文章:

  • 安全狗亮相厦门市工信领域数据安全宣贯培训会
  • 最长回文子串
  • 从瀑布模式到水母模式:ChatGPT引领软件研发的革新之路
  • 一种使用wireshark快速分析抓包文件amr音频流的思路方法
  • 银河麒麟x86版、银河麒麟arm版操作系统编译zlmediakit
  • InnoDB - 双写机制
  • 【蓝桥杯选拔赛真题08】C++最大值最小值平均值 青少年组蓝桥杯C++选拔赛真题 STEMA比赛真题解析
  • 软考高级系统架构设计师系列之:系统开发基础知识、项目管理、信息安全和网络安全、计算机网络章节选择题详解
  • 0基础学习PyFlink——时间滑动窗口(Sliding Time Windows)
  • API安全之《大话:API的前世今生》
  • H5或者Vue实现二维码识别
  • stm32整理(三)ADC
  • Redis-持久化+主从架构
  • STM32H750之FreeRTOS学习--------(四)中断管理
  • Macroscope安全漏洞检测工具简介
  • 【Linux】Nignx的入门使用负载均衡动静分离(前后端项目部署)---超详细
  • 【入门Flink】- 04Flink部署模式和运行模式【偏概念】
  • react面试要点
  • 在Google Kubernetes集群创建分布式Jenkins(一)
  • 【Python全栈_公开课学习记录】
  • uniapp循环列表单选框实现单选
  • 【强化学习】14 —— A3C(Asynchronous Advantage Actor Critic)
  • Google单元测试sample分析(四)
  • 网络套接字编程(二)
  • LLaMA-Adapter源码解析
  • JavaScript设计模式之发布-订阅模式
  • mysql---索引
  • 微信小程序——简易复制文本
  • 【51单片机】矩阵键盘与定时器(学习笔记)
  • vue 中使用async await