如何使用 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 次瀏覽

開啟你的 事業

透過完成課程獲得認證

開始
廣告