【问题标题】:Affecting ScaleType or applying a Matrix to AnimationDrawable影响 ScaleType 或将 Matrix 应用于 AnimationDrawable
【发布时间】:2011-06-27 03:09:23
【问题描述】:

让 AnimationDrawable 工作没有问题,但它总是拉伸到 ImageView 的完整大小(如果我有 layout_height,width 到 fill_parent)。有趣的是,当我将 layout_height、width 设置为其他任何值(wrap_content、特定像素等)时,动画根本不会显示。不管我使用 Image setImage 还是 setBackgroundImage,结果都一样。

如果我可以将现有的 Matrix 应用于整个 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();`

【问题讨论】:

    标签: android animation matrix bitmap drawable


    【解决方案1】:

    我也遇到过这个问题。对于它的价值,我遇到了一个出于我的目的的黑客解决方法。似乎当您使用“new AnimationDrawable()”以编程方式创建 AnimationDrawable 时,它​​不会尊重 ImageView 的 scaleType。但是,如果您使用 .anim xml 资源和该引用资源可绘制对象内的帧列表,它将采用第一个图像的大小并根据您的 ImageView 的 scaleType 适当地缩放动画。由于我试图将资产文件夹中的图像加载到我的 AnimationDrawable 中,因此我需要从代码中完成。我最终做了以下事情:

    在 res/anim/empty.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();
    

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

    【讨论】:

      【解决方案2】:

      我正在寻找另一种方式......

      问题是 - "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资源文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多