在 Android 中以 XML 定义形状时,是否有与 setShadowLayer 等效的方法?

发布于 2024-09-27 18:19:45 字数 1454 浏览 3 评论 0原文

如果我通过代码绘制圆形矩形,我可以使用 setShadowLayer 为形状绘制阴影。在 XML 中定义形状时是否有等效的?

以下示例将圆形矩形背景绘制为形状。我需要添加什么才能将阴影添加到形状中?甚至可以使用 XML 吗?

shape_test.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#8067BF6A"/>    
    <stroke android:width="3dp" android:color="#80000000" />
    <padding android:left="1dp"
      android:top="1dp"
      android:right="1dp"
      android:bottom="1dp" /> 
    <corners android:bottomRightRadius="7dp"
      android:bottomLeftRadius="7dp"
      android:topLeftRadius="7dp"
      android:topRightRadius="7dp"/> 
</shape>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:padding="10dp"
 android:background="#ffdddddd"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <Button android:background="@drawable/shape_test"
 android:padding="5dp"
 android:textStyle="bold"
 android:textSize="28sp"
 android:text="Hello"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
</LinearLayout>

If I draw a round rect shape by code I can use setShadowLayer to get a shadow drawn for the shape. Is there an equivalent when defining shapes in XML?

The following example draws a round rect background to a shape. What would I need to add to get a shadow added to the shape? Is it even possible using XML?

shape_test.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#8067BF6A"/>    
    <stroke android:width="3dp" android:color="#80000000" />
    <padding android:left="1dp"
      android:top="1dp"
      android:right="1dp"
      android:bottom="1dp" /> 
    <corners android:bottomRightRadius="7dp"
      android:bottomLeftRadius="7dp"
      android:topLeftRadius="7dp"
      android:topRightRadius="7dp"/> 
</shape>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:padding="10dp"
 android:background="#ffdddddd"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <Button android:background="@drawable/shape_test"
 android:padding="5dp"
 android:textStyle="bold"
 android:textSize="28sp"
 android:text="Hello"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍白女子 2024-10-04 18:19:45

据我所知,使用 XML 还没有真正的方法可以做到这一点。我看到了一些建议,在物品后面制作第二个相同形状的盒子,然后用黑色填充它,但我认为这不是一个好的解决方案。一段时间以来,我一直在尝试寻找一种方法来自己做到这一点。

如果有帮助,这里有一个链接,指向我发布的类似问题,以及一些代码。我让它适用于一些图像,但有时似乎仍然无法找到 Alpha 通道。基本上,我已经重写了 ImageView 并将其放入 onDraw() 方法中:

@Override 
protected void onDraw(Canvas canvas)  
{ 
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.omen); 
    Paint paint = new Paint(); 
    paint.setAntiAlias(true); 
    paint.setShadowLayer(5.5f, 6.0f, 6.0f, Color.BLACK); 
    canvas.drawColor(Color.GRAY); 
    canvas.drawRect(50, 50, 50 + bmp.getWidth(), 50 + bmp.getHeight(), paint); 
    canvas.drawBitmap(bmp, 50, 50, null);        
} 

不过,这只是测试,因此显然很多参数必须更加通用。虽然我最近没有太多时间来研究它,但也许这会帮助你找到答案。

Using XML, there's no real way to do this that I know of. I've seen some suggestions of making a second box of the same shape behind the item, and just filling it with black, but I don't feel that's a good solution. I've been trying to find a way to do this myself for a while.

If it helps, here's a link to a similar question I posted, along with some code. I got it working for some images, but it still seems to have trouble finding the alpha channel sometimes. Basically, I've overridden ImageView and put this in the onDraw() method:

@Override 
protected void onDraw(Canvas canvas)  
{ 
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.omen); 
    Paint paint = new Paint(); 
    paint.setAntiAlias(true); 
    paint.setShadowLayer(5.5f, 6.0f, 6.0f, Color.BLACK); 
    canvas.drawColor(Color.GRAY); 
    canvas.drawRect(50, 50, 50 + bmp.getWidth(), 50 + bmp.getHeight(), paint); 
    canvas.drawBitmap(bmp, 50, 50, null);        
} 

That was just testing, though, so obviously a lot of the parameters would have to be more generic. I haven't had a lot of time to work on it lately though, but maybe this will help you find your answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文