ドラッグアンドドロップすることで数字を入れ替えるシステムを作りたいと考えています。
この状態では指が触れたマス目が色が付き、ランダムでマス目が振動するものです。
これを改良して(おそらくonTouchEventのところでしょうか?)数字が入れ替わる仕組みの作り方を教えていただけますでしょうか。
java初心者なので全然わかりません。アルゴリズム等アドバイスいただけたらと思います。よろしくお願いします。
java
1package com.example.admin.a0116myapplication; 2 3import android.graphics.Color; 4import android.graphics.Point; 5import android.graphics.Rect; 6import android.os.Vibrator; 7import android.support.v7.app.AppCompatActivity; 8import android.os.Bundle; 9import android.util.Log; 10import android.view.Display; 11import android.view.Gravity; 12import android.view.MotionEvent; 13import android.view.View; 14import android.view.WindowManager; 15import android.widget.TableLayout; 16import android.widget.TableRow; 17import android.widget.TextView; 18 19import org.w3c.dom.Text; 20 21import java.util.Random; 22 23public class MainActivity extends AppCompatActivity { 24 25 int COL=4; 26 int ROW=4; 27 int ON_VIB_NUM=4; 28 int viewId[] = new int[COL*ROW+1]; 29 30 /* vibがオンの番号 */ 31 int onVib[] = new int[ON_VIB_NUM]; 32 33 //最後にタッチしたViewの列番号と行番号を保持 34 private int fromRow = -1; 35 private int fromCol = -1; 36 private int fromId = -1; 37 38 //現在タッチしたViewに書いてある番号を保持 39 private int toRow = -1; 40 private int toCol = -1; 41 private int toId = -1; 42 43 MainActivity(){ 44 Random rand = new Random(); 45 for(int i = 0; i<3; i++){ 46 onVib[i] = rand.nextInt(16)+1; 47 48 int x = onVib[i]; 49 for(i = 0; i<16; i++){ 50 if(onVib[i]==x) 51 break; 52 } 53 } 54 } 55 56 @Override 57 protected void onCreate(Bundle savedInstanceState) { 58 super.onCreate(savedInstanceState); 59 setContentView(R.layout.activity_main); 60 61 /*端末の画面の幅を取得*/ 62 WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE); 63 Display disp = wm.getDefaultDisplay(); 64 Point size = new Point(); 65 disp.getSize(size); 66 int mainWidth = size.x; 67 68 /* ROW*COLテンキーの外枠のインスタンス*/ 69 TableLayout tenKey = (TableLayout) findViewById(R.id.ten_key); 70 for(int numCol=0;numCol<COL;numCol++){ 71 /* テンキーの行部分のインスタンス*/ 72 TableRow tableRow = new TableRow(this); 73 for(int numRow=1;numRow<ROW+1;numRow++){ 74 /* テンキーの要素のインスタンスの生成と番号付け */ 75 TextView tenKeyTextView = (TextView) getLayoutInflater().inflate(R.layout.ten_key_button,null); 76 tenKeyTextView.setText(String.valueOf(numCol*COL+numRow)); 77 78 /* テンキー要素にIDを付与 */ 79 viewId[numCol*COL+numRow] = tenKeyTextView.generateViewId(); 80 tenKeyTextView.setId(viewId[numCol*COL+numRow]); 81 82 /* 画面幅よりテンキーの要素ごとの大きさを設定 */ 83 tenKeyTextView.setWidth((int)((float)mainWidth/(float)(COL+0.5))); 84 tenKeyTextView.setHeight((int)((float)mainWidth/(float)(ROW+0.5))); 85 tenKeyTextView.setGravity(Gravity.CENTER); 86 87 /* テンキーの要素を行部分に入れる*/ 88 tableRow.addView(tenKeyTextView); 89 } 90 /* テンキーの行部分をテンキーの枠に入れる */ 91 tenKey.addView(tableRow); 92 } 93 } 94 95 @Override 96 public boolean onTouchEvent(MotionEvent event) { 97 98 for(int numCol=0;numCol<COL;numCol++) { 99 for(int numRow = 1; numRow < ROW + 1; numRow++) { 100 switch (event.getAction()) { 101 case MotionEvent.ACTION_DOWN: { 102 103 if(inViewBounds(findViewById(viewId[numCol*COL+numRow]), (int)event.getX(), (int)event.getY())){ 104 105 //今タッチした所 106 fromCol = numCol+1; 107 fromRow = numRow+0; 108 fromId =viewId[numCol*COL+numRow]; 109// Log.d("fromId", String.valueOf(fromId)); 110 } 111 112 } 113 case MotionEvent.ACTION_MOVE:{ 114 115 View fromView; 116 View toView; 117 118 if(inViewBounds(findViewById(viewId[numCol*COL+numRow]), (int)event.getX(), (int)event.getY())){ 119 120 toCol = numCol+1; 121 toRow = numRow+0; 122 toId = viewId[numCol*COL+numRow]; 123// Log.d("toId", String.valueOf(viewId[numCol*COL+numRow])); 124 125 fromView = findViewById(fromId); 126 toView = findViewById(toId); 127 128 //隣のマス目にまたいだら入れ替え処理 129 if(fromId != toId){ 130 131 } 132 133 } 134 } 135 } 136 } 137 } 138 139 //各フィールド変数を更新 140// fromRow = // //; 141// fromCol = // //; 142// fromId = // //; 143// 144 return true; 145 } 146 147 /* view内に(x,y)が存在するか */ 148 private boolean inViewBounds(final View view, int x, int y){ 149 Rect outRect = new Rect(); 150 view.getDrawingRect(outRect); 151 int[] location = new int[2]; 152 view.getLocationOnScreen(location); 153 outRect.offset(location[0], location[1]); 154 //Log.d("TouchEvsyent", "X:" + location[0] + ",Y:" + location[0]); 155 return outRect.contains(x, y); 156 } 157 158 private void randOnVib(){ 159 boolean duplicate=false; 160 Random rnd = new Random(); 161 for(int i=0;i<ON_VIB_NUM;i++) { 162 while(true) { 163 onVib[i] = rnd.nextInt(COL * ROW) + 1; 164 for (int j=0;j<i;j++) { 165 if (onVib[i] == onVib[j]) { 166 duplicate = true; 167 } 168 } 169 if (!duplicate) { 170 break; 171 } 172 } 173 } 174 } 175}
java
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 android:id="@+id/main" 11 tools:context="com.example.admin.a0116myapplication.MainActivity"> 12 13 <TextView 14 android:id="@+id/textview" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:text="start"/> 18 19 <Button 20 android:id="@+id/vibAnsButton" 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" 23 android:text="ok" 24 android:layout_below="@+id/text" 25 android:layout_alignParentStart="true" /> 26 27 <TableLayout 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content" 30 android:layout_below="@id/vibAnsButton" 31 android:id="@+id/ten_key"> 32 33 34 </TableLayout> 35</RelativeLayout> 36
java
1<?xml version="1.0" encoding="utf-8"?> 2<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 > 6</TableLayout> 7
java
1<?xml version="1.0" encoding="utf-8"?> 2<TextView xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:padding="0dp" 6 > 7 8</TextView>
回答1件
あなたの回答
tips
プレビュー