【问题标题】:How to make custom rectangle shape with hex color of any color with opacity?如何使用不透明度的任何颜色的十六进制颜色制作自定义矩形形状?
【发布时间】:2022-01-06 08:01:51
【问题描述】:

有没有可能用任何十六进制颜色绘制一个矩形形状,该颜色不透明

例如 blue_color_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid
        android:color="#29618E"/>
    <corners
        android:radius="@dimen/_10sdp"/>

</shape>

上面的代码是一个半径为10dp的普通矩形

但我希望在不更改十六进制代码的情况下为形状提供给定的不透明度。以及如何实现这一目标

【问题讨论】:

    标签: android kotlin android-drawable opacity android-shape


    【解决方案1】:

    如果您不想更改颜色,可以将android:alpha 属性添加到您的视图中。例如:

    <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/blue_color_backgroung"
        android:alpha="0.5"/>
    

    【讨论】:

    • 我只是使用上面的 blue_color_background.xml 作为视图的背景。这不是我正在寻找的确切答案。感谢您的评论
    • @SumanRadhakrishnan 你有你在找什么的照片吗?
    • @LouisChabert [prnt.sc/20z0j12] 这是示例图片
    【解决方案2】:

    android XML 中的 opacity 属性是 alpha,您可以在 0.0 到 1.0 的范围内为视图设置 alpha,其中 0.0 是完全透明的。 像这样使用:

    android.alpha="0.7"
    

    【讨论】:

    • 将 alpha 设置为父视图也会影响子视图。不是我正在寻找的确切答案
    【解决方案3】:
    Drawable customeShape = 
    getResources().getDrawable(R.drawable.custom_shape);
    
     // setting the opacity (alpha)
    
    customeShape.setAlpha(10);
    
    // setting the images on the ImageViews
    
    image.setImageDrawable(customeShape);
    

    【讨论】:

    • 对于您的种类信息 getDrawable(R.drawable.custom_shape) 已弃用。
    猜你喜欢
    • 2011-03-11
    • 2013-03-28
    • 2011-12-27
    • 2021-10-06
    • 2014-09-22
    • 2019-04-19
    • 1970-01-01
    • 2020-08-12
    • 2020-12-25
    相关资源
    最近更新 更多