动画后显示元素

时间:2019-07-10 15:43:25

标签: java android android-studio

我希望单击TextView后显示动画,然后再显示动画窗口。我该怎么做? P.s.我的动画包含在<animation-list>

的XML文件中
public class ExerciseWithExplain1 extends AppCompatActivity {

    private Button solution;
    private TextView txtVWRed, explainForTable, solExplain, nextScreen, falseRow53, falseRow54, falseRow55, falseRow56, falseRow46, trueRow45, trueRow44, falseRow43, trueRow36, trueRow35, falseRow34, trueRow33, trueRow23, falseRow23, trueRow25, trueRow24, falseRow24, falseRow25, falseRow26, falseRow33, trueRow34, falseRow35, falseRow36, trueRow43, falseRow44, falseRow45, trueRow46, trueRow53, trueRow54, trueRow55, trueRow56, trueRow26;
    LinearLayout layForTable;
    AlertDialog.Builder ad;
    Context context;
    AnimationDrawable animationDrawable;
    ImageView animImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercise_with_explain1);
        trueRow23 = findViewById(R.id.trueRow23);
        falseRow23 = findViewById(R.id.falseRow23);
        falseRow24 = findViewById(R.id.falseRow24);
        trueRow24 = findViewById(R.id.trueRow24);
        trueRow25 = findViewById(R.id.trueRow25);
        falseRow25 = findViewById(R.id.falseRow25);
        trueRow26 = findViewById(R.id.trueRow26);
        falseRow26 = findViewById(R.id.falseRow26);
        falseRow33 = findViewById(R.id.falseRow33);
        trueRow33 = findViewById(R.id.trueRow33);
        trueRow34 = findViewById(R.id.trueRow34);
        falseRow34 = findViewById(R.id.falseRow34);
        falseRow35 = findViewById(R.id.falseRow35);
        trueRow35 = findViewById(R.id.trueRow35);
        falseRow36 = findViewById(R.id.falseRow36);
        trueRow36 = findViewById(R.id.trueRow36);
        trueRow43 = findViewById(R.id.trueRow43);
        falseRow44 = findViewById(R.id.falseRow44);
        falseRow45 = findViewById(R.id.falseRow45);
        falseRow43 = findViewById(R.id.falseRow43);
        trueRow44 = findViewById(R.id.trueRow44);
        trueRow45 = findViewById(R.id.trueRow45);
        trueRow46 = findViewById(R.id.trueRow46);
        falseRow46 = findViewById(R.id.falseRow46);
        trueRow53 = findViewById(R.id.trueRow53);
        trueRow54 = findViewById(R.id.trueRow54);
        trueRow55 = findViewById(R.id.trueRow55);
        trueRow56 =findViewById(R.id.trueRow56);
        falseRow53 = findViewById(R.id.falseRow53);
        falseRow54 = findViewById(R.id.falseRow54);
        falseRow55 = findViewById(R.id.falseRow55);
        falseRow56 =findViewById(R.id.falseRow56);

        animImage = findViewById(R.id.animImage);
        animImage.setBackgroundResource(R.drawable.question_mark);
        animationDrawable = (AnimationDrawable) animImage.getBackground();

        context = ExerciseWithExplain1.this;
        ad = new AlertDialog.Builder(context);
        ad.setTitle(R.string.explainLogic);
        ad.setMessage(R.string.logicForAnd);
        final int ir = 0;
        falseRow56.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animImage.setVisibility(View.VISIBLE);
                    animationDrawable.start();
                    ad.show();
            }
        });
        trueRow56.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animImage.setVisibility(View.VISIBLE);
                animationDrawable.start();
            }
        });
        falseRow55.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animImage.setVisibility(View.VISIBLE);
                animationDrawable.start();
            }
        });

1 个答案:

答案 0 :(得分:0)

您可以使用“自定义动画Drwable”来尝试

import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;

public abstract class CustomAnimationDrawableNew extends AnimationDrawable {

/** Handles the animation callback. */
Handler mAnimationHandler;

public CustomAnimationDrawableNew(AnimationDrawable aniDrawable) {
    /* Add each frame to our animation drawable */
    for (int i = 0; i < aniDrawable.getNumberOfFrames(); i++) {
        this.addFrame(aniDrawable.getFrame(i), aniDrawable.getDuration(i));
    }
}

@Override
public void start() {
    super.start();
    /*
     * Call super.start() to call the base class start animation method.
     * Then add a handler to call onAnimationFinish() when the total
     * duration for the animation has passed
     */
    mAnimationHandler = new Handler();
    mAnimationHandler.post(new Runnable() {
        @Override
        public void run() {
            onAnimationStart();
        }
    });
    mAnimationHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            onAnimationFinish();
        }
    }, getTotalDuration());

}

/**
 * Gets the total duration of all frames.
 *
 * @return The total duration.
 */
public int getTotalDuration() {

    int iDuration = 0;

    for (int i = 0; i < this.getNumberOfFrames(); i++) {
        iDuration += this.getDuration(i);
    }

    return iDuration;
}

/**
 * Called when the animation finishes.
 */
public abstract void onAnimationFinish();
/**
 * Called when the animation starts.
 */
public abstract void onAnimationStart();

}

现在像下面的代码一样使用它。

TextView tv = (TextView) findViewById(R.id.iv_testing_testani);

tv.setOnClickListener(new OnClickListener() {
    public void onClick(final View v) {

        // Pass our animation drawable to our custom drawable class
        CustomAnimationDrawableNew cad = new CustomAnimationDrawableNew(
                (AnimationDrawable) getResources().getDrawable(
                        R.drawable.anim_test)) {
            @Override
            void onAnimationStart() {
                // Animation has started...
            }

            @Override
            void onAnimationFinish() {
                // Animation has finished...
            }
        };

        // Set the views drawable to our custom drawable
        v.setBackgroundDrawable(cad);

        // Start the animation
        cad.start();
    }
});