【问题标题】:Changing image in ImageView on orientation change does not work在方向更改时更改 ImageView 中的图像不起作用
【发布时间】:2011-08-26 10:30:44
【问题描述】:

我有 android:configChanges="orientation|keyboardHidden"

的活动

我正在尝试在设备方向更改时更改 ImageView 中的图像:

public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);

  // refresh the instructions image
  ImageView instructions = (ImageView) findViewById(R.id.img_instructions);
  instructions.setImageResource(R.drawable.img_instructions);
}

这仅在手机第一次旋转时有效,之后无效。

谁能告诉我为什么会这样?

【问题讨论】:

    标签: android


    【解决方案1】:

    Peceps 自己的答案 IMO 是正确的,因为它不依赖于为可绘制对象提供不同的名称。似乎资源 id 被缓存但 drawable 没有,所以我们可以将 drawable 提供给 ImageView 而不是资源 id,使解决方案更加优雅(避免 try/catch 块):

    public void onConfigurationChanged(Configuration newConfig) {
       super.onConfigurationChanged(newConfig);
    
        // refresh the instructions image
        ImageView instructions = (ImageView) findViewById(R.id.instructions);
        instructions.setImageDrawable(
            getResources().getDrawable(R.drawable.img_instructions));
    
    }
    

    【讨论】:

      【解决方案2】:

      我认为你试图做这样的事情

      public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
      
        // refresh the instructions image
          ImageView instructions = (ImageView) findViewById(R.id.img_instructions);
      
          if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            instructions.setImageResource(R.drawable.img_instructions_land);
          } else {
            instructions.setImageResource(R.drawable.img_instructions_port);
          }
      }
      

      【讨论】:

        【解决方案3】:

        使用这个你可以多次改变方向它工作正常

        public class VersionActivity extends Activity {
        
            ImageView img;
        
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
        
                img = (ImageView) findViewById(R.id.imageView1);
        
        
            }
            public void onConfigurationChanged(Configuration newConfig) {
                super.onConfigurationChanged(newConfig);
        
                // Checks the orientation of the screen
                if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        
                    img.setImageResource(R.drawable.splash);
        
                } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        
                    img.setImageResource(R.drawable.icon);
                }
              }
        
        }
        

        欲了解更多信息,请转到How to detect orientation change in layout in Android?

        【讨论】:

          【解决方案4】:

          谢谢,如果我对纵向和横向使用不同的图像名称,这两个答案都是正确的。

          要使用相同的名称,这可行:

          public void onConfigurationChanged(Configuration newConfig) {
              super.onConfigurationChanged(newConfig);
          
              // refresh the instructions image
              ImageView instructions = (ImageView) findViewById(R.id.instructions);
              // prevent caching
              try {
                instructions.setImageResource(0);
              } catch (Throwable e) {
                // ignore
              }
              instructions.setImageResource(R.drawable.img_instructions);
            }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-05-28
            • 2013-03-12
            • 2019-10-07
            相关资源
            最近更新 更多