挑战练习:优化照片显示

出处:《Android编程权威指南》第16章-16.7

内容:单击缩放版本照片的DialogFragment,弹出大图


1 创建布局文件dialog_photo.xml




2 创建PhotoFragment类

public class PhotoFragment extends DialogFragment {
    public static final String EXTRA_PHOTO_PATH = "com.bignerdranch.android.criminalintent.photo_path";
    private ImageView mImageView;

    public static PhotoFragment newInstance(String photoPath) {
        Bundle args = new Bundle();
        args.putSerializable(EXTRA_PHOTO_PATH, photoPath);

        PhotoFragment fragment = new PhotoFragment();
        fragment.setArguments(args);
        fragment.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
        return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_photo, null);
        mImageView = (ImageView)v.findViewById(R.id.crime_photo);
        String path = getArguments().getString(EXTRA_PHOTO_PATH);
        Bitmap bitmap = PictureUtils.getScaledBitmap(path, getActivity());
        mImageView.setImageBitmap(bitmap);

        return new AlertDialog.Builder(getActivity()).setView(v).create();
    }
}

3 更新CrimeFragment类

{CSDN:CODE:2236612}

你可能感兴趣的:(Android)