728x90
아래 그림과 같이 도형을 선택하는 라디오 버튼을 만들고, 선택한 도형에 따라 터치할 때 그린다.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<view
android:id="@+id/view"
class="com.example.myapplication.MyView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:layout_constraintBottom_toTopOf="@+id/radioGp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="8dp" />
<RadioGroup
android:id="@+id/radioGp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent">
<RadioButton
android:id="@+id/radio_Rectangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="150dp"
android:checked="true"
android:text="@string/rectangle" />
<RadioButton
android:id="@+id/radio_Circle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/circle" />
</RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.myapplication.databinding.ActivityMainBinding
class MainActivity() : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.radioGp.setOnCheckedChangeListener { _, checkedId ->
when (checkedId) {
R.id.radio_Circle -> binding.view.setFigure("circle")
R.id.radio_Rectangle -> binding.view.setFigure("rect")
}
}
}
}
MyView
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
class MyView : View {
private var rect = Rect(10, 10, 110, 110)
private var circleX = 100F
private var circleY = 100F
private var circleR = 50F
private var color = Color.BLUE
private val paint = Paint()
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
private var newFigure: String = "rect"
fun setFigure(figure: String) {
newFigure = figure
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
paint.color = color
when (newFigure) {
"circle" -> canvas.drawCircle(circleX, circleY, circleR, paint)
"rect" -> canvas.drawRect(rect, paint)
}
}
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
if (event.action == MotionEvent.ACTION_DOWN ||
event.action == MotionEvent.ACTION_MOVE
) {
rect.left = event.x.toInt()
rect.top = event.y.toInt()
rect.right = rect.left + 100
rect.bottom = rect.top + 100
circleX = event.x
circleY = event.y
invalidate()
return true
}
return super.onTouchEvent(event)
}
}
간단한 추가실습이었다.
728x90
'Android > Study' 카테고리의 다른 글
[Android] MVP 패턴이란? (0) | 2023.01.01 |
---|---|
[Android] MVC 패턴이란? (0) | 2023.01.01 |
[Android] Notification(노티피케이션) (0) | 2022.10.18 |
[Android] CustomView(커스텀 뷰) (0) | 2022.09.07 |
[Android] 안드로이드 취업시 회사에서 요구하는 기본적인 사항 (0) | 2022.09.04 |