毕加索在纵向和横向模式下加载不同的图像
Picasso load different images in portrait and landscape mode
我有一个使用 Picasso 下载图像并将其显示在 ImageView 中的片段。问题是图像处于纵向模式,如果 phone 更改为横向模式,则同一图像会被拉伸。我寻找解决方案,但我得到的一般意义是简单地将 旋转到 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);
}
}
}
问题是当您旋转设备时,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
}
...
}
希望对您有所帮助
您可以使用此代码获取方向
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
}
}
我有一个使用 Picasso 下载图像并将其显示在 ImageView 中的片段。问题是图像处于纵向模式,如果 phone 更改为横向模式,则同一图像会被拉伸。我寻找解决方案,但我得到的一般意义是简单地将 旋转到 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);
}
}
}
问题是当您旋转设备时,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
}
...
}
希望对您有所帮助
您可以使用此代码获取方向
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
}
}