图片裁剪(基于RxPaparazzo)
来源:互联网 发布:斯伦贝谢软件研发中心 编辑:程序博客网 时间:2024/06/17 08:45
图片裁剪(基于RxPaparazzo)
前言:基于RxPaparazzo的图片裁剪,图片旋转、比例放大|缩小。
效果:
开发环境:AndroidStudio2.2.1+gradle-2.14.1
引入依赖:
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.Material Design(CardView+CoordinatorLayout+AppBarLayout+NestedScrollView+
CollapsingToolbarLayout+Toolbar+FloatingActionButton)使用
2.butterknife注解式开发
3.基于RxJava+RxAndroid的RxPaparazzo使用
部分代码:
public class MainActivity extends AppCompatActivity { @Bind(R.id.iv_appbar) ImageView iv_appbar; @Bind(R.id.main_toolbar) Toolbar toolbar; /* @BindView(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> mainActivityFileDataResponse) throws Exception { if (mainActivityFileDataResponse.resultCode() == Activity.RESULT_OK) { File filePath = mainActivityFileDataResponse.data().getFile(); Bitmap bitmap = BitmapFactory.decodeFile(filePath.getPath()); iv_appbar.setImageBitmap(bitmap); } else if (mainActivityFileDataResponse.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);//解除绑定 }
源码下载...
0 0
- 图片裁剪(基于RxPaparazzo)
- 基于angular图片裁剪
- 基于MFC简单图片裁剪工具
- 基于Flex的裁剪图片功能封装
- 基于Flex的裁剪图片功能封装
- 基于ucrop实现图片裁剪需求
- 图片裁剪
- 裁剪图片
- 图片裁剪
- 图片裁剪
- 裁剪图片
- 裁剪图片
- 图片裁剪
- 图片裁剪
- 图片裁剪
- 图片裁剪
- 图片裁剪
- 图片裁剪
- 配置数据库认证单点登录
- 简单的程序诠释C++ STL算法系列之十五:swap ---》利用swap()函数进行内存的释放
- 用python画折线图、柱状图、饼图
- CDoj 1324<分块>
- dom4j生成xml格式(微信回复消息格式)
- 图片裁剪(基于RxPaparazzo)
- tree指令的用法
- 工作中的经验教训
- Spring的Bean配置项
- 基于DragonBoard 410c的智能门铃系列二之软件总框架
- 时间复杂度和空间复杂度详解
- SQL 拼接多个字段的值&一个字段多条记录的拼接
- Java Dataoutputstream包装流
- PowerDesigner怎样才能在修改表的字段Name的时候Code不自动跟着变