【问题标题】:Shadow displaying as square on FloatingActionButton Glide V4FloatingActionButton Glide V4 上的阴影显示为正方形
【发布时间】:2017-07-10 21:36:02
【问题描述】:

我最近将 Glide 升级到版本 4,这导致我在浮动操作按钮中显示的图像出现问题。我尝试了 2 种方法,并且对这两种方法都使用了相同的 XML:

XML:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/profilepic_fab"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:scaleType="center"
    app:layout_anchor="@id/appbar"
    app:layout_anchorGravity="bottom|center_horizontal"
    app:srcCompat="@mipmap/bookmark_checked_white" />

方法#1:

            RequestOptions optionsCentre = new RequestOptions()
                    .placeholder(R.drawable.profilepic_placeholder)
                    error(R.drawable.profilepic_placeholder).dontTransform()
                    .dontAnimate().CircleCrop();

            Glide.with(context)
                    .load(userinfo.getprofilePic())
                    .apply(optionsCentre)
                    .into(profilepic);

方法 #1 的结果:在 Android 7.1.2 上看起来不错,但在 4.4 上看起来像下图:

方法#2:

RequestOptions options = new RequestOptions()
                .fitCenter()
                .placeholder(R.drawable.profilepic_placeholder)
                .error(R.drawable.profilepic_placeholder)
                .priority(Priority.HIGH);

    Glide.with(context)
            .asBitmap()
            .apply(options)
            .load(url)
            .into(new BitmapImageViewTarget(fab) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                    circularBitmapDrawable.setCircular(true);
                    fab.setImageDrawable(circularBitmapDrawable);
                }
            });

方法 #2 的结果:结果在 Android 7.1.2 和 4.4 上看起来不同

Android 7.1.2:

Android 4.4:

方法 #2 是用于 Glide v3 的方法...任何帮助将不胜感激!

【问题讨论】:

    标签: android android-view floating-action-button android-glide


    【解决方案1】:

    FloatingActionButton 不能采用任意宽度/高度值,而是使用 app:fabSize 属性从 3 种可用尺寸中提供输入尺寸:自动、迷你和普通。

    将FAB 的layout_widthlayout_height 指定为wrap_content,并使用app:fabSize="normal"(或列表中的其他参数)提供所需的晶圆厂尺寸。

    这个问题类似于this one

    【讨论】:

    • 问题是,当我使用 Glide 3.7/3.8 时,它曾经能够接受任意宽度/高度值。我想我只需要使用图像视图和自定义阴影。
    • 您为什么使用 FAB?使用 ImageView。
    • 我使用它是因为我是一个带有渐变阴影的圆形 ImageView
    【解决方案2】:

    使用FloatingActionButton,不可能在所有 Android 版本上实现您的预​​期行为。

    代替FloatingActionButton,使用ImageViewPicassoGlide 等第三方库来加载和转换您的Image 资源。您也可以使用CircleImageView 库。

    解决方案 1:

    ImageViewPicasso 一起使用。

    Gradle:

    dependencies {
        .........
        compile 'com.squareup.picasso:picasso:2.5.2'
    }
    

    用法:

    XML

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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="match_parent">
    
        <ImageView
            android:id="@+id/image_header"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@drawable/dummy_background"/>
    
        <ImageView
            android:id="@+id/image_profile_pic"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginTop="125dp"
            android:layout_centerHorizontal="true"/>
    
    </RelativeLayout>
    

    JAVA

    ImageView imageProfilePic = (ImageView) findViewById(R.id.image_profile_pic);
    Picasso.with(this)
            .load(R.drawable.dummy_profile_pic)
            .resize(150, 150)
            .centerCrop()
            .transform(new CircleTransform())
            .into(imageProfilePic);
    

    输出:

    解决方案 2:

    ImageViewGlide 一起使用。

    Gradle:

    dependencies {
        .........
        compile 'com.github.bumptech.glide:glide:4.0.0-RC1'
    }
    

    用法:

    XML

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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="match_parent">
    
        <ImageView
            android:id="@+id/image_header"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@drawable/dummy_background"/>
    
        <ImageView
            android:id="@+id/image_profile_pic"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginTop="125dp"
            android:layout_centerHorizontal="true"/>
    
    </RelativeLayout>
    

    JAVA

    ImageView imageProfilePic = (ImageView) findViewById(R.id.image_profile_pic);
    
    
    Glide.with(this)
            .load(R.drawable.dummy_profile_pic)
            .apply(RequestOptions.circleCropTransform())
            .into(imageProfilePic);
    

    输出:

    解决方案 3:

    使用CircleImageView 库。

    Gradle:

    dependencies {
        .........
        compile 'de.hdodenhof:circleimageview:2.1.0'
    }
    

    用法:

    XML

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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="match_parent">
    
        <ImageView
            android:id="@+id/image_header"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@drawable/dummy_background"/>
    
        <de.hdodenhof.circleimageview.CircleImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/image_profile_pic"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginTop="125dp"
            android:layout_centerHorizontal="true"        
            app:civ_border_width="2dp"
            app:civ_border_color="#FFFFFF"/>
    
    </RelativeLayout>
    

    JAVA

    CircleImageView imageProfilePic = (CircleImageView) findViewById(R.id.image_profile_pic);
    
    Picasso.with(this)
            .load(R.drawable.dummy_profile_pic)
            .into(imageProfilePic);
    

    输出:

    希望对你有帮助~

    【讨论】:

    • 感谢详细的解决方案!我不得不用 ImageView 来实现它,但我很难得到一个有渐变的阴影。我只能在 ImageView 周围制作实心灰色边框,而不是阴影,因为不透明度向外降低。有什么想法吗?
    猜你喜欢
    • 2015-12-01
    • 1970-01-01
    • 2016-10-14
    • 2012-11-14
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多