如何使用 Kotlin 在 Android 中从位图中裁剪圆形区域?

kotlin mobile developmentapps/applications

本示例演示了如何使用 Kotlin 在 Android 中从位图中裁剪圆形区域。

步骤 1 − 在 Android Studio 中创建一个新项目,转到"文件"->"新建项目",并填写所有必需的详细信息以创建新项目。

步骤 2 − 将以下代码添加到 res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#edf2ea"
   android:padding="8dp"
   tools:context=".MainActivity">
   <ImageView
      android:id="@+id/iv"
      android:layout_width="match_parent"
      android:layout_height="500dp"
      android:layout_centerInParent="true" />
   <Button
      android:id="@+id/btn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentEnd="true"
      android:layout_alignParentBottom="true"
      android:text="Crop It" />
</RelativeLayout>

步骤 3 − 将以下代码添加到 src/MainActivity.kt

import android.graphics.*
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import kotlin.math.min
class MainActivity : AppCompatActivity() {
   lateinit var button: Button
   lateinit var imageView: ImageView
   lateinit var bitmap: Bitmap
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      title = "KotlinApp"
      imageView = findViewById(R.id.iv)
      button = findViewById(R.id.btn)
      // 获取位图资源 ID
      // 获取位图资源 ID
      val bitmapResourceID: Int = R.drawable.image
      // 将图片设置为 ImageView
      // 将图片设置为 ImageView
      imageView.setImageBitmap(BitmapFactory.decodeResource(resources, bitmapResourceID))
      // 为 Button 小部件设置点击监听器
      // 为 Button 小部件设置点击监听器
      button.setOnClickListener {
         bitmap = BitmapFactory.decodeResource(resources, bitmapResourceID)
         // Create a circular bitmap
         bitmap = getCircularBitmap(bitmap)
         imageView.setImageBitmap(bitmap)
      }
   }
   private fun getCircularBitmap(srcBitmap: Bitmap?): Bitmap {
      val squareBitmapWidth = min(srcBitmap!!.width, srcBitmap.height)
      // 初始化一个新的 Bitmap 实例
      // 初始化一个新的 Bitmap 实例
      val dstBitmap = Bitmap.createBitmap(
         squareBitmapWidth,  // 宽度
         squareBitmapWidth,  // 高度
         Bitmap.Config.ARGB_8888 // 配置
      )
      val canvas = Canvas(dstBitmap)
      // 初始化一个新的 Paint 实例
      // 初始化一个新的 Paint 实例
      val paint = Paint()
      paint.isAntiAlias = true
      val rect = Rect(0, 0, squareBitmapWidth, squareBitmapWidth)
      val rectF = RectF(rect)
      canvas.drawOval(rectF, paint)
      paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
      // 计算复制位图的左侧和顶部
      // 计算复制位图的左侧和顶部
      val left = ((squareBitmapWidth - srcBitmap.width) / 2).toFloat()
      val top = ((squareBitmapWidth - srcBitmap.height) / 2).toFloat()
      canvas.drawBitmap(srcBitmap, left, top, paint)
      // 释放与此位图关联的原生对象。
      // 释放与此位图关联的原生对象。
      srcBitmap.recycle()
      // 返回圆形位图
      // 返回圆形位图
      return dstBitmap
   }
}

步骤 4 − 将以下代码添加到 androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.q11">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

让我们尝试运行您的应用程序。假设您已将实际的 Android 移动设备连接到电脑。要从 Android Studio 运行该应用程序,请打开项目的某个活动文件,然后点击工具栏上的"运行"图标 Play 。选择您的移动设备作为选项,然后检查将显示默认屏幕的移动设备。

点击此处下载项目代码。


相关文章