animate removeAllViews when recreating view

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 number picker and it removes the existing buttons and creates again with the correct background for that chosen number, all right so the problem is that when it does this rebuild the scrollview flashes, and it looks pretty ugly, it looks like a glitch, how could it animate this re-creation of buttons?

My method:

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

    }


}
    
asked by anonymous 27.12.2017 / 19:31

1 answer

2

Inside the folder res has a directory with name "anim", if not you create this folder, and create an Animation resource file, or use some that has there. Animation resource file example:

<?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 the button, eg:

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

    
29.12.2017 / 19:24