用函数参数替换重复的代码

我有以下代码,其中包含很多重复..

override fun onOptionsItemSelected(item: MenuItem?): Boolean { when (item?.itemId) { R.id.action_crop -> { val bitmap = capturedReceiptImageView.bitmap val bitmapCropped = BitmapHelper.cropBitmap(bitmap, capturedReceiptImageView.getDimensions()) capturedReceiptImageView.setImageBitmap(bitmapCropped) capturedReceiptImageView.invalidate() bitmap.recycle() } R.id.action_rotate_left -> { val bitmap = capturedReceiptImageView.bitmap val bitmapRotated = BitmapHelper.rotateBitmap(bitmap, -90.0f) capturedReceiptImageView.setImageBitmap(bitmapRotated) capturedReceiptImageView.invalidate() bitmap.recycle() } R.id.action_rotate_right -> { val bitmap = capturedReceiptImageView.bitmap val bitmapRotated = BitmapHelper.rotateBitmap(bitmap, 90.0f) capturedReceiptImageView.setImageBitmap(bitmapRotated) capturedReceiptImageView.invalidate() bitmap.recycle() } R.id.action_increase_contrast -> { val bitmap = capturedReceiptImageView.bitmap val bitmapChangedContrast = BitmapHelper.changeBitmapContrast(bitmap, 1.10f) capturedReceiptImageView.setImageBitmap(bitmapChangedContrast) capturedReceiptImageView.invalidate() bitmap.recycle() } R.id.action_decrease_contrast -> { val bitmap = capturedReceiptImageView.bitmap val bitmapChangedContrast = BitmapHelper.changeBitmapContrast(bitmap, 0.90f) capturedReceiptImageView.setImageBitmap(bitmapChangedContrast) capturedReceiptImageView.invalidate() bitmap.recycle() } R.id.action_save -> { val bitmap = capturedReceiptImageView.bitmap BitmapHelper.saveBitmap(bitmap, capturedReceiptUri) } else -> { return false } } return true } 

我想要一个函数,这需要另一个函数通过函数rotateBitmap changeBitmapContrast等这应该在BitmapHelper中调用这个函数的重复,并返回位图

我怎样才能改变我的代码来完成呢?

2 Solutions collect form web for “用函数参数替换重复的代码”

 fun applyBitmapOperation(imageView: ImageView): ((Bitmap) -> Bitmap) -> Unit { val bitmap = imageView.bitmap return { function -> val resultBitmap = function(bitmap) capturedReceiptImageView.setImageBitmap(resultBitmap) capturedReceiptImageView.invalidate() bitmap.recycle() } } override fun onOptionsItemSelected(item: MenuItem?): Boolean { val apply = applyBitmapOperation(capturedReceiptImageView) when (item?.itemId) { R.id.action_crop -> { apply { bitmap -> BitmapHelper.cropBitmap(bitmap, capturedReceiptImageView.getDimensions()) } } R.id.action_rotate_left -> { apply { bitmap -> BitmapHelper.rotateBitmap(bitmap, -90.0f) } } R.id.action_rotate_right -> { apply { bitmap -> BitmapHelper.rotateBitmap(bitmap, 90.0f) } } R.id.action_increase_contrast -> { apply { bitmap -> BitmapHelper.changeBitmapContrast(bitmap, 1.10f) } } R.id.action_decrease_contrast -> { apply { bitmap -> BitmapHelper.changeBitmapContrast(bitmap, 0.90f) } } R.id.action_save -> { val bitmap = capturedReceiptImageView.bitmap BitmapHelper.saveBitmap(bitmap, capturedReceiptUri) } else -> { return false } } return true } 

我能够得到这个与以下工作..

  private fun applyBitmapOperation(predicate: KFunction2<@ParameterName(name = "bitmap") Bitmap, @ParameterName(name = "changeAmount") Float, Bitmap>, changeAmount: Float) { val bitmap = capturedReceiptImageView.bitmap val bitmapChanged = predicate.invoke(bitmap, changeAmount) capturedReceiptImageView.setImageBitmap(bitmapChanged) capturedReceiptImageView.invalidate() bitmap.recycle() } 

像这样称呼

 applyBitmapOperation(::changeBitmapContrast, 1.10f) 
  • 为什么var foo = null编译
  • 在Kotlin中的记忆function
  • Kotlin与Java:使用lambda参数调用函数
  • Kotlin:在Activity中使用BroadcastReceiver时未解析的本地类
  • 辛特尔顿在Kotlin有争论
  • 匕首+ Kotlin不注射
  • Kotlin有垃圾收集器吗? 如果是这样,基于哪种算法?
  • 我可以在没有`this`的情况下在自己的class级中应用中缀函数吗?
  • Kotlin递归
  • 两个构造函数有什么不同?
  • RecyclerView - 正确实施SnackBar UNDO删除(Kotlin)
  • Kotlin language will be the best programming language for Android.