美文网首页
背景加透明度

背景加透明度

作者: jiluyixia | 来源:发表于2020-08-26 17:21 被阅读0次

有时候需要给背景加透明度,基本上有一下三种情况。


00000.png
111.png
2222.png

第一种情况。
当不支持配送时,在货物的背景图上加个透明度,并且,还要在上层加个不支持配送图片。
首先考虑货物图用src,半透明背景加一个background。
eg:

<ImageView
                android:id="@+id/iv_coupones_geted"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#66FFFFFF"
                android:paddingTop="@dimen/dp_24"
                android:paddingBottom="@dimen/dp_24"
                android:src="@drawable/coupones_geted"
                android:visibility="gone"
                tools:visibility="visible" />

在满足不支持配送的时候,动态加background和不支持图片。
但是这个货物图片是网络图片,不能用src。

Glide.with(context).load(url).fitCenter().into(imageView);

想到一种笨办法:

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <com.yijiago.ecstore.widget.RatioImageView
        android:id="@+id/iv_goods_picture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        app:riv_ratio="1"
        tools:src="@drawable/app_icon" />

        <ImageView
            android:id="@+id/iv0002"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            android:background="#CFFF"/>


        <com.yijiago.ecstore.widget.RatioImageView
            android:id="@+id/iv_goods_picture2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone"
            android:background="@drawable/no_distribution" />

    </FrameLayout>

用了三层,第一层货物,第二层半透明,第三层不支持配送图片。

第二种情况。
背景图是固定的,好像可以满足src+background的设定。
但是很可惜,这个背景图需要作为RelativeLayout或者LinearLayout当做布局控件的。
而LinearLayout和RelativeLayout并没有src属性。
如果像刚才第一种情况那样,透明背景的宽高不好设置,因为这个背景图有空白区域。
在网上找到一种被背景图设置透明度的方法:

holder.coupon_rl.getBackground().setAlpha(130)

此种方法看起来可行,但是他是把背景图获取到,再给他添加一个透明度,而这个RelativeLayout上的控件并没有改变。

4444.png
很明显不符合要求,偶尔看到一篇文章:
Android背景设置透明和半透明效果
里面有个:
title.setAlpha(0.2f);  

这个是针对整个view加透明度。
将holder.coupon_rl.getBackground().setAlpha(130)改为

holder.coupon_rl.setAlpha(0.5f);

就到达要求了。

第三种情况。
此种情况是背景图的一部分半透明。
这个一部分,是以虚线分界的,可以用画图工具打开,将鼠标放在虚线上,看下面的像素是多少,可以算出比例。
先按这个比例写好左右布局,再在左边加个半透明背景就可以了。具体代码如下(用了src+background):

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/coupon_rl"
        android:layout_marginTop="@dimen/dp_4"
        android:background="@drawable/market_coupon_bg"
        android:orientation="horizontal">

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="470">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical"
                android:paddingLeft="@dimen/dp_10"
                android:paddingTop="@dimen/dp_16"
                android:paddingRight="@dimen/dp_10">


        <TextView
            android:id="@+id/coupon_desc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="@dimen/dp_8"
            android:textColor="@color/color_ff3a49"
            android:textSize="@dimen/sp_15"
            android:textStyle="bold"
            tools:text="全场商品,可抵用现金60元" />

        <TextView
            android:id="@+id/cansend_start_time"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="@dimen/dp_16"
            android:textColor="@color/color_666666"
            android:textSize="@dimen/sp_11"
            tools:text="有效期:2020.03.12-2020.03.12"/>

        <TextView
            android:id="@+id/shop_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/dp_8"
            android:paddingBottom="@dimen/dp_8"
            android:text="限阿迪达斯服饰店可用"
            android:textColor="@color/color_666666"
            android:textSize="@dimen/sp_12" />

            </LinearLayout>

            <ImageView
                android:id="@+id/iv_coupones_geted"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#66FFFFFF"
                android:paddingTop="@dimen/dp_24"
                android:paddingBottom="@dimen/dp_24"
                android:src="@drawable/coupones_geted"
                android:visibility="gone"
                tools:visibility="visible" />

        </FrameLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="240"
            android:gravity="center"
            android:orientation="vertical">

        <TextView
            android:id="@+id/deduct_money"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="@dimen/dp_8"
            android:textColor="@color/red"
            android:text="¥100"
            android:textStyle="bold"
            android:textSize="22sp" />

            <com.yijiago.ecstore.widget.StateButton
                android:id="@+id/obtain_time"
                android:layout_width="wrap_content"
                android:layout_height="@dimen/dp_24"
                android:minWidth="0dp"
                android:minHeight="0dp"
                android:paddingLeft="@dimen/dp_16"
                android:paddingTop="@dimen/dp_0"
                android:paddingRight="@dimen/dp_16"
                android:paddingBottom="@dimen/dp_0"
                android:text="立即领取"
                android:textSize="@dimen/dp_12"
                android:textStyle="bold"
                app:normalBackgroundColor="@color/color_ff3a49"
                app:normalTextColor="@color/color_white"
                app:radius="@dimen/dp_12" />

        </LinearLayout>

    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/applicable_stores_list"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

参考资料:
Android 背景透明度设置总结
Android背景设置透明和半透明效果

相关文章

网友评论

      本文标题:背景加透明度

      本文链接:https://www.haomeiwen.com/subject/nphfsktx.html