[英]Picasso load different images in portrait and landscape mode
I have a fragment which downloads an image using Picasso and displays it in ImageView.我有一个片段,它使用毕加索下载图像并将其显示在 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.我寻找解决方案,但我得到的一般意义是简单地将其旋转 90 度。 But that's not what I want.但这不是我想要的。 I want to load two different images ie one image url will be for portrait mode and the other will be for landscape mode.我想加载两个不同的图像,即一个图像 url 用于纵向模式,另一个用于横向模式。 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);
}
}
}
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) { } exm :- if(rotation== 你的条件) { }
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
}
}
The problem is when you rotate the device the onViewCreated
is called again (but it called after onConfigurationChanged
called)问题是当你旋转设备时onViewCreated
再次被调用(但它在onConfigurationChanged
调用之后调用)
In your code, the image have changed to landscape onConfigurationChanged
, but after that the image change to portrait when onViewCreated
called.在您的代码中,图像已更改为横向onConfigurationChanged
,但之后在调用onViewCreated
时图像更改为纵向。 This is the reason why you always have portrait image这就是为什么你总是有肖像图像的原因
Therefore you should change the image in onViewCreated
instead of in onConfigurationChanged
因此,您应该在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
}
...
}
Hope this help希望这有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.