|
1 | 1 | package com.linchaolong.android.imagepicker.demo; |
2 | 2 |
|
3 | 3 | import android.content.DialogInterface; |
4 | | -import android.content.Intent; |
5 | 4 | import android.net.Uri; |
6 | 5 | import android.os.Bundle; |
7 | | -import androidx.annotation.NonNull; |
8 | | -import androidx.annotation.Nullable; |
9 | | -import androidx.fragment.app.Fragment; |
10 | | -import androidx.appcompat.app.AlertDialog; |
11 | 6 | import android.view.Gravity; |
12 | 7 | import android.view.LayoutInflater; |
13 | 8 | import android.view.View; |
14 | 9 | import android.view.ViewGroup; |
15 | 10 | import android.widget.ImageView; |
16 | 11 |
|
| 12 | +import androidx.annotation.Nullable; |
| 13 | +import androidx.appcompat.app.AlertDialog; |
| 14 | +import androidx.fragment.app.Fragment; |
| 15 | + |
17 | 16 | import com.linchaolong.android.imagepicker.ImagePicker; |
18 | 17 | import com.linchaolong.android.imagepicker.cropper.CropImageView; |
19 | 18 |
|
|
22 | 21 | */ |
23 | 22 | public class TestFragment extends Fragment { |
24 | 23 |
|
25 | | - private ImagePicker imagePicker = new ImagePicker(); |
26 | | - private ImageView imageView; |
27 | | - private CropImageView cropImageView; |
28 | | - |
29 | | - @Nullable @Override |
30 | | - public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, |
31 | | - @Nullable Bundle savedInstanceState) { |
32 | | - View contentView = inflater.inflate(R.layout.fragment_test, null); |
33 | | - init(contentView); |
34 | | - return contentView; |
35 | | - } |
| 24 | + private ImagePicker imagePicker; |
| 25 | + private ImageView imageView; |
| 26 | + private CropImageView cropImageView; |
36 | 27 |
|
37 | | - @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { |
38 | | - super.onActivityResult(requestCode, resultCode, data); |
39 | | - imagePicker.onActivityResult(this, requestCode, resultCode, data); |
40 | | - } |
41 | | - |
42 | | - @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, |
43 | | - @NonNull int[] grantResults) { |
44 | | - super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
45 | | - imagePicker.onRequestPermissionsResult(this, requestCode, permissions, grantResults); |
46 | | - } |
47 | | - |
48 | | - private void init(View contentView) { |
49 | | - imageView = (ImageView) contentView.findViewById(R.id.imageView); |
50 | | - cropImageView = (CropImageView) contentView.findViewById(R.id.cropImageView); |
51 | | - contentView.setOnClickListener(new View.OnClickListener() { |
52 | | - @Override public void onClick(View v) { |
53 | | - startCameraOrGallery(); |
54 | | - } |
55 | | - }); |
56 | | - } |
57 | | - |
58 | | - private void startCameraOrGallery() { |
59 | | - new AlertDialog.Builder(getActivity()).setTitle("设置头像") |
60 | | - .setItems(new String[] { "选择器(Chooser)", "从相册中选取图片(Gallery)", "拍照(Camera)" }, new DialogInterface.OnClickListener() { |
61 | | - @Override public void onClick(DialogInterface dialog, int which) { |
62 | | - // 回调 |
63 | | - ImagePicker.Callback callback = new ImagePicker.Callback() { |
64 | | - @Override public void onPickImage(Uri imageUri) { |
65 | | - } |
66 | | - |
67 | | - @Override public void onCropImage(Uri imageUri) { |
| 28 | + @Override |
| 29 | + public void onCreate(@Nullable Bundle savedInstanceState) { |
| 30 | + super.onCreate(savedInstanceState); |
| 31 | + imagePicker = new ImagePicker(this, new ImagePicker.Callback() { |
| 32 | + @Override |
| 33 | + public void onCropImage(Uri imageUri) { |
68 | 34 | imageView.setImageURI(imageUri); |
69 | 35 | cropImageView.setImageUriAsync(imageUri); |
70 | | - } |
71 | | - }; |
72 | | - if (which == 0) { |
73 | | - imagePicker.startChooser(TestFragment.this, callback); |
74 | | - } else if (which == 1) { |
75 | | - // 从相册中选取图片 |
76 | | - imagePicker.startGallery(TestFragment.this, callback); |
77 | | - } else { |
78 | | - // 拍照 |
79 | | - imagePicker.startCamera(TestFragment.this, callback); |
80 | 36 | } |
81 | | - } |
82 | | - }) |
83 | | - .show() |
84 | | - .getWindow() |
85 | | - .setGravity(Gravity.BOTTOM); |
86 | | - } |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + @Nullable |
| 41 | + @Override |
| 42 | + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, |
| 43 | + @Nullable Bundle savedInstanceState) { |
| 44 | + View contentView = inflater.inflate(R.layout.fragment_test, null); |
| 45 | + init(contentView); |
| 46 | + return contentView; |
| 47 | + } |
| 48 | + |
| 49 | + private void init(View contentView) { |
| 50 | + imageView = (ImageView) contentView.findViewById(R.id.imageView); |
| 51 | + cropImageView = (CropImageView) contentView.findViewById(R.id.cropImageView); |
| 52 | + contentView.setOnClickListener(new View.OnClickListener() { |
| 53 | + @Override |
| 54 | + public void onClick(View v) { |
| 55 | + startCameraOrGallery(); |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + private void startCameraOrGallery() { |
| 61 | + new AlertDialog.Builder(getActivity()).setTitle("设置头像") |
| 62 | + .setItems(new String[]{"选择器(Chooser)", "从相册中选取图片(Gallery)", "拍照(Camera)"}, new DialogInterface.OnClickListener() { |
| 63 | + @Override |
| 64 | + public void onClick(DialogInterface dialog, int which) { |
| 65 | + if (which == 0) { |
| 66 | + imagePicker.startChooser(); |
| 67 | + } else if (which == 1) { |
| 68 | + // 从相册中选取图片 |
| 69 | + imagePicker.startGallery(); |
| 70 | + } else { |
| 71 | + // 拍照 |
| 72 | + imagePicker.startCamera(); |
| 73 | + } |
| 74 | + } |
| 75 | + }) |
| 76 | + .show() |
| 77 | + .getWindow() |
| 78 | + .setGravity(Gravity.BOTTOM); |
| 79 | + } |
87 | 80 | } |
0 commit comments