前言:基于RxPaparazzo的圖片裁剪,圖片旋轉(zhuǎn)、比例放大|縮小。
成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的武川網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
效果:
開(kāi)發(fā)環(huán)境:AndroidStudio2.2.1+gradle-2.14.1
涉及知識(shí):
1.Material Design
(CardView+CoordinatorLayout+AppBarLayout+NestedScrollView+CollapsingToolbarLayout+Toolbar+FloatingActionButton)使用
2.butterknife注解式開(kāi)發(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í)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前標(biāo)題:基于RxPaparazzo實(shí)現(xiàn)圖片裁剪、圖片旋轉(zhuǎn)、比例放大縮小功能
網(wǎng)站鏈接:http://aaarwkj.com/article14/gjipde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、服務(wù)器托管、軟件開(kāi)發(fā)、建站公司、網(wǎng)站設(shè)計(jì)、網(wǎng)站營(yíng)銷(xiāo)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)