gpt4 book ai didi

Rounded Corners in an ImageView in Android v31 and below(Android v31及更低版本中ImageView中的圆角)

转载 作者:bug小助手 更新时间:2023-10-28 11:53:32 26 4
gpt4 key购买 nike



I'm trying to put make an ImageView's corners rounded with pure xml in an Home Screen widget. I am using android:clipToOutline="true" in v31 and higher but as my app supports lower, I couldn't use it all the time. This is my code:

我正在尝试将Make an ImageView的角用纯XML舍入到一个Home Screen窗口小部件中。我在v31和更高版本中使用android:clipToOutline=“true”,但由于我的应用程序支持更低版本,我不能一直使用它。这是我的代码:


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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="9dp"
android:background="@drawable/widget_background"
android:padding="9dp"
android:id="@+id/widget_container"
android:orientation="horizontal">

<TextView
android:id="@+id/default_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="25sp"
android:textStyle="bold"
android:gravity="center"
android:textColor="#E9E9E9"
tools:text="Nothing yet!" />

<ImageView
android:id="@+id/scribb_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:background="@drawable/image_background"
android:contentDescription="@string/image" />
</LinearLayout>

image_background.xml:

IMAGE_BACKERGRONG.xml:


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="9dp"/>
</shape>

This is run with a Flutter project so I try not to use too many dependencies.

这是一个颤动项目,所以我尽量不使用太多的依赖项。


更多回答
优秀答案推荐

Note : Given approach is kind of a hack, use if you don't find a better solution.

注意:给定的方法是一种黑客,如果你找不到更好的解决方案,请使用。


Create a frame drawable with required corner radius and transparent background. The color of the frame should match the background of the root parent. Then apply this frame as an overlay to your image view.

创建一个框架绘制所需的角半径和透明的背景。框架的颜色应该与根父级的背景相匹配。然后将此帧作为覆盖应用到图像视图。


Since the frame background is transparent, you will be able to see the bottom image, but since the frame border color matches you screen color, it hides the image at that part making image to appear as curved edges.

由于框架背景是透明的,您将能够看到底部图像,但由于框架边框颜色与您的屏幕颜色匹配,因此它隐藏了使图像显示为弯曲边缘的部分的图像。


Drawable Code : I created a required image in image editor and then converted it to
vector using this site.

可绘制代码:我在图像编辑器中创建了所需的图像,然后使用此站点将其转换为矢量。


<vector xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:aapt="http://schemas.android.com/aapt"
android:viewportWidth="474"
android:viewportHeight="469"
android:width="200dp"
android:height="200dp">
<path
android:pathData="M0 0L474 0L474 469L0 469L0 0ZM66 3Q64 6 58 4L45 8Q33 13 25 22Q9 36 5 61L5 410L8 424L19 443Q33 462 61 467L414 467Q432 464 444 454Q455 444 463 431L467 421L469 410L469 61L464 43Q459 32 451 24Q442 14 430 8L417 4Q410 6 409 3L66 3Z"
android:fillColor="#000000"
android:strokeColor="#000000"
android:strokeWidth="1" />
</vector>

XML Code : You can use backgroundTint property to change color of your drawable. You may also want to use RelativeLayout or ConstraintLayout to properly align the overlay view on top of the image view.

可扩展标记语言代码:您可以使用BackageTint属性来更改可绘制的颜色。您可能还希望使用RelativeLayout或ConstraintLayout来正确对齐图像视图顶部的叠加视图。


<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"
android:background="@color/background_color">

<ImageView
android:id="@+id/image_view"
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:src="@drawable/ic_launcher_background" />

<View
android:id="@+id/circular_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@id/image_view"
android:layout_alignTop="@id/image_view"
android:layout_alignEnd="@id/image_view"
android:layout_alignBottom="@id/image_view"
android:background="@drawable/bg"
android:backgroundTint="@color/background_color" />

<ImageView
android:id="@+id/image_view_without_overlay"
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_centerVertical="true"
android:layout_toEndOf="@id/image_view"
android:src="@drawable/ic_launcher_background" />
</RelativeLayout>

Drawable:

可绘制的:


Drawable Output


Sample Output:

示例输出:


Sample Output


Drawbacks: The above approach is has some drawbacks, like you cannot customize the radius of the curve easily. And, if you want this frequently you will end up with double the number of views for every image view, making the layout complex.

缺点:上面的方法有一些缺点,比如你不能轻松地定制曲线的半径。而且,如果您经常想要这样做,每个图像视图的查看次数将增加一倍,从而使布局变得复杂。


Better Approach : A better approach will be to create a custom view, with the same idea as above, extend the base ImageView class and draw a border outside it using Paint and Canvas. With custom view you can easily customize it later, including radius and color. Also, you layout will become simpler since instead of ImageView with you can use your custom view. You can read about custom views here.

更好的方法:更好的方法是创建一个自定义视图,与上面的想法相同,扩展基本的ImageView类,并使用油漆和画布在其外部绘制一个边框。使用自定义视图,您可以在以后轻松地自定义它,包括半径和颜色。此外,您的布局将变得更简单,因为您可以使用您的自定义视图而不是ImageView。您可以在此处阅读有关自定义视图的内容。


更多回答

This was very helpful, thank you. I use RelativeLayout to add an another layer of radius box on top of the image.

这对我很有帮助,谢谢。我使用RelativeLayout在图像顶部添加另一层半径框。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com