Android: 圆形图像的框架
Android: frame for circle image
我正在努力解决以下问题:
我想实现这个:
我得出以下结论:
<LinearLayout
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_frame_thickness"
app:shapeAppearanceOverlay="@style/CircleImageView" />
</LinearLayout>
<style name="CircleImageView" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
<dimen name="all_time_best_photo_size">70dp</dimen>
<dimen name="all_time_best_frame_thickness">5dp</dimen>
<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>
然后使用 Glide 加载图像本身。这看起来像这样:
戒指形状在哪里?好吧,如果我增加 ImageView
的边距,它就会变得可见:
android:layout_margin="20dp"
和android:layout_margin="15dp"
(不介意颜色)
所以最后一个是我能得到的最接近的,我想我可以 fiddle 增加边距并使其变得更好,但我认为它可能会因不同的设备等而损坏...
我不明白的是为什么使用相同的 dimen
环厚度和 ImageView
边距不起作用。有没有更“安全”的方法来实现这个?
请注意,所有图片的帧大小都是相同的,我只是将屏幕截图裁剪得非常糟糕
我无法用环形获得干净的结果,所以我最后做的是:
<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上创建了问题并通过PR解决了问题,已合并但尚未发布,查看issue and pull request。
如果我设法使用 Ring shape 完成更新,我会 post 更新。
我正在努力解决以下问题:
我想实现这个:
我得出以下结论:
<LinearLayout
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_frame_thickness"
app:shapeAppearanceOverlay="@style/CircleImageView" />
</LinearLayout>
<style name="CircleImageView" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
<dimen name="all_time_best_photo_size">70dp</dimen>
<dimen name="all_time_best_frame_thickness">5dp</dimen>
<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>
然后使用 Glide 加载图像本身。这看起来像这样:
戒指形状在哪里?好吧,如果我增加 ImageView
的边距,它就会变得可见:
android:layout_margin="20dp"
和android:layout_margin="15dp"
(不介意颜色)
所以最后一个是我能得到的最接近的,我想我可以 fiddle 增加边距并使其变得更好,但我认为它可能会因不同的设备等而损坏...
我不明白的是为什么使用相同的 dimen
环厚度和 ImageView
边距不起作用。有没有更“安全”的方法来实现这个?
请注意,所有图片的帧大小都是相同的,我只是将屏幕截图裁剪得非常糟糕
我无法用环形获得干净的结果,所以我最后做的是:
<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上创建了问题并通过PR解决了问题,已合并但尚未发布,查看issue and pull request。
如果我设法使用 Ring shape 完成更新,我会 post 更新。