关于Android的径向渐变高级编程的实现
来源:互联网 发布:stc单片机usb驱动下载 编辑:程序博客网 时间:2024/06/05 16:10
在最近的一系列文章,对midipad APP,有一个关于一个radialgradiant渲染每个padview利用的探讨,对审美的原因,这是一个软件层,而不是一个硬件层。在这个简短的系列中,我们首先看看差异是什么,然后探索一种方法来调整硬件层。
在midipad的文章,我说我喜欢如何呈现的一个软件层的径向渐变,所以让我们开始比较同一径向出现时所采用的硬件和软件层。让我们先定义一个简单的自定义视图是viewpad中midipad文章大大简化版。
它使用相同的技术–我们创建一个新的径向尺寸变化时的观点,但径向本身是在PadView使用的使用非常相似。shaderfactory的使用是一种机制,我们将使用后在不同的径向厂替代。
class GradientView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0, private val bounds: RectF = RectF()
) : View(context, attrs, defStyleAttr, defStyleRes) {
private val defaultColour: Int by lazyFast { context.theme.getColour(R.attr.colorAccent)}private val paint: Paint = Paint().apply { isAntiAlias = true style = Paint.Style.FILL }var shaderFactory: (width: Float, height: Float) -> Shader = { viewWidth, viewHeight -> RadialGradient( viewWidth / 2f, viewHeight / 2f, Math.min(viewWidth, viewHeight) / 2f, defaultColour, Color.TRANSPARENT, Shader.TileMode.CLAMP )}override fun onSizeChanged(newWidth: Int, newHeight: Int, oldWidth: Int, oldHeight: Int) = super.onSizeChanged(newWidth, newHeight, oldWidth, oldHeight).run { adjustBounds(newWidth.toFloat(), newHeight.toFloat()) }private fun adjustBounds(width: Float, height: Float) { bounds.set(0f, 0f, width, height) paint.shader = shaderFactory(width, height)}override fun onDraw(canvas: Canvas?) { super.onDraw(canvas) canvas?.drawRect(bounds, paint)}
}
我们现在可以创建一个布局,其中包含4个视图,其中两个在一个黑暗的背景下,两个在一个光的背景下。相同背景的每一对都由一个使用硬件层渲染,一个使用软件层渲染:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.stylingandroid.radialgradient.MainActivity"> <View android:layout_width="0dp" android:layout_height="0dp" android:background="@android:color/black" app:layout_constraintBottom_toBottomOf="@+id/dark_hardware" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <com.stylingandroid.radialgradient.GradientView android:id="@+id/dark_hardware" android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="8dp" android:layerType="hardware" app:layout_constraintBottom_toTopOf="@+id/light_hardware" app:layout_constraintEnd_toStartOf="@+id/dark_software" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <com.stylingandroid.radialgradient.GradientView android:id="@+id/dark_software" android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="8dp" android:layerType="software" app:layout_constraintBottom_toTopOf="@+id/light_software" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/dark_hardware" app:layout_constraintTop_toTopOf="parent" /> <com.stylingandroid.radialgradient.GradientView android:id="@+id/light_hardware" android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="8dp" android:layerType="hardware" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/light_software" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/dark_hardware" /> <com.stylingandroid.radialgradient.GradientView android:id="@+id/light_software" android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="8dp" android:layerType="software" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/light_hardware" app:layout_constraintTop_toBottomOf="@+id/dark_software" /> </android.support.constraint.ConstraintLayout>
如果我们现在在黑暗背景下比较这两个,我们可以看到它们的出现有很大的不同:
使用软件层呈现的是右边,而这一个看起来更适合我想要达到的目标。它褪得稍微快一点,看起来更漂亮。但是,如果我选择使用一个光的背景,我肯定我会选择使用硬件层渲染的那个:
再一次,使用软件层呈现的是右边的一个,现在有一个暗晕,看起来不太好。
为了理解为什么会出现这种差异,让我们先退一步,了解一下硬件和软件层实际上做了什么。
原文地址:http://www.apkbus.com/blog-914653-76499.html
阅读全文
0 0
- 关于Android的径向渐变高级编程的实现
- Css 径向渐变的使用
- CSS3的线性渐变 和 径向渐变。
- CSS渐变效果的使用,线性渐变、径向渐变。
- CSS3径向渐变色的一种
- 利用OpenCV生成关于某点的颜色径向均匀渐变图像
- 利用OpenCV生成关于某点的颜色径向均匀渐变图像
- CSS3高级特性之径向渐变
- radial-gradient CSS3的蛋疼的径向渐变
- radial-gradient CSS3的蛋疼的径向渐变
- CSS3 两种类型的渐变(lineat Gradients线性渐变和Radial Gradients径向渐变)
- Canvas径向渐变createRadialGradient的各种取值研究
- table上下对齐,径向渐变,swiper使用的一个注意事项
- Android渐变标题栏的实现
- android渐变Toolbar的实现
- 径向渐变
- 玩命牛的成长记录(二十四)——线性渐变和径向渐变
- iOS - 关于蒙版渐变的实现
- thinkphp5.0学习(八):模型
- OpenGL简单实现太阳系模拟
- 一起来学习 系统封装接口层- CMSIS-OS 之freeRTOS
- 【java.lang.UnsupportedClassVersionError】版本不一致出错
- apache httpd event worker 笔记1
- 关于Android的径向渐变高级编程的实现
- 数据库及图片文件定期备份
- 虚拟机 centos7 ping不通主机及其他网站
- Neo4j的CQL语法
- C++ escape和unescape算法
- ZooKeeper 入门
- mysql 解压版安装教程
- Mysql四种语句
- mybatis报错 Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements col