服务器之家

服务器之家 > 正文

基于RxPaparazzo实现图片裁剪、图片旋转、比例放大缩小功能

时间:2020-09-29 13:50     来源/作者:曦笑大海

前言:基于RxPaparazzo的图片裁剪图片旋转、比例放大|缩小。

效果:

 基于RxPaparazzo实现图片裁剪、图片旋转、比例放大缩小功能

开发环境:AndroidStudio2.2.1+gradle-2.14.1 

涉及知识:

1.Material Design

(CardView+CoordinatorLayout+AppBarLayout+NestedScrollView+CollapsingToolbarLayout+Toolbar+FloatingActionButton)使用 

2.butterknife注解式开发 

3.基于RxJava+RxAndroid的RxPaparazzo使用 

引入依赖: 

?
1
2
3
4
5
6
7
8
9
10
11
12
compile 'com.android.support:appcompat-v7:24.+'
 //RxPaparazzo 拍照&相册
// compile "com.github.miguelbcr:RxPaparazzo:0.4.2-2.x"
 compile ("com.github.miguelbcr:RxPaparazzo:0.5.2-2.x") {
 exclude module: 'okhttp'
 exclude module: 'okio'
 }
 compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
 compile 'com.android.support:cardview-v7:24.+'
// compile 'com.android.support:customtabs:24.+'
 compile 'com.android.support:design:24.+'
 compile 'com.jakewharton:butterknife:7.0.1'

部分代码:

?
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
public class MainActivity extends AppCompatActivity {
 
 @Bind(R.id.iv_appbar)
 ImageView iv_appbar;
 
 @Bind(R.id.main_toolbar)
 Toolbar toolbar;
 
 /* @Bind(R.id.btn_float)
 FloatingActionButton btn_float;*/
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 ButterKnife.bind(this);
 initToolBar();
 }
 
 private void initToolBar() {
 this.setSupportActionBar(toolbar);
 toolbar.setTitle("我的");
 }
 
 @OnClick({R.id.main_toolbar, R.id.btn_float})
 public void onClick(View view) {
 final UCrop.Options options = new UCrop.Options();
 int color = ContextCompat.getColor(view.getContext(), R.color.colorPrimary);
 options.setToolbarColor(color);
 options.setStatusBarColor(ContextCompat.getColor(view.getContext(), R.color.colorPrimaryDark));
 options.setActiveWidgetColor(color);
 switch (view.getId()) {
  case R.id.main_toolbar:
  Toast.makeText(MainActivity.this, "Toolbar点击", Toast.LENGTH_SHORT).show();
  break;
  case R.id.btn_float: {
  showDialog(view, options);
  break;
  }
 }
 }
 
 private void showDialog(View view, final UCrop.Options options) {
 final Context context = view.getContext();
 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
 builder.setTitle("设置背景图片:").setMessage("如何获取图片?")
  .setPositiveButton("相册", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
   dialog.dismiss();
//   RxPaparazzo.takeImage(MainActivity.this)
   RxPaparazzo.single(MainActivity.this)
    .crop(options)
    .usingGallery()
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Consumer<Response<MainActivity, FileData>>() {
     @Override
     public void accept(Response<MainActivity, FileData>
        response) throws Exception {
     if (response.resultCode() == Activity.RESULT_OK) {
 
      File filePath = response.data().getFile();
      Bitmap bitmap = BitmapFactory.
       decodeFile(filePath.getPath());
      iv_appbar.setImageBitmap(bitmap);
 
     } else if (response.resultCode() == Activity.RESULT_CANCELED) {
 
      Toast.makeText(MainActivity.this, "取消相册访问",
       Toast.LENGTH_SHORT).show();
 
     } else {
 
      Toast.makeText(MainActivity.this, "未知错误!",
       Toast.LENGTH_SHORT).show();
     }
     }
    });
   }
  })
  .setNeutralButton("取消", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
   dialog.dismiss();
   }
  })
  .setNegativeButton("拍照", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
   dialog.dismiss();
//   RxPaparazzo.takeImage(MainActivity.this)
   RxPaparazzo.single(MainActivity.this)
    .crop(options)
    .usingCamera()
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Consumer<Response<MainActivity, FileData>>() {
     @Override
     public void accept(Response<MainActivity, FileData>
        response) throws Exception {
 
     if (response.resultCode() == Activity.RESULT_OK) {
      FileData filePath = response.data();
      Bitmap bitmap = BitmapFactory.
       decodeFile(filePath.getFile().getPath());
      iv_appbar.setImageBitmap(bitmap);
     } else if (response.resultCode() == Activity.RESULT_CANCELED) {
      Toast.makeText(MainActivity.this, "取消拍照",
       Toast.LENGTH_SHORT).show();
     } else {
      Toast.makeText(MainActivity.this, "未知错误!",
       Toast.LENGTH_SHORT).show();
     }
     }
    });
   /**
    * new Consumer<Response<MainActivity, String>>() {
   @Override public void accept(@NonNull Response<MainActivity, String> response) throws Exception {
   if (response.resultCode() == Activity.RESULT_OK) {
   String filePath = response.data();
   Bitmap bitmap = BitmapFactory.decodeFile(filePath);
   iv_appbar.setImageBitmap(bitmap);
   } else if (response.resultCode() == Activity.RESULT_CANCELED) {
   Toast.makeText(MainActivity.this, "取消拍照", Toast.LENGTH_SHORT).show();
   } else {
   Toast.makeText(MainActivity.this, "未知错误!", Toast.LENGTH_SHORT).show();
   }
   }
   }
    *
    */
   }
  });
 
 AlertDialog dialog = builder.create();
 dialog.show();
 
 dialog.getButton(DialogInterface.BUTTON_POSITIVE).
 
  setTextColor(ContextCompat.getColor(context, R.color.colorPrimary)
  );
 dialog.getButton(DialogInterface.BUTTON_NEGATIVE).
 
  setTextColor(ContextCompat.getColor(context, R.color.colorPrimary)
 
  );
 dialog.getButton(DialogInterface.BUTTON_NEUTRAL).
 
  setTextColor(ContextCompat.getColor(context, R.color.colorAccent)
 
  );
 }
 
 @Override
 protected void onDestroy() {
 super.onDestroy();
 ButterKnife.unbind(this);//解除绑定
 }
}

源码下载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部