animate removeAllViews when recreating view

Asked

Viewed 29 times

0

I have a scrollview with 6 buttons that change the background according to a previously chosen numberpicker, When I change the number of the Picker number and it removes the existing buttons and creates again with the correct background for that chosen number, all right so far, the problem is that when he does this reconstruction the scrollview flashes, and it looks really ugly, looks like a Glitch, would have to animate this re-creation of buttons?

My way:

    private void criardias(int diapreparacao) {

    layoutbtns.removeAllViewsInLayout();


    for (int i = 1; i <= 6 ; i++) {
        FancyButton diabtn = new FancyButton(getContext());
        diabtn.setText("Dia " + i);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(135, 135);
        layoutParams.setMargins(8, 0, 8, 0);
        diabtn.setLayoutParams(layoutParams);
        diabtn.setRadius(64);
        diabtn.setTextSize(15);
        diabtn.setRadius(64);
        diabtn.setBackground(getResources().getDrawable(R.drawable.selectorbtndia));

        if (i == diapreparacao) {
            diabtn.setTextColor(getResources().getColor(R.color.vermelhoperfil));
            diabtn.setBackground(getResources().getDrawable(R.drawable.btndiaatual));
        } else {
            if (i < diapreparacao) {
                diabtn.setBackground(getResources().getDrawable(R.drawable.btndiadisponivel));
            } else {
                diabtn.setEnabled(false);
                diabtn.setBackground(getResources().getDrawable(R.drawable.btndiadisabilitado));
            }
        }

        layoutbtns.addView(diabtn);

    }


}

1 answer

2


Inside the res folder is a directory with the name "anim", if you don’t have it, you create this folder, and create an Animation Resource file, or use one that you have there. Example of Animation Resource file:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    android:interpolator="@android:anim/bounce_interpolator">
    <translate
        android:fromXDelta="0%p"
        android:toXDelta="0.8%p"
        android:repeatCount="3"
        android:duration="160"/>
    <translate
        android:fromXDelta="-0.8%p"
        android:toXDelta="0%p"
        android:repeatCount="3"
        android:duration="160"/>

</set>

Now you can set the animation in the view by clicking on the button, ex:

TextView texto = (TextView) findViewById(R.id.textview);
Animation animacao = AnimationUtils.loadAnimation(getBaseContext(), R.anim.SuaAnimacao);
    texto.startAnimation(animacao);

Or you can put inside a delay to animate before changing the backgrounds, more or less like this:

    texto.startAnimation(animacao);
        final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
//Executa a função de mudar as coisas
        }
    }, 3000);

Then you call at the beginning of your For and it was already Dad, everything beautiful

Browser other questions tagged

You are not signed in. Login or sign up in order to post.