###実現したいこと
onDrawで描いた図形に当たり判定を設けて連動させたいです。
###困っていること
なぜか当たり判定が行われるところがずれているのと、円の当たり判定が全くなされないです。
###該当のソースコード
全文です
Java
1import android.app.Activity; 2import android.content.Context; 3import android.content.pm.ActivityInfo; 4import android.graphics.Canvas; 5import android.graphics.Color; 6import android.graphics.Paint; 7import android.graphics.Point; 8import android.graphics.RectF; 9import android.os.Bundle; 10import android.view.Display; 11import android.view.MotionEvent; 12import android.view.View; 13import android.view.WindowManager; 14import android.widget.FrameLayout; 15import android.widget.LinearLayout; 16import android.widget.TextView; 17 18import static java.lang.Math.pow; 19 20public class Main extends Activity { 21 Screen sc; 22 private TextView textView; 23 private int winW, winH; 24 private int winW2,winH2; 25 private int colchk = 0; 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 30 //setContentView(R.layout.activity_main); 31 32 sc = new Screen(this); 33 parts(); 34 } 35 public void parts() { 36 FrameLayout fl = new FrameLayout(this); 37 this.setContentView(fl); 38 fl.addView(sc); 39 LinearLayout ll = new LinearLayout(this); 40 ll.setOrientation(LinearLayout.VERTICAL); 41 fl.addView(ll); 42 String action = "Touch Event"; 43 textView = new TextView(this); 44 textView.setText(action); 45 textView.setTextSize(36.0f); 46 ll.addView(textView); 47 } 48 @Override 49 public boolean onTouchEvent(MotionEvent event) { 50 float tx = event.getX(); 51 float ty = event.getY(); 52 WindowManager wm2 = (WindowManager)getSystemService(WINDOW_SERVICE); 53 Display dp2 = wm2.getDefaultDisplay(); 54 Point po2 = new Point(); 55 dp2.getSize(po2); 56 winW2 = po2.x; 57 winH2 = po2.y; 58 switch (event.getAction() & MotionEvent.ACTION_MASK) { 59 case MotionEvent.ACTION_DOWN: 60 if (pow(tx - (winW - 120),2) + pow(ty - (winH - 110),2) < 2500 || pow(tx - 120,2) + pow(ty - (winH - 110),2) < 2500 || (70 <= tx && tx <= 270) && (winH - 160 <= ty && ty <= winH - 60) || (winW - 270 <= tx && tx <= winW - 70) && (winH - 160 <= ty && ty <= winH - 60)){ 61 sc.colchange(); 62 }else{ 63 sc.colreturn(); 64 } 65 break; 66 case MotionEvent.ACTION_MOVE: 67 if (pow(tx - (winW - 120),2) + pow(ty - (winH - 110),2) < 2500 || pow(tx - 120,2) + pow(ty - (winH - 110),2) < 2500 || (70 <= tx && tx <= 270) && (winH - 160 <= ty && ty <= winH - 60) || (winW - 270 <= tx && tx <= winW - 70) && (winH - 160 <= ty && ty <= winH - 60)){ 68 sc.colchange(); 69 }else{ 70 sc.colreturn(); 71 } 72 break; 73 case MotionEvent.ACTION_UP: 74 sc.colreturn(); 75 break; 76 } 77 textView.setText("x:" + tx + "y:" + ty); 78 textView.setTextSize(14.0f); 79 return super.onTouchEvent(event); 80 } 81 public class Screen extends View { 82 public Screen(Context context) { 83 super(context); 84 } 85 protected void onDraw(Canvas c) { 86 super.onDraw(c); 87 WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE); 88 Display dp = wm.getDefaultDisplay(); 89 Point po = new Point(); 90 dp.getSize(po); 91 winW = po.x; 92 winH = po.y; 93 int wxa = winW - 320; 94 int wxb = winW - 20; 95 int wya = winH - 160; 96 int wyb = winH - 60; 97 int r = 50; 98 RectF arear = new RectF(wxa, wya, wxb, wyb); 99 RectF areal = new RectF(20,wya,320,wyb); 100 Paint p = new Paint(); 101 c.drawColor(Color.WHITE); 102 p.setARGB(100,50,215,50); 103 if (colchk == 1){ 104 p.setARGB(100,200,50,50); 105 colchk = 0; 106 } 107 p.setStyle(Paint.Style.FILL); 108 c.drawRoundRect(arear,r,r,p); 109 c.drawRoundRect(areal,r,r,p); 110 } 111 public void colchange() { 112 colchk = 1; 113 invalidate(); 114 } 115 public void colreturn() { 116 colchk = 0; 117 invalidate(); 118 } 119 } 120}
以下で描画しています
Java
1WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE); 2 Display dp = wm.getDefaultDisplay(); 3 Point po = new Point(); 4 dp.getSize(po); 5 winW = po.x; 6 winH = po.y; 7 int wxa = winW - 320; 8 int wxb = winW - 20; 9 int wya = winH - 160; 10 int wyb = winH - 60; 11 int r = 50; 12 RectF arear = new RectF(wxa, wya, wxb, wyb); 13 RectF areal = new RectF(20,wya,320,wyb); 14 Paint p = new Paint(); 15 c.drawColor(Color.WHITE); 16 p.setARGB(100,50,215,50); 17 if (colchk == 1){ 18 p.setARGB(100,200,50,50); 19 colchk = 0; 20 } 21 p.setStyle(Paint.Style.FILL); 22 c.drawRoundRect(arear,r,r,p); 23 c.drawRoundRect(areal,r,r,p);
以下が当たり判定の部分です
Java
1if (pow(tx - (winW2 - 120),2) + pow(ty - (winH2 - 110),2) < 2500 || pow(tx - 120,2) + pow(ty - (winH2 - 110),2) < 2500 || 70 <= tx && tx <= 270 && winH2 - 160 <= ty && ty <= winH2 - 60 || winW2 - 270 <= tx && tx <= winW2 - 70 && winH2 - 160 <= ty && ty <= winH2 - 60){ 2 sc.colchange(); 3 }else{ 4 sc.colreturn(); 5 } 6 break; 7 case MotionEvent.ACTION_MOVE: 8 if (pow(tx - (winW2 - 120),2) + pow(ty - (winH2 - 110),2) < 2500 || pow(tx - 120,2) + pow(ty - (winH2 - 110),2) < 2500 || 70 <= tx && tx <= 270 && winH2 - 160 <= ty && ty <= winH2 - 60 || winW2 - 270 <= tx && tx <= winW2 - 70 && winH2 - 160 <= ty && ty <= winH2 - 60){ 9 sc.colchange(); 10 }else{ 11 sc.colreturn(); 12 }
###補足
感覚ですが、四角い当たり判定のゾーンが存在しているのですが、その位置が描かれるものと数十ずれていると思われます。半円の部分が当たり判定されていないので円が当たり判定されていないのかと思われます。
###修正点
・クリック処理を無くしました
・ORとANDにおいて()でくくるべき箇所を()でくくりました。
回答3件
あなたの回答
tips
プレビュー