###前提・実現したいこと
Xamarin.Androido(VisualStudio 2015)での質問です。
Androidの画面のタップした位置(座標)を取得する方法を教えてください。
DrawBitmapでクリップして作成された画像の配列をタップすると、その位置の画像がimg1からimg2の物に切り替わるというプログラムを作成しています。
カードの裏表を切り替えるイメージです。
少なくとも、タップした位置の座標を取得できれば、あとは計算でどのカードが
タップされたかは割り出せるかと考えました。
なので、画面のタップ位置を取得する方法を教えていただきたいです。
下記のプログラムは、img1の状態で並べて表示する所まで作成しています。
タップされたら、その部分の画像をimg2に切り替えることで、
rect1をいじらずに変更したいと考えています。
(最終的には、rect幅を画面に合わせて入力し、
カードの並びをランダムにしますが、それは今は不要です)
初歩的な質問で申し訳ありませんが、回答をよろしくおねがいします。
また、質問に至らない点がありましたら、ご指摘をお願いします。
###該当のソースコード
namespace App { [Activity(Label = "tes", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); RequestWindowFeature(WindowFeatures.NoTitle); SetContentView(new Class1(this)); } } } namespace App { public class Class1 : View { Bitmap Img1 = null; Bitmap Img2 = null; int[,] card = new int[8,8]; public Class1(Context context) : base(context) { var Image1 = context.Resources.GetIdentifier("Image1", "drawable", context.PackageName); Img1 = BitmapFactory.DecodeResource(context.Resources, Image1); var Image2 = context.Resources.GetIdentifier("Image2", "drawable", context.PackageName); Img2 = BitmapFactory.DecodeResource(context.Resources, Image2); for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { card[x, y] = 0; } } } public override void Draw(Canvas canvas) { base.Draw(canvas); for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { rect1 = new Rect(x * 100, y * 100, (x * 100) + 100, (y * 100) + 100); rect2 = new Rect(x * 50, y * 50, (x * 50) + 50, (y * 50) + 50); switch (card[x, y]) { case 0: canvas.DrawBitmap(Img1, rect1, rect2, null); break; case 1: canvas.DrawBitmap(Img2, rect1, rect2, null); break; } } } } } }
###試したこと
google等で調べ、MotionEvent、OnTouchEventなどのクラス、メソットの存在はわかりましたが、
javaやIos、Xamarin.Formsの資料ばかりで、構文がよく分かりませんでした。
表示する画像の数が多いので(さらに増やす可能性もあり)、ImageViewを使うやり方は避けたほうが良いかと思い、このコードを作成しました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/10 13:51