Somnath Pal
Somnath Pal

Reputation: 1512

Picasso load different images in portrait and landscape mode

I have a fragment which downloads an image using Picasso and displays it in ImageView. Problem is the image is in portrait mode and if the phone changes to landscape mode the same image gets stretched. I looked for solutions but what I got in general sense is to simply rotate the to 90 degrees. But that's not what I want. I want to load two different images i.e. one image url will be for portrait mode and the other will be for landscape mode. I tried detecting orientation change and then loading the images accordingly. This didn't worked. In landscape mode also, the portrait images were loading. Any suggestions?

public class FragmentOne extends Fragment{

    Button next, previous;
    ProportionalImageView imageView;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragmentone, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        next = (Button)view.findViewById(R.id.next);
        previous = (Button)view.findViewById(R.id.previous);
        imageView = (ProportionalImageView)view.findViewById(R.id.image);

        Picasso.with(getActivity())
                .load("http://i.imgur.com/h5iwVmh.jpg")
                .placeholder(R.drawable.placeholder)
                .into(imageView);



        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((ViewPager) ((MainActivity)getActivity()).findViewById(R.id.pager)).setCurrentItem(1);
            }
        });

        previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((ViewPager) ((MainActivity)getActivity()).findViewById(R.id.pager)).setCurrentItem(-1);
            }
        });

    }

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

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Picasso.with(getActivity())
                    .load("http://i.imgur.com/H0IXUy0.jpg")
                    .placeholder(R.drawable.placeholder)
                    .into(imageView);

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            Picasso.with(getActivity())
                    .load("http://i.imgur.com/h5iwVmh.jpg")
                    .placeholder(R.drawable.placeholder)
                    .into(imageView);


        }
    }








}

Upvotes: 2

Views: 1442

Answers (2)

Bhunnu Baba
Bhunnu Baba

Reputation: 1802

you can use this code for get the orientation

Display display = ((WindowManager)    context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();

and load the image according rotation condition. exm :- if(rotation== your condition) { }

you can also use to below code:

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) {
        //load image in picasso here
    } else {
        //load image in picasso here
    }
}

Upvotes: 1

Linh
Linh

Reputation: 60933

The problem is when you rotate the device the onViewCreated is called again (but it called after onConfigurationChanged called)

In your code, the image have changed to landscape onConfigurationChanged, but after that the image change to portrait when onViewCreated called. This is the reason why you always have portrait image

Therefore you should change the image in onViewCreated instead of in onConfigurationChanged

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    ...
    int currentOrientation = getResources().getConfiguration().orientation;
    if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
        // load you landscape image
    }
    else {
       // load your portrait image
    }
    ...
}

Hope this help

Upvotes: 0

Related Questions