実現したいこと
タップした部分に画像を描画できているのですが右下にずれます。
タップした部分が画像の中央座標となるように描画したいです。
ある記事では、タップした部分は画像の左上座標を示すため右下にずれる・・・と記述があったので、32*32の小さな画像を用意しましたがズレ具合は変わりませんでした。
このズレは何からきているのでしょうか。
またズレはどのように調整すればよいのでしょうか。
ソースコード
onTouchEventで画像を描画する座標をセットしています。
img1.pngの解像度は32*32の小さいファイルです。
activity_main.xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 tools:ignore="UsingOnClickInXml" 6 tools:context=".MainActivity" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 android:id="@+id/mainActivity" 10 > 11 12 <ImageView 13 android:id="@+id/img1" 14 android:layout_width="100px" 15 android:layout_height="100px" 16 android:src="@drawable/img1" 17 tools:ignore="MissingConstraints" 18 /> 19</androidx.constraintlayout.widget.ConstraintLayout>
MianActivity.kt
1private var x: Float = 0f 2private var y: Float = 0f 3 4override fun onTouchEvent(event: MotionEvent?): Boolean { 5 x = event!!.x 6 y = event.y 7 binding.img1.x = x 8 binding.img1.y = y 9 return true 10}
回答1件
あなたの回答
tips
プレビュー