影响ScaleType或将矩阵应用于AnimationDrawable



我很容易让AnimationDrawable工作,但它总是拉伸到ImageView的全尺寸(如果我有layout_height,width为fill_rent)。有趣的是,当我将layout_height、width设置为其他任何值(wrap_content、特定像素等)时,动画根本不会显示。不管我使用的是图像集图像还是背景图像,结果都一样。

理想的情况是,如果我可以将现有的矩阵应用于整个AnimationDrawables列表。

这是代码。。。`

            wxChartAnimation = new AnimationDrawable(); 
            int numCharts = subChart.getChartCount();
            for (int i=0; i < numCharts; i++) {
                WeatherChart.BaseChart baseChart = subChart.getBaseChart(i);
                File file = new File(baseChart.localPath);
                if (file != null && file.exists()) {
                    Drawable d = Drawable.createFromPath(baseChart.localPath);
                    if (d != null) {
                        if (wxChartAnimation != null) {
                            wxChartAnimation.addFrame(d,250);
                        }
                    }
                }
            }
            wxChartAnimation.setOneShot(false);
            wxChartImage.setImageDrawable(wxChartAnimation);
            wxChartAnimation.start();`

我也遇到过这个问题。不管它的价值如何,我发现了一个适合我的破解方法。似乎当您使用"new AnimationDrawable()"以编程方式创建AnimationDrawable时,它将不尊重ImageView的scaleType。但是,如果使用.anim xml资源,其中包含引用资源drawables中的帧列表,则它将采用第一个图像的大小,并根据ImageView的scaleType适当缩放动画。由于我正试图将assets文件夹中的图像加载到我的AnimationDrawable中,所以我需要从代码中完成。我最终做了以下事情:

在res/anim/pempty.xml中(其中empty_slide是一个透明图像,大小与我将在动画的其余部分使用的大小相同):

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/empty_slide" android:duration="0" />
</animation-list>

然后在我的java文件中:

imageView.setImageResource(R.anim.empty);
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getDrawable();
animationDrawable.addFrame(Drawable.createFromStream(context.getAssets().open(drawablePath), null),animationDuration);
// Add the rest of the frames
...
animationDrawable.start();

我很想听听你是否找到了更好的方法。。。

我正在寻找另一种方法。。。

问题是-"new AnimationDrawable()"未设置CurrentDrawable=>未设置AnimationDrawable的高度和宽度。

我是这样解决问题的:

protected void playFrameAnimationWithCurrentFrame(final ImageView ImView,final AnimationDrawable AnimDrawable){
    ImView.post(new Runnable(){
        public void run(){
            AnimDrawable.start();
        }});//We start the animation so that we have the current frame on which the matrix for mapping is based
    Thread t = new Thread() {
        @Override
        public void run() {
            while(AnimDrawable.getCurrent()==null){//wait for the current frame to appear                    
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            ImView.post(new Runnable(){//stop the current animation to assign animation to view and restart 
                public void run(){
                    AnimDrawable.stop();
                }});
                runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ImView.setImageDrawable(AnimDrawable);}
            });
            ImView.post(new Runnable(){
                public void run(){
                    AnimDrawable.start();}});
            interrupt();
        }};
    t.start();
}

之后,动画将使用我们之前安装在ImageView上的矩阵播放。

此方法不需要.xml资源文件。

最新更新