• Android编程权威指南(第2版)--第16章 使用intent拍照 挑战练习


    13112Q52601-0.jpg


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

    新建dialog_photo.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <?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>

    新建PhotoDetailFragment.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36

    public class 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;
    }


    @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)

    .setPositiveButton(android.R.string.ok, null)
    .create();

    }
    }

    修改CrimeFragment.java

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

    onCreateView增加处理

    大专栏  Android编程权威指南(第2版)--第16章 使用intent拍照 挑战练习r">
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    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

    updatePhotoView,调用带宽高参数的getScaledBitmap

    1
    2
    3
    4
    5
    6
    7
    8
    9
    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);
    }
    }

    onCreateView加上

    1
    2
    3
    4
    5
    6
    7
    8
    mPhotoObserver = mPhotoView.getViewTreeObserver();
    mPhotoObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
    updatePhotoView(mPhotoView.getWidth(), mPhotoView.getHeight());
    }
    });
    return v;

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

    1
    2
    3
    else if (requestCode == REQUEST_PHOTO) {
    updatePhotoView(mPhotoView.getWidth(), mPhotoView.getHeight());
    }

    上述代码运行无误,可以达到效果,虽然可以通过OnGlobalLayoutListener这个方式获取视图的宽和高,但是还没搞清楚,待日后补充吧

    参考:点击打开链接

    Github具体代码:hyyyrwang.github.io

    我的简书:https://www.jianshu.com/p/1b9785db93a6


  • 相关阅读:
    socket-重叠模型(overlap)
    ssh 免密登陆
    安装google 框架
    为什么不同网段的ip 不能直接通信
    python中的import,reload,以及__import__
    C Runtime Library、C  Runtime
    SQLite3 C/C++ 开发接口简介
    mysql添加索引语句
    mysql 字段左右补0
    @Transactional注解的失效场景
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12247850.html
Copyright © 2020-2023  润新知