【问题标题】:Android: drawable ring shape multi-gradientAndroid:可绘制的环形多梯度
【发布时间】:2020-11-19 10:24:35
【问题描述】:

我正在尝试实现以下目标:

里面的图片问题不大,但戒指似乎是一个。我能够做到这一点:

使用:

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:thickness="@dimen/all_time_best_frame_thickness"
    android:useLevel="false"
    >
    <gradient
        android:startColor="@color/gold_gradient_start"
        android:centerColor="@color/gold_gradient_end"
        android:endColor="@color/gold_gradient_start"
        android:angle="180" />
</shape>

如您所见,这只需要 3 种颜色。所以我尝试以编程方式进行:

val ring = GradientDrawable(
            GradientDrawable.Orientation.LEFT_RIGHT, intArrayOf(
                ContextCompat.getColor(requireContext(), R.color.gold_gradient_1),
                ContextCompat.getColor(requireContext(), R.color.gold_gradient_2),
                ContextCompat.getColor(requireContext(), R.color.gold_gradient_3),
                ContextCompat.getColor(requireContext(), R.color.gold_gradient_4),
                ContextCompat.getColor(requireContext(), R.color.gold_gradient_5)
            )
        )

            ring.shape = GradientDrawable.OVAL
            ring.thickness = resources.getDimension(R.dimen.all_time_best_frame_thickness).toInt()
            binding.goldFrame.background = ring

这并没有画出任何东西,所以我确定我错过了一些东西。此外,ring.thickness 仅在 API 29 上可用,而我的最小值是 23。所以我需要找到一些替代方案。

布局是:

<LinearLayout
                        android:id="@+id/gold_frame"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/drawable_gold_frame">

                        <com.google.android.material.imageview.ShapeableImageView
                            android:id="@+id/gold_photo"
                            android:layout_width="@dimen/all_time_best_photo_size"
                            android:layout_height="@dimen/all_time_best_photo_size"
                            android:layout_margin="@dimen/all_time_best_photo_margin"
                            app:shapeAppearanceOverlay="@style/CircleImageView" />
                    </LinearLayout>

知道如何解决这个问题吗?

【问题讨论】:

    标签: android android-drawable android-shape


    【解决方案1】:

    我无法用环形来实现它,所以我最终做的是:

    <FrameLayout
                        android:id="@+id/gold_frame"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
    
                        <com.google.android.material.imageview.ShapeableImageView
                            android:id="@+id/gold_photo"
                            android:layout_width="@dimen/all_time_best_photo_size"
                            android:layout_height="@dimen/all_time_best_photo_size"
                            android:layout_margin="@dimen/all_time_best_frame_thickness"
                            app:shapeAppearanceOverlay="@style/CircleImageView" />
                    </FrameLayout>
    

    val medal = GradientDrawable(
                GradientDrawable.Orientation.LEFT_RIGHT, intArrayOf(
                    ContextCompat.getColor(requireContext(), R.color.gold_gradient_1),
                    ContextCompat.getColor(requireContext(), R.color.gold_gradient_2),
                    ContextCompat.getColor(requireContext(), R.color.gold_gradient_3),
                    ContextCompat.getColor(requireContext(), R.color.gold_gradient_4),
                    ContextCompat.getColor(requireContext(), R.color.gold_gradient_5)
                )
            )
    
                medal.shape = GradientDrawable.OVAL
                binding.goldFrame.background = medal
    

    这样我创建了一个内部带有渐变的圆圈,然后将其设置为背景作为ImageView 的容器。不完美,因为如果您想要的图像具有透明度,它就不起作用,但在我的情况下有效。

    此外,还可以通过移除容器并执行以下操作来改进:

    <com.google.android.material.imageview.ShapeableImageView
        android:id="@+id/gold_photo"
        android:layout_width="@dimen/all_time_best_photo_size"
        android:layout_height="@dimen/all_time_best_photo_size"
        android:padding="@dimen/all_time_best_frame_thickness"
        app:shapeAppearanceOverlay="@style/CircleImageView" />
    

    并将背景设置为ImageView 本身,不幸的是ShapeableImageView 根据填充缩放背景(标准ImageView 不这样做)。

    一位用户在github上创建了issue,通过PR解决了这个问题,已经合并但还没有发布,查看issuepull request

    如果我设法使用环形来完成更新,我会发布更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多