简体   繁体   中英

how to change the image of imageview on changing configuration in android

as new to android and have a requirement that imageview src image change when changing the phone oreintation(portrait to landscape and vice versa) .set the image for imageview is ok for me but how can the above requirement can be achieve.is this thing possible in android. Thanks .A little help will be very much appreciate. here i am mention the two layout which contain some imageview

1 the portrait mode of design

应用程序的肖像设计

2.the landscape mode of design 应用程序的景观设计 if anything more require please ask me. the app is design for the android version 2.3 and api level 10

Try this.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    ImageView imageView = (ImageView)findViewById(R.id.imageView);
    if(getResources().getConfiguration().orientation == 2) {
        imageView.setImageDrawable(R.drawable.landscapeimage);
    } else if(getResources().getConfiguration().orientation == 1){
        imageView.setImageDrawable(R.drawable.portraitimage);
    }
}

you can create Layout-land and from that you can copy paste your code from the layout folder , and change the imageview to whatever you like. now when the user change orientation it will go to the layout-land. hope that help you.

write this in onStart()

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

if(width<height){
   //in portrait mode

}
else{
   //in landscape mode.
}

you have to create new layout and put it to its designated folder such like:

- layout layout-800x400 
- layout-land 
- layout-land-800x400 
- layout-port
- layout-port-1232x800

etc.

in each folder are adjusted layout depends on what design would you want it to be like. Now, if its not working, you have to identify the dimension or resolution of your device and add it as your layout folder like sample above,coz maybe the device was unable to find the layout folder for it.

for more reference check this link http://developer.android.com/guide/practices/screens_support.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM