บทนำ
ตัวอย่างนี้สาธิตการวาดเส้นบน Canvas โดยใช้ Bitmap และ Paint ใน Android ด้วย Kotlin
ขั้นตอนที่ 1: สร้างโปรเจกต์
สร้างโปรเจกต์ Android ใหม่ใน Android Studio
ขั้นตอนที่ 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:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp"
android:text="Draw Line" />
</RelativeLayout>
ขั้นตอนที่ 3: MainActivity.kt
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var button: Button
private lateinit var imageView: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "Draw Line Demo"
button = findViewById(R.id.button)
imageView = findViewById(R.id.imageView)
button.setOnClickListener {
// สร้าง Bitmap ขนาด 400x400 พิกเซล
val bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
// ล้างพื้นหลังสีขาว
canvas.drawColor(Color.WHITE)
// ตั้งค่า Paint
val paint = Paint().apply {
color = Color.BLUE
style = Paint.Style.STROKE
strokeWidth = 8f
isAntiAlias = true
strokeCap = Paint.Cap.ROUND
}
// วาดเส้นทแยงมุม
val offset = 50f
canvas.drawLine(
offset, offset,
canvas.width - offset, canvas.height - offset,
paint
)
// วาดเส้นแนวนอนกึ่งกลาง
paint.color = Color.RED
paint.strokeWidth = 4f
canvas.drawLine(
offset, canvas.height / 2f,
canvas.width - offset, canvas.height / 2f,
paint
)
imageView.setImageBitmap(bitmap)
}
}
}
ขั้นตอนที่ 4: Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.drawline">
<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>
แนวคิดสำคัญ
- Bitmap - พื้นผิววาดภาพในหน่วยความจำ
- Canvas - ผืนผ้าใบสำหรับวาด (เก็บคำสั่งวาด)
- Paint - แปรง/ปากกา กำหนดสี ความหนา รูปแบบ
- drawLine(x1, y1, x2, y2, paint) - วาดเส้นตรงจากจุด (x1,y1) ไป (x2,y2)
การประยุกต์ใช้ขั้นสูง
- วาดกราฟ/ชาร์ทด้วย Path และ drawPath()
- วาดรูปทรงต่างๆ: drawRect, drawCircle, drawOval
- ใช้ Shader สำหรับ Gradient, Bitmap texture
- Custom View โดย override onDraw() แทนการวาดบน Bitmap