【问题标题】:Picasso load different images in portrait and landscape modePicasso 在纵向和横向模式下加载不同的图像
【发布时间】:2016-07-18 03:32:48
【问题描述】:

我有一个片段,它使用 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);


        }
    }








}

【问题讨论】:

  • 你检查过你的 onConfigurationChanged 是否被解雇了吗?并且代码流程正确
  • 纵向效果很好。当我尝试将模式更改为横向时,它仍会加载纵向图像 url。
  • 你能不能在onConfigurationChanged里面的if else写一些logcat来检查应用检测到正确的方向
  • 是的。我使用 Toast 进行了检查。它被解雇了。
  • 请试试我的回答

标签: android screen-orientation picasso


【解决方案1】:

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

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

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

您也可以使用以下代码:

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

【讨论】:

  • 你在水平屏幕和垂直屏幕上得到什么
  • 在纵向模式下,加载正确的图像,但在横向模式下,显示相同的图像。它不会加载它应该加载的 url。 @Bhunnu
【解决方案2】:

问题是当您旋转设备时,再次调用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
    }
    ...
}

希望有帮助

【讨论】:

  • 我试过了,但它仍然在横向模式下加载相同的图像。
猜你喜欢
  • 1970-01-01
  • 2013-05-12
  • 1970-01-01
  • 2019-02-15
  • 2011-09-05
  • 1970-01-01
  • 1970-01-01
  • 2018-05-01
  • 2013-11-21
相关资源
最近更新 更多