Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีหมุนรูปภาพใน ImageView ด้วยมุมที่กำหนดใน Android Kotlin

บทนำ

ตัวอย่างนี้แสดงวิธีหมุนรูปภาพใน ImageView โดยตั้งค่า property rotation ใน Kotlin

ขั้นตอนที่ 1: สร้างโปรเจกต์

สร้างโปรเจกต์ Android ใหม่ และวางไฟล์รูปภาพ (เช่น image.jpg) ในโฟลเดอร์ res/drawable/

ขั้นตอนที่ 2: Layout (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="2dp"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:src="@drawable/image"
        android:scaleType="centerInside"
        android:adjustViewBounds="true" />

    <Button
        android:id="@+id/btnRotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:layout_marginBottom="20dp"
        android:text="Rotate 90°"
        android:textStyle="bold" />
</RelativeLayout>

ขั้นตอนที่ 3: MainActivity.kt

import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var imageView: ImageView
    private lateinit var btnRotate: Button
    private var currentRotation = 0f

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "Image Rotation"

        imageView = findViewById(R.id.imageView)
        btnRotate = findViewById(R.id.btnRotate)

        btnRotate.setOnClickListener {
            currentRotation = (currentRotation + 90f) % 360f
            imageView.rotation = currentRotation
            btnRotate.text = "Rotate ${currentRotation.toInt()}°"
        }
    }
}

ขั้นตอนที่ 4: Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.imagerotation">
    <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>

Property การหมุนที่มีประโยชน์

Propertyคำอธิบาย
rotationหมุนในแกน Z (องศา) - ใช้ในตัวอย่างนี้
rotationXหมุนในแกน X (3D effect)
rotationYหมุนในแกน Y (3D effect)
pivotX, pivotYจุดศูนย์กลางการหมุน (default: center)

การหมุนแบบ Animation

// หมุนแบบ Smooth Animation
imageView.animate()
    .rotation(currentRotation + 90f)
    .setDuration(300)
    .start()

// หรือใช้ ObjectAnimator
ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f)
    .setDuration(1000)
    .start()

หมุน Bitmap โดยตรง (ถ้าต้องการบันทึกไฟล์)

fun rotateBitmap(source: Bitmap, degrees: Float): Bitmap {
    val matrix = Matrix().apply { postRotate(degrees) }
    return Bitmap.createBitmap(source, 0, 0, source.width, source.height, matrix, true)
}