程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 基於RxPaparazzo實現圖片裁剪、圖片旋轉、比例放大縮小功能

基於RxPaparazzo實現圖片裁剪、圖片旋轉、比例放大縮小功能

編輯:關於JAVA

前言:基於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;

 /* @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);//解除綁定
 }
}

源碼下載

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved