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

查看:42
本文介绍了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>

如果你能想象,这个渐变在中间有一个半透明的白色光晕,然后在边缘渐变到 alpha 0.我需要让它变成椭圆形,而不仅仅是圆形渐变.我怎样才能做到这一点?

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?

推荐答案

这很困难,因为以这种方式定义的可绘制对象在运行时自行绘制,以适应您放置它们的空间.如果您必须在代码,将采用您在 XML 中定义的可绘制形状并将其绘制到 Canvas 或 Bitmap 中作为一个完美的圆圈.此时,渐变图案将遵循形状轮廓.一旦将形状绘制到静态上下文中,您就可以将形状添加到视图中(例如作为背景),当它尝试适应视图边界时,它会将其扭曲成椭圆形.既然你有一个图像,整个事情就会成比例地扭曲.

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天全站免登陆