开发者

Changing image in ImageView on orientation change does not work

I have activity with android:configChanges="orientation|keyboardHidden"

I am trying to change image in ImageView when the device orientation changes with:

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

  // refresh the instructions image
  ImageView instructions = (ImageView) fi开发者_JS百科ndViewById(R.id.img_instructions);
  instructions.setImageResource(R.drawable.img_instructions);
}

This only works the first time when the phone is rotated but not after that.

Can someone pls tell me why this happens?


Peceps' own answer IMO is the correct one since it does not depend on giving different names to the drawables. It seems that the resource id is cached but the drawable is not, so we can supply the drawable to the ImageView instead of the resource id, making the solution a little more elegant (avoiding the try/catch block):

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));

}


I think you trying to do something like this

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);
    }
}


use this you can multiple time change orientation it's work fine

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);
        }
      }

}

for more information goto How to detect orientation change in layout in Android?


Thanks, both answers are correct and work if I am using different image name for portrait and landscape.

To use the same name this works:

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);
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜