java-按顺序播放帧动画.安卓系统

java-按顺序播放帧动画.安卓系统,第1张

概述好的,有人提出了类似的问题,但是没有答案对解决我的问题有任何影响.我尝试过Thread.sleep,以及延迟的可运行.使用处理程序等我想使用相同的imageview显示帧动画序列(AnimationDrawable),并根据需要更改背景动画.用户输入一系列5个动画,然后可以播放它们(如果我的程序可以工作的话)

好的,有人提出了类似的问题,但是没有答案对解决我的问题有任何影响.我尝试过Thread.sleep,以及延迟的可运行.使用处理程序等

我想使用相同的imagevIEw显示帧动画序列(AnimationDrawable),并根据需要更改背景动画.用户输入一系列5个动画,然后可以播放它们(如果我的程序可以工作的话).一旦选择了动画,我将使用一个包含if语句和switch语句的for循环,以选择是否选择了动画并进行播放.

如您所料,这无法正常工作,因为程序会跳过for循环,而实际上只会播放第一个和最后一个动画.这是代码的原理:

for(i=0;i<5;i++){    if(conditions){        vIEw.setBackground(chosenAnimation);        ((AnimationDrawable)vIEw.getBackground().start();    }}

因此,正如我所说,我尝试了Thread.sleep(),但它并没有满足我的需求.我尝试过使用Handler类,并在可运行项上设置了延迟.我已经试过了:

vIEw.postDelayed(new Runnable() {    @OverrIDe     public voID run() {        //works the same without this line        ((AnimationDrawable)vIEw.getBackground()).stop();        ((AnimationDrawable)vIEw.getBackground()).start();    }}, 1000);

除了添加暂停之前,这些 *** 作均不执行任何 *** 作,之前执行的 *** 作与添加此内容之前完全相同.我精心调试了代码,一切正常.动画均已单独测试.

就像我说的那样,已经提出并回答了类似的问题,但我没有想要的结果,程序要等到一个动画完成后才能再次通过for循环.

我想再次声明,这是一系列使用AnimationDrawable和每次相同的imagevIEw的帧动画.提前致谢!

解决方法:

您所有的动画都将以相同的延迟开始,例如,应通过将其乘以i来增加此延迟.您还可以通过编程方式计算每个动画的持续时间,并根据需要增加延迟.

我只是尝试实现您想要的并且没有问题,尽管我的示例使用了3rd party库,但这不是必需的.

package com.example.masktest.app;import androID.animation.Animator;import androID.graphics.drawable.AnimationDrawable;import androID.os.Bundle;import androID.support.v4.content.ContextCompat;import androID.support.v7.app.AppCompatActivity;import androID.vIEw.VIEw;import androID.Widget.ImageVIEw;import java.lang.ref.softReference;import java.lang.ref.WeakReference;import java.util.concurrent.atomic.atomicreference;import static com.dtx12.androID_animations_actions.actions.Actions.*;public class MainActivity extends AppCompatActivity {    ImageVIEw imageVIEw;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        imageVIEw = (ImageVIEw) findVIEwByID(R.ID.imageVIEw);        findVIEwByID(R.ID.button).setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                playAnimation();            }        });    }    private voID playAnimation() {        final AnimationDrawable firstDrawable = (AnimationDrawable) ContextCompat.getDrawable(MainActivity.this, R.anim.anim_androID);        final AnimationDrawable secondDrawable = (AnimationDrawable) ContextCompat.getDrawable(MainActivity.this, R.anim.anim_androID_2);        final atomicreference<Integer> cpt = new atomicreference<>(0);        Animator sequence = repeat(6, sequence(run(new Runnable() {            @OverrIDe            public voID run() {                if (imageVIEw.getDrawable() instanceof AnimationDrawable) {                    ((AnimationDrawable) imageVIEw.getDrawable()).stop();                }                imageVIEw.setimageDrawable(cpt.get() % 2 == 0 ? secondDrawable : firstDrawable);                ((AnimationDrawable) imageVIEw.getDrawable()).start();                cpt.set(cpt.get() + 1);            }        }), delay(countAnimationDuration(secondDrawable))));        play(sequence, imageVIEw);    }    private float countAnimationDuration(AnimationDrawable drawable) {        int duration = 0;        for (int i = 0; i < drawable.getNumberOfFrames(); i++) {            duration += drawable.getDuration(i);        }        return duration / 1000f;    }}
总结

以上是内存溢出为你收集整理的java-按顺序播放帧动画.安卓系统全部内容,希望文章能够帮你解决java-按顺序播放帧动画.安卓系统所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1079480.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存