您的位置:首页 > 移动开发 > Android开发

Android编程权威指南(第2版)—第16章挑战练习

2017-09-11 23:04 337 查看
16.7挑战练习:优化照片显示

(1)新建dialog_photo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/crime_photo_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</LinearLayout>


(2)新建PhotoDetailFragment.java

public class PhotoDetailFragment extends DialogFragment {

private static final String ARG_File = "file";
private ImageView mPhotoView;

public static PhotoDetailFragment newInstance(File file) {
Bundle args = new Bundle();
args.putSerializable(ARG_File, file);

PhotoDetailFragment fragment = new PhotoDetailFragment();
fragment.setArguments(args);
return fragment;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
File file = (File) getArguments().getSerializable(ARG_File);

View v = LayoutInflater.from(getActivity())
.inflate(R.layout.dialog_photo, null);
mPhotoView = (ImageView) v.findViewById(R.id.crime_photo_detail);

Bitmap bitmap = PictureUtils.getScaledBitmap(
file.getPath(), getActivity());
mPhotoView.setImageBitmap(bitmap);

return new AlertDialog.Builder(getActivity())
.setView(v)
//.setTitle(R.string.date_picker_title)
.setPositiveButton(android.R.string.ok, null)
.create();

}
}

(3)修改CrimeFragment.java

private static final int REQUEST_PHOTO = 3;
private static final String DIALOG_PHOTO = "DialogPhoto";


onCreateView增加处理

mPhotoView = (ImageView) v.findViewById(R.id.crime_photo);
mPhotoView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mPhotoFile == null || !mPhotoFile.exists()) {
mPhotoView.setImageDrawable(null);
} else {

FragmentManager manager = getFragmentManager();
PhotoDetailFragment dialog = PhotoDetailFragment.newInstance(mPhotoFile);
dialog.setTargetFragment(CrimeFragment.this, REQUEST_PHOTO);
dialog.show(manager, DIALOG_PHOTO);

}

}
});
updatePhotoView();




16.8 挑战练习:优化缩略图加载

修改CrimeFragment.java

(1)updatePhotoView,调用带宽高参数的getScaledBitmap

private void updatePhotoView(int width, int height) {
if (mPhotoFile == null || !mPhotoFile.exists()) {
mPhotoView.setImageDrawable(null);
} else {
Bitmap bitmap = PictureUtils.getScaledBitmap(
mPhotoFile.getPath(), width, height); // change this one
mPhotoView.setImageBitmap(bitmap);
}
}


(2)onCreateView加上

mPhotoObserver = mPhotoView.getViewTreeObserver();
mPhotoObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
updatePhotoView(mPhotoView.getWidth(), mPhotoView.getHeight());
}
});

return v;


(3)修改onActivityResult,不然返回拍完照片返回CrimeFragment的时候会显示不出略缩图

else if (requestCode == REQUEST_PHOTO) {

updatePhotoView(mPhotoView.getWidth(), mPhotoView.getHeight());
}


参考:点击打开链接

上述代码运行无误,可以达到效果,但是OnGlobalLayoutListener的原理还没有太搞懂,只知道可以通过这个方式获取视图的宽和高,待以后深入学习后再加补充。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: