Android:圆形图像的框架
我正在努力解决以下问题:Android:圆形图像的框架,android,imageview,drawable,Android,Imageview,Drawable,我正在努力解决以下问题: <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/drawable_gold_frame"> <com.google.android.material.imageview.Sha
<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>
我想做到这一点:
我得出以下结论:
<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>
android:layout\u margin=“15dp”(不介意颜色)
所以最后一个是我能得到的最接近的,我想我可以摆弄一下利润,使它更好,但我认为它可能会打破不同的设备等
我不明白的是为什么使用相同的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>
通过这种方式,我创建了一个内部带有渐变的圆,然后将其设置为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解决了这个问题,PR已经被合并,但尚未发布,请检查并重试
我会发布一个更新,如果我曾经设法做它使用环形状
<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" />