时间:2021-05-19
前言:基于RxPaparazzo的图片裁剪,图片旋转、比例放大|缩小。
效果:
开发环境:AndroidStudio2.2.1+gradle-2.14.1
涉及知识:
1.Material Design
(CardView+CoordinatorLayout+AppBarLayout+NestedScrollView+CollapsingToolbarLayout+Toolbar+FloatingActionButton)使用
2.butterknife注解式开发
3.基于RxJava+RxAndroid的RxPaparazzo使用
引入依赖:
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'部分代码:
public class MainActivity extends AppCompatActivity { @Bind(R.id.iv_appbar) ImageView iv_appbar; @Bind(R.id.main_toolbar) Toolbar toolbar; @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);//解除绑定 }}源码下载
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
上篇文章给大家介绍了vue实现裁切图片同时实现放大、缩小、旋转功能,今天给大家介绍vue.js实现点击图标放大离开缩小功能,具体代码如下所示:@-webkit-
本文实例主要展示了.Net实现上传图片按比例自动缩小或放大的方法,是非常实用的功能。分享给大家供大家参考之用。具体方法如下:///////按比例缩小图片,自动计
相比于ps,美图秀秀在等比例缩小图片上会更加方便。等比例缩小并不是裁剪图片,而是在保持原有图片所有内容的前提下缩小图片。等比例缩小图片有两个含义:一是缩小尺寸,
前言本系统是基于fabric.js实现的canvas版图片,文本编辑器,支持对图片的放大,缩小,旋转,镜面翻转,拖动,显示/隐藏图层,删除图层,替换图层等操作,
transform:scale()可以实现按比例放大或者缩小功能。transition可以设置动画执行的时间,实现缓慢或者快速的执行动画,效果图如下:源码:cs