我开始从图库中选择图像,然后缩放图像并将 ImageView 动态添加到布局中。
我希望在方向改变时重新加载该图像。我在onCreate中设置了setRetainInstance(true)。但是,在方向更改之后,当我尝试通过在应该是其容器的 FrameLayout 上调用 getWidth() 来缩放位图的适当宽度时,我得到 0。
public class NewWallpaper extends Fragment {
private static final int PICK_BACKGROUND = 1;
private BitmapFactory.Options bitmapFactoryOptions;
// Data
private Uri backgroundURI = null;
private WeakReference<Bitmap> backgroundBitmap = null;
// Views
FrameLayout backgroundContainer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_new_wallpaper, container, false);
// BitmapFactory options
bitmapFactoryOptions = new BitmapFactory.Options();
bitmapFactoryOptions.inPurgeable = true;
Button pickBackground = (Button) view.findViewById(R.id.buttonPickBackground);
pickBackground.setOnClickListener(pickBackgroundListener);
backgroundContainer = (FrameLayout) view.findViewById(R.id.frameLayoutbackgroundContainer);
if (backgroundURI != null)
setBackgroundPreview();
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
private Button.OnClickListener pickBackgroundListener = new Button.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, PICK_BACKGROUND);
}
};
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_BACKGROUND && resultCode == getActivity().RESULT_OK && data != null && data.getData() != null) {
backgroundURI = data.getData();
setBackgroundPreview();
}
super.onActivityResult(requestCode, resultCode, data);
}
private void setBackgroundPreview() {
try {
backgroundContainer.removeAllViews();
Bitmap background = BitmapHelper.loadScaledBitmap(getActivity(), backgroundURI, backgroundContainer.getWidth());
backgroundBitmap = new WeakReference<Bitmap>(background);
// returns 0
int containerWidth = backgroundContainer.getWidth();
ImageView backgroundView = new ImageView(getActivity());
backgroundView.setLayoutParams(new LinearLayout.LayoutParams(
backgroundContainer.getWidth(),
BitmapHelper.calculateHeight(
background.getWidth(),
background.getHeight(),
backgroundContainer.getWidth())
));
backgroundView.setAdjustViewBounds(true);
backgroundView.setScaleType(ImageView.ScaleType.FIT_XY);
backgroundView.setImageBitmap(background);
backgroundContainer.addView(backgroundView);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我似乎找不到在方向更改后重新填充 View 的方法。有人可以告诉我我做错了什么吗?
最佳答案
不要从 onCreateView
调用 setBackgroundPreview()
。如果 View 尚未创建完成,则无法测量该 View 。
改为在 onActivityCreated
中调用 setBackgroundPreview()
。
关于android - onCreateView() 中 getWidth() 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20312768/