服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 基于RxPaparazzo实现图片裁剪、图片旋转、比例放大缩小功能

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

2020-09-29 13:50曦笑大海 Java教程

这篇文章主要为大家详细介绍了基于RxPaparazzo实现图片裁剪、图片旋转、比例放大缩小功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

前言:基于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);//解除绑定
 }
}

源码下载

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

延伸 · 阅读

精彩推荐