用动画填充自定义视图 (Android)

3

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

类似于这样:

enter image description here

最简单的方法是什么?

1个回答

4
你可以尝试以下方法:
    @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>

希望这有所帮助。 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接