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


本示例演示如何使用 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)
      // Get the bitmap resource id
      // Get the bitmap resource id
      val bitmapResourceID: Int = R.drawable.image
      // Set an image to ImageView
      // Set an image to ImageView
      imageView.setImageBitmap(BitmapFactory.decodeResource(resources, bitmapResourceID))
      // Set a click listener for Button widget
      // Set a click listener for Button widget
      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)
      // Initialize a new instance of Bitmap
      // Initialize a new instance of Bitmap
      val dstBitmap = Bitmap.createBitmap(
         squareBitmapWidth,  // Width
         squareBitmapWidth,  // Height
         Bitmap.Config.ARGB_8888 // Config
      )
      val canvas = Canvas(dstBitmap)
      // Initialize a new Paint instance
      // Initialize a new Paint instance
      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)
      // Calculate the left and top of copied bitmap
      // Calculate the left and top of copied bitmap
      val left = ((squareBitmapWidth - srcBitmap.width) / 2).toFloat()
      val top = ((squareBitmapWidth - srcBitmap.height) / 2).toFloat()
      canvas.drawBitmap(srcBitmap, left, top, paint)
      // Free the native object associated with this bitmap.
      // Free the native object associated with this bitmap.
      srcBitmap.recycle()
      // Return the circular bitmap
      // Return the circular bitmap
      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 运行应用,请打开项目的一个活动文件,然后单击工具栏中的运行图标 播放 。将你的移动设备作为选项,然后查看将显示你的默认屏幕的移动设备。

单击 此处 下载项目代码。

更新于:28-11-2020

663 次浏览

开启你的 事业

通过完成课程获得认证

开始
广告
© . All rights reserved.