【发布时间】:2021-01-18 23:35:50
【问题描述】:
我正在尝试在 android studio 中创建一个可以扩展和缩小的圆圈。我只想使用一个 Java 类和一个布局 xml 文件。有关如何执行此操作的任何建议?
【问题讨论】:
-
通常人们会展示一些他们迄今为止尝试过的代码......
标签: android android-studio animation android-animation android-xml
我正在尝试在 android studio 中创建一个可以扩展和缩小的圆圈。我只想使用一个 Java 类和一个布局 xml 文件。有关如何执行此操作的任何建议?
【问题讨论】:
标签: android android-studio animation android-animation android-xml
希望这会有所帮助: 如果你想创建圆形,我可以建议你创建一个椭圆形。
首先在你的drawable包中创建一个XML文件,右键单击它并选择一个新的资源文件,给那个文件命名让我们假设bg_circle。
<shape android:shape="oval"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorPrimary"/>
<stroke android:color="@color/colorAccent" android:width="2dp"/>
</shape>
这将创建一个椭圆形,您可以根据需要从 color.xml 文件中更改颜色。 现在,您需要做的是您必须在 imageView 中添加一个属性,即
android: src = "@drawable/bg_circle".
如果您不需要圆的轮廓,您可以从 bg_circle.xml 中删除笔触颜色
【讨论】:
有几种方法可以做到这一点。 一个选项是在您的 xml 布局中的 CardView 内使用 ImageView 并为其设置动画,这是一个带有黑色圆圈的最小示例:
<androidx.cardview.widget.CardView
android:id="@+id/cv_round"
android:layout_width="40dp"
android:layout_height="40dp"
app:cardCornerRadius="20dp"> // -> this makes it look circular
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black" />
</androidx.cardview.widget.CardView>
请注意,这并不是一个真正的圆,它只是看起来像一个。
在 Java 代码中转换 CardView 后:
CardView cardViewCircular = (CardView) findViewById(R.id.cv_round);
展开/放气动画是一个带有ViewPropertyAnimator f.e. 的单行。 (扩展 50f):
cardViewCircular.animate().scaleYBy(50f).scaleXBy(50f); //deflating is -50f
或使用 scaleX(50f) / scaleY(50f) 方法,哪个更适合您。
【讨论】: