android - 我可以预加载 Animation Drawable 吗?

我正在使用 Android 的 Animation Drawable 来显示动画。但是我希望它尽可能活泼,因为我希望它在触摸屏幕时显示。现在的方式太慢了。显示大约需要 500 毫秒。有什么方法可以预加载动画或其他替代方法吗?

代码如下:

public void setYes(){
    ImageView prev_view = (ImageView) ref_container.getChildAt(ref_container.getChildCount()-2);
    prev_view.setImageResource(R.drawable.accept);
    acceptAnimation = (AnimationDrawable) prev_view.getDrawable();
    acceptAnimation.start();
}

这里是包含动画帧的 drawable 文件夹中的 XML:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/accept01" android:duration="33" />
<item android:drawable="@drawable/accept02" android:duration="33" />
<item android:drawable="@drawable/accept03" android:duration="33" />
<item android:drawable="@drawable/accept04" android:duration="33" />
<item android:drawable="@drawable/accept05" android:duration="33" />
<item android:drawable="@drawable/accept06" android:duration="33" />
<item android:drawable="@drawable/accept07" android:duration="33" />
<item android:drawable="@drawable/accept08" android:duration="33" />
<item android:drawable="@drawable/accept09" android:duration="33" />
<item android:drawable="@drawable/accept10" android:duration="33" />
<item android:drawable="@drawable/accept11" android:duration="33" />
<item android:drawable="@drawable/accept12" android:duration="33" />
<item android:drawable="@drawable/accept13" android:duration="33" />
<item android:drawable="@drawable/accept14" android:duration="33" />
<item android:drawable="@drawable/accept15" android:duration="33" />
<item android:drawable="@drawable/accept16" android:duration="33" />
</animation-list>

当我检测到某种触摸模式时,我会调用此函数。我想我之前无法加载它,因为我在不同的 View 中使用它 ref_container.getChildCount()-2。你们有任何解决方案,建议加快速度吗?

最佳答案

是的,动画第一次显示时有延迟。解决方案是在 onCreate() 中分配图像以避免延迟。

这是代码

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.animated_view) AppCompatImageView animatedView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_activity);
        ButterKnife.bind(this);

        // assign animation
        animatedView.setBackgroundResource(R.drawable.your_xml_list_of_images);
    }

    @OnClick(R.id.yolo)
    public void onClickYolo() {
        animatedView.setVisibility(View.VISIBLE);
        ((AnimationDrawable) animatedView.getBackground()).start();
    }

}

https://stackoverflow.com/questions/33565208/

相关文章:

javascript - JS 闭包和函数参数

email - 更改 TFS 2015 电子邮件警报中的主题

python - 以 CSR/COO 格式为存储在 Pandas DataFrame 中的分类数据的

Python - sys.stderr 未保存到 .txt 或 .log

python - NumPy 或 SciPy 计算加权中位数

android - RecyclerView 不显示任何图像

android - 呈现问题 :Binary XML file line #-1: No start

amazon-web-services - AWS CLI 工具 - 无法从 S3 递归下载

windows-services - Windows 服务的启动和停止超时是多少?

amazon-web-services - spark-ec2 --copy-aws-credent