'Fill Custom View with animation (Android)

I'm trying to create a transition/animation where a portion of the view gets filled by a different color.

Something like this:

enter image description here

What would be the easiest way to do this?



Solution 1:[1]

You could try the following:

    @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);
    }

And in the layout:

    <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>

Hope it helps. :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kishan Mevada