gpt4 book ai didi

android - 用动画填充自定义 View (Android)

转载 作者:太空狗 更新时间:2023-10-29 13:11:47 27 4
gpt4 key购买 nike

我正在尝试创建一个过渡/动画,其中 View 的一部分被不同的颜色填充。

像这样:

enter image description here

最简单的方法是什么?

最佳答案

您可以尝试以下方法:

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View progress = findViewById(R.id.progress);
// from -1f to 0f
float desiredPercentage = -1f;
TranslateAnimation anim = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -1,
Animation.RELATIVE_TO_PARENT, desiredPercentage, Animation.RELATIVE_TO_PARENT,
0, Animation.RELATIVE_TO_PARENT, 0);
anim.setDuration(3000);
anim.setFillAfter(true);
progress.startAnimation(anim);
progress.setVisibility(View.VISIBLE);
}

在布局中:

    <LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:layout_margin="10dp"
android:background="@android:color/holo_green_dark">
<View
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_dark"
android:visibility="invisible"
/>
</LinearLayout>

希望对您有所帮助。 :)

关于android - 用动画填充自定义 View (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39732248/

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