在Android的椭圆形渐变 [英] Oval Gradient in Android

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

问题描述

我知道如何设置和显示一个椭圆形。我知道如何渐变适用于这种形状。我无法弄清楚是怎么可以得到一个椭圆形的渐变相匹配的形状。

I know how to setup and display an oval shape. I know how to apply a gradient to this shape. What I cant figure out is how I can get an oval gradient to match the shape.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval" >
    <gradient
        android:startColor="#66FFFFFF"
        android:endColor="#00FFFFFF"
        android:gradientRadius="100"
        android:type="radial" />
</shape>

如果你能想象,这个梯度有一个半透明的白色光晕在中间,然后淡出阿尔法零的边缘。我需要得到它出去为椭圆形,而不仅仅是一个圆形的渐变。我怎样才能做到这一点?

If you can imagine, this gradient has a semi transparent white glow in the middle, then fades to alpha zero at the edges. I need to get it to go out in an oval shape, not just a circular gradient. How can I achieve this?

推荐答案

这是困难的,因为以这种方式定义的可绘制绘制自身在运行时,适应你把它们的空间。你最好的解决办法,如果你必须这样做code,将带你已经在XML定义的形状可绘制并绘制到画布或成位图作为一个完美的圆。在这点上,梯度图案将遵循的形状轮廓。一旦形状已被卷入一个静态背景下,你可以添加形状的视图(作为背景,比方说),当它试图适应范围来看,这将扭曲成一个椭圆形。既然你有一个形象,整个事情就会扭曲成比例。

This is difficult since drawables defined in this fashion draw themselves at runtime, adapting to the space you put them in. Your best solution, if you must do this in code, would be to take the shape drawable you have defined in XML and draw it onto a Canvas or into a Bitmap as a perfect circle. At this point, the gradient pattern will follow the shape outline. Once the shape has been drawn into a static context you can add the shape to a view (as a background, let's say), which will distort it into an oval when it tries to fit the view bounds. Since you have an image, the whole thing will distort proportionately.

但愿它不会像素太差用这种方法。

Hopefully, it won't pixel too bad with this method.

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

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