android渐变布局,关于Android的径向渐变高级编程的实现
在最近的一系列文章,对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个视图,其中两个在一个黑暗的背景下,两个在一个光的背景下。相同背景的每一对都由一个使用硬件层渲染,一个使用软件层渲染:
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">
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" />
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" />
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" />
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" />
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渐变布局,关于Android的径向渐变高级编程的实现相关推荐
- android圆角布局阴影,Android 布局阴影实现
最近项目要求,ui有很多有关于阴影的设计要求,网上找了些实现方式,但都不是很理想.现在闲下来了,就寻思着自己写个阴影布局耍耍,以备后用.先说道说道我找到的几种阴影实现方式: 系统阴影 Andorid ...
- android 线性布局位置,android – 如何在线性布局中更改视图的位置.
所以我有一个已经填充了子项的线性布局.有没有办法改变其中一个孩子的位置? 如果有任何帮助的话,我正试图交换他们之间的观点. final LinearLayout parrent = (LinearLa ...
- android 获取布局textview,android – 获取TextView中文本的位置
看看几个Paint方法: getTextBounds()和 measureText.我们可以使用它们来确定TextView中文本的偏移量.确定TextView中的偏移后,我们可以将其添加到TextVi ...
- android局部布局替换,Android 局部布局替换的实现方式
最近再搞远程视频的功能,其中要实现加载视频.加载视频失败.加载成功的局部布局替换,查阅相关资料,找到一种投机取巧的方式. 首先分别写这三种效果的子布局,分别为 top_remotetreate.xml ...
- android ui布局适配,Android适配全面总结(一)----屏幕适配
前言 Android适配是一个老生常谈的问题,很多程序员觉得很恶心,不愿意做适配,但是又不得不做.然后老板说,这位兄弟,做好了,今天晚饭给你加个鸡腿,然后程序员开始找各种资料,忙活起来了,最终在苦逼的 ...
- android滚动条布局横向,Android自定义ViewGroup实现可滚动的横向布局(2)
这里直接代码: package com.example.libingyuan.horizontallistview.ScrollViewGroup; import android.content.Co ...
- android相对布局代码,Android基础_3 Activity相对布局(示例代码)
相对布局要比前面讲的线性布局和表格布局要灵活一些,所以平常用得也是比较多的.相对布局控件的位置是与其周围控件的位置相关的,从名字可以看出来,这些位置都是相对的,确定出了其中一个控件的位置就可以确定另一 ...
- android 首页布局变换,Android XML布局与View之间的转换
Android的布局方式有两种,一种是通过xml布局,一种是通过java代码布局,两种布局方式各有各的好处,当然也可以相互混合使用.很多人都习惯用xml布局,那xml布局是如何转换成view的呢?本文 ...
- android百分比布局失效,Android 百分比布局库【原创】
为了解决android手机适配问题,我们经常想如果可以按照百分比的方式进行界面布局,这样适配各种屏幕就简单多了吧!现在谷歌正式提供百分比布局支持库(android-support-percent-li ...
最新文章
- HDU 4391 Paint The Wall 段树(水
- Tornado学习笔记(四)
- Codeforces 1254C/1255F Point Ordering (交互题)
- python读txt文件 数组-python将txt等文件中的数据读为numpy数组的方法
- 软件工程作业之小学数学题目
- 你的Wi-Fi 还安全吗?全球重大漏洞WPA2 KRACK 详细分析报告
- 修改 VS2013 项目属性的默认包含路径(全局)
- 数据挖掘算法_技术分享|大数据挖掘算法之KNNk近邻算法
- SetWindowHookEx 做消息响应
- 精和泛的一点思考(跨学科思维)
- Selenium webdriver实现原理
- win10u盘被写保护怎么解除_win10系统U盘提示磁盘被写保护的解决办法
- 开源图像数据集管理工具fiftyone使用
- 我努力了十年,才让我的老婆不上班
- Java常用类之String类知识清单,简单易理解
- python学习-循环替换txt文件中的指定字符
- Joda-Time 简介
- 【机器学习】机器学习30个笔试题
- AICodeHelper - AI编程助手
- 网络通信之如何广播发送
热门文章
- HDU 5552 Bus Routes(NTT+分治)
- WireShark 查看UDP码流的丢包率
- AJAX异步实现简单的瀑布流
- 给radio添加点击事件
- textAppearance的属性设置
- POJ 1664 放苹果(递归或DP)
- LETTers比赛第三场 --1002 Ignatius and the Princess III解题报告
- ServerSocketChannel的使用例子
- python遍历目录树 除文件_python遍历目录树删除指定后缀的文件
- python opencv高斯滤波_【OpenCV】基于Python的图像高斯平滑和椒盐噪声处理 | 学步园...