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

16.7挑战练习:优化照片显示

(1)新建dialog_photo.xml




    


(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();

Android编程权威指南(第2版)—第16章挑战练习_第1张图片


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的原理还没有太搞懂,只知道可以通过这个方式获取视图的宽和高,待以后深入学习后再加补充。

你可能感兴趣的:(Android编程权威指南(第2版)—第16章挑战练习)