码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2020-02-01 12:26:24      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:led   -o   ica   too   list   mat   puts   oob   created   

技术图片


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


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

标签:led   -o   ica   too   list   mat   puts   oob   created   

原文地址:https://www.cnblogs.com/lijianming180/p/12247850.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!