基于RxPaparazzo實(shí)現(xiàn)圖片裁剪、圖片旋轉(zhuǎn)、比例放大縮小功能
前言:基于RxPaparazzo的圖片裁剪,圖片旋轉(zhuǎn)、比例放大|縮小。
效果:
開發(fā)環(huán)境:AndroidStudio2.2.1+gradle-2.14.1
涉及知識(shí):
1.Material Design
(CardView+CoordinatorLayout+AppBarLayout+NestedScrollView+CollapsingToolbarLayout+Toolbar+FloatingActionButton)使用
2.butterknife注解式開發(fā)
3.基于RxJava+RxAndroid的RxPaparazzo使用
引入依賴:
compile 'com.android.support:appcompat-v7:24.+' //RxPaparazzo 拍照&相冊(cè) // 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'
部分代碼:
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點(diǎn)擊", 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("設(shè)置背景圖片:").setMessage("如何獲取圖片?") .setPositiveButton("相冊(cè)", 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, "取消相冊(cè)訪問(wèn)", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "未知錯(cuò)誤!", 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, "未知錯(cuò)誤!", 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, "未知錯(cuò)誤!", 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);//解除綁定 } }
源碼下載
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android調(diào)用原生圖片裁剪后圖片尺寸縮放的解決方法
- Android ImageView實(shí)現(xiàn)圖片裁剪和顯示功能
- Android 7.0中拍照和圖片裁剪適配的問(wèn)題詳解
- Android圖片裁剪功能實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)相機(jī)拍攝、選擇、圖片裁剪功能
- Android開發(fā)從相機(jī)或相冊(cè)獲取圖片裁剪
- 使用Java代碼在Android中實(shí)現(xiàn)圖片裁剪功能
- Android UI之ImageView實(shí)現(xiàn)圖片旋轉(zhuǎn)和縮放
- Android中利用matrix 控制圖片的旋轉(zhuǎn)、縮放、移動(dòng)
- Android Tween動(dòng)畫之RotateAnimation實(shí)現(xiàn)圖片不停旋轉(zhuǎn)效果實(shí)例介紹
相關(guān)文章
java實(shí)體對(duì)象與Map之間的轉(zhuǎn)換工具類代碼實(shí)例
這篇文章主要介紹了java實(shí)體對(duì)象與Map之間的轉(zhuǎn)換工具類代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Feign?日期格式轉(zhuǎn)換錯(cuò)誤的問(wèn)題
這篇文章主要介紹了Feign?日期格式轉(zhuǎn)換錯(cuò)誤的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java的JSON格式轉(zhuǎn)換庫(kù)GSON的初步使用筆記
GSON是Google開發(fā)并在在GitHub上開源的Java對(duì)象與JSON互轉(zhuǎn)功能類庫(kù),在Android開發(fā)者中也大受歡迎,這里我們就來(lái)看一下Java的JSON格式轉(zhuǎn)換庫(kù)GSON的初步使用筆記:2016-06-06詳解SpringMVC驗(yàn)證框架Validation特殊用法
本篇文章主要介紹了詳解SpringMVC驗(yàn)證框架Validation特殊用法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02淺談Java異常的Exception e中的egetMessage()和toString()方法的區(qū)別
下面小編就為大家?guī)?lái)一篇淺談Java異常的Exception e中的egetMessage()和toString()方法的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07springmvc配置線程池Executor做多線程并發(fā)操作的代碼實(shí)例
今天小編就為大家分享一篇關(guān)于springmvc配置線程池Executor做多線程并發(fā)操作的代碼實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12Spring?Cloud?Sleuth?和?Zipkin?進(jìn)行分布式跟蹤使用小結(jié)
分布式跟蹤是一種機(jī)制,我們可以使用它跟蹤整個(gè)分布式系統(tǒng)中的特定請(qǐng)求,分布式跟蹤允許您跟蹤分布式系統(tǒng)中的請(qǐng)求,本文給大家介紹Spring?Cloud?Sleuth?和?Zipkin?進(jìn)行分布式跟蹤使用小結(jié),感興趣的朋友一起看看吧2022-03-03MyBatis-Flex實(shí)現(xiàn)分頁(yè)查詢的示例代碼
在MyBatis-Flex中實(shí)現(xiàn)分頁(yè)查詢時(shí),需要注意維護(hù)一個(gè)獲取數(shù)據(jù)庫(kù)總數(shù)的方法,詳細(xì)介紹了UserService、UserServiceImpl類以及Mapper.xml配置,感興趣的可以了解一下2024-10-10