我对Android开发相对较新。
但我想知道是否可以移动这些图像,因此它们可以在网格中相互交换。因为这是我试图实现的棋盘游戏。
这就是它的样子:http://i.imgur.com/2shAuGd.png
图像数组的代码:
private Integer[] mThumbIds = {
R.drawable.blank, R.drawable.blank, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.blank, R.drawable.blank,
R.drawable.blank, R.drawable.blank, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.blank, R.drawable.blank,
R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.peg,
R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.empty, R.drawable.peg, R.drawable.peg, R.drawable.peg,
R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.peg,
R.drawable.blank, R.drawable.blank, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.blank, R.drawable.blank,
R.drawable.blank, R.drawable.blank, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.blank, R.drawable.blank,
};
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="40dp"
android:columnWidth="50dp"
android:numColumns="7"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
答案 0 :(得分:1)
如果你不需要拖放,这应该很简单。
private View firstClicked = null;
private OnClickListener swapImages = new OnClickListener() {
@Override
public void onClick(View v) {
if(firstClicked != null){
LayoutParams p1 = firstClicked.getLayoutParams();
LayoutParams p2 = v.getLayoutParams();
v.setLayoutParams(p1);
firstClicked.setLayoutParams(p2);
firstClicked = null;
}
else
firstClicked = v;
}
};
这样你只需交换他们的位置,而不是他们内部的图像。
编辑:但如果这会干扰您的游戏逻辑,您只需交换而不是布局参数,您只需交换他们的图像。
编辑:此代码交换您的drawable,您需要创建自己的逻辑
package com.example.stackoverflow;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.Activity;
import android.graphics.drawable.Drawable;
public class Game extends Activity {
private ImageView firstClicked = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(Game.this, "" + position, Toast.LENGTH_SHORT).show();
if(firstClicked != null){
ImageView v2 = (ImageView)v;
Drawable d2 = v2.getDrawable();
Drawable d1 = firstClicked.getDrawable();
v2.setImageDrawable(d1);
firstClicked.setImageDrawable(d2);
firstClicked = null;
}
else {
firstClicked = (ImageView)v;
}
}
});
}
}
希望这有助于并享受您的工作。