線でポイントして、テキストと適切な画像を一致させるアプリを作成しようとしています。
下の画像に示されているものとまったく同じアプリを作成したいと思います。
誰か私にアイデアを教えてもらえますか?
これは私のメインクラスです:
public class MatchActivity extends Activity {
ArrayAdapter<String> listadapter;
float x1;
float y1;
float x2;
float y2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] s1 = { "smiley1", "smiley2", "smiley3" };
ListView lv = (ListView) findViewById(R.id.text_list);
ArrayList<String> list = new ArrayList<String>();
list.addAll(Arrays.asList(s1));
listadapter = new ArrayAdapter<String>(this,R.layout.rowtext, s1);
lv.setAdapter(listadapter);
GridView gv = (GridView) findViewById(R.id.image_list);
gv.setAdapter(new ImageAdapter(this));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3){
x1=v.getX();
y1=v.getY();
Log.d("list","text positions x1:"+x1+" y1:"+y1);
}
});
gv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3){
DrawView draw=new DrawView(MatchActivity.this);
x2=v.getX();
y2=v.getY();
draw.position1.add(x1);
draw.position1.add(y1);
draw.position2.add( x2);
draw.position2.add(y2);
Log.d("list","image positions x2:"+x2+" y2:"+y2);
LinearLayout ll=LinearLayout)findViewById(R.id.draw_line);
ll.addView(draw);
}
});
}
}
これは、線を引くための私の描画クラスです。
public class DrawView extends View {
Paint paint = new Paint();
private List<Float> position1=new ArrayList<Float>();
private List<Float> position2=new ArrayList<Float>();;
public DrawView(Context context) {
super(context);
invalidate();
Log.d("drawview","In DrawView class position1:"+position1+" position2:"+position2) ;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.d("on draw","IN onDraw() position1:"+position1+" position2:"+position2);
assert position1.size() == position2.size();
for (int i = 0; i < position1.size(); i += 2) {
float x1 = position1.get(i);
float y1 = position1.get(i + 1);
float x2 = position2.get(i);
float y2 = position2.get(i + 1);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawLine(x1,y1, x2,y2, paint);
}
}
}
私のレイアウトmain.xmlファイル:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<ListView
android:id="@+id/text_list"
android:layout_width="150dp"
android:layout_height="fill_parent"
/>
<LinearLayout
android:id="@+id/draw_line"
android:layout_width="150dp"
android:layout_height="fill_parent"
android:background="#cccccc" />
<GridView
android:id="@+id/image_list"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
android:columnWidth="150dp"/>
</LinearLayout>
私のLogcatの詳細:
テキストと画像の初回選択:
10-19 10:42:23.672: D/Text list(653): Clicking on text co-ordinates are:0.0 ,151.0
10-19 10:42:25.831: D/Image list(653): Clicking on image co-ordinates are:0.0 ,320.0
10-19 10:42:25.861: D/onDraw()(653): In onDraw() co-ordinates of text position:[0.0, 151.0] image position:[0.0, 320.0]
テキストと画像の2回目の選択:
10-19 10:42:58.512: D/Text list(653): Clicking on text co-ordinates are:0.0 ,302.0
10-19 10:43:00.144: D/Image list(653): Clicking on image co-ordinates are:0.0 ,0.0
10-19 10:43:00.303: D/onDraw()(653): In onDraw() co-ordinates of text position:[0.0, 151.0] image position:[0.0, 320.0]
テキストと画像の3回目の選択:
10-19 10:43:24.962: D/Text list(653): Clicking on text co-ordinates are:0.0 ,0.0
10-19 10:43:26.144: D/Image list(653): Clicking on image co-ordinates are:0.0 ,320.0
10-19 10:43:26.261: D/onDraw()(653): In onDraw() co-ordinates of text position:[0.0, 151.0] image position:[0.0, 320.0]
前もって感謝します 。