毕加索在纵向和横向模式下加载不同的图像

时间:2016-03-30 08:33:53

标签: android screen-orientation picasso

我有一个片段,使用Picasso下载图像并在ImageView中显示。问题是图像处于纵向模式,如果手机变为横向模式,则同一图像会被拉伸。我寻找解决方案,但我在一般意义上得到的只是旋转到90度。但这不是我想要的。我想加载两个不同的图像,即一个图像URL将用于纵向模式,另一个将用于横向模式。我尝试检测方向更改,然后相应地加载图像。这没有用。在横向模式中,肖像图像也在加载。有什么建议吗?

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


        }
    }








}

2 个答案:

答案 0 :(得分:1)

您可以使用此代码获取方向

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

并根据旋转条件加载图像。 exm: - if(rotation == your condition){}

您也可以使用以下代码:

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

答案 1 :(得分:0)

问题是当您旋转设备时再次调用onViewCreated(但在调用onConfigurationChanged后调用它)

在您的代码中,图片已更改为横向onConfigurationChanged,但在此之后,当onViewCreated被调用时,图像会变为纵向。这就是为什么你总是有肖像图像

因此,您应该更改onViewCreated而不是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
    }
    ...
}

希望这个帮助

相关问题