在多个视图应用渐变 [英] Applying gradient over multiple views

查看:112
本文介绍了在多个视图应用渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表的布局,看起来像圆形按钮的数字键盘(按键是圆形背景textviews)。现在我想整个键盘采用梯度。但我想梯度可见只上的按钮。按钮周围的空间应是白色的。有什么办法通过XML来实现呢?

I have a table layout that looks like a numeric keypad with circular buttons (the buttons are textviews with round background). Now i want to apply gradient across the entire keypad. but i want the gradient to be visible only on the buttons. the space around the buttons should be white. Is there any way to achieve it through xml?

推荐答案

更​​新:
是的,我能想到的一个办法做到这一点的XML。设置的背景的布局的梯度。然后你需要用白色填充它周围和周围的每个边缘限定的小九补丁(这样一圈不会被拉伸)

Update: Yes, I can think of a way to do that in xml. set the gradient on the background layout. Then create a nine patch drawable image with a transparent circle of the size you need with a white fill around it and a small nine patch defined around each edge (so the circle doesn't get stretched)

设置TextView的背景,你的十个补丁并确保网格单元被拉伸以适应整个表格布局(所以白edgee合并在一起。

Set the TextView background to your nine patch and make sure the grid cells stretch to fit the entire table layout (so the white edgee merge together.

原来的答复:
我想不出一个简单的方法来做到这一点,绝对不是XML。你可能需要创建一个扩展可绘制类,所以可以重写的onDraw方法的类。您需要创建一个的RadialGradient(着色器,而不是一个可绘制),然后每个按钮将创造设为您共同渲染着色器的涂料。

Original answer: I can't think of an easy way to do it and definitely not in xml. You will probably need to create a class that extends the Drawable class so you can override the onDraw method. You would need to create a RadialGradient (the Shader, not a Drawable) and then each button would create a paint with the shader set to your common shader.

然后,当你绘制每个按钮,你需要看一下按钮的y值,由翻译量-y在画布上,然后在ÿ使用着色颜料画一个圆(canvas.drawCircle(..)。这将会使圆在屏幕的同一个地方,与映射仿佛梯度整个页面绘制的颜色渐变,但只看到的地方的按钮。

Then when you draw each button you'd need to look at the y value for the button, translate the canvas by -y amount and then draw a circle (canvas.drawCircle(..) at y using your shader paint. That should put the circle in the same place on screen, with the color gradient mapped as if the gradient was drawn across the entire page, but only visible where the buttons .

这篇关于在多个视图应用渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆