Я занимаюсь разработкой игры памяти в Android.Использование пользовательских элементов ImageView в GridView Android
Прямо сейчас мне нужно создать сетку кепок, и я использую пользовательское изображение как элементы в сетке.
Это мой класс объектов класса, который расширяет изображение.
public class DhakkanObject extends ImageView {
public List<Fruit> fruitMap = new ArrayList<Fruit>();
public ClosedState closedState;
public OpenState openState;
public AnimationState animationState;
public State<DhakkanObject> previousState;
public State<DhakkanObject> currentState;
public DhakkanObject(Context context) {
super(context);
closedState = new ClosedState();
openState = new OpenState();
animationState = new AnimationState();
}
public void gotoState(State<DhakkanObject> newState) {
if (newState == null) {
return;
} else if (currentState == newState) {
return;
}
if (currentState == null) {
currentState = newState;
}
if (previousState != null)
previousState.exit();
previousState = currentState;
currentState = newState;
currentState.enter();
invalidate();
}
public class OpenState extends State<DhakkanObject> {
@Override
public void enter() {
setBackgroundResource(R.drawable.open);
}
@Override
public void update(long gameTime) {
// TODO Auto-generated method stub
}
@Override
public void exit() {
// TODO Auto-generated method stub
}
}
public class ClosedState extends State<DhakkanObject>
{
@Override
public void enter() {
setBackgroundResource(R.drawable.closed);
}
@Override
public void update(long gameTime) {
// TODO Auto-generated method stub
}
@Override
public void exit() {
// TODO Auto-generated method stub
}
}
public class AnimationState extends State<DhakkanObject>{
public AnimationDrawable frameByframe_animation = null;
@Override
public void enter() {
setVisibility(View.VISIBLE);
setBackgroundResource(R.anim.dhakkan_animation);
frameByframe_animation = (AnimationDrawable) getBackground();
frameByframe_animation.start();
frameByframe_animation.setOneShot(true);
}
@Override
public void update(long gameTime) {
post(new Runnable() {
public void run() {
if(frameByframe_animation.isRunning()){
gotoState(openState);
}
}
});
}
@Override
public void exit() {
frameByframe_animation.stop();
frameByframe_animation.setVisible(false, false);
}
}
}
Это моя деятельность, которая реализует GridView.
public class DoubleBlastMainActivity extends Activity {
private Intent intent = null;
public GridView gameGrid;
public List<DhakkanObject> dhakkanList = null;
public int mLevel = 0;
public UpdateThread updateThread = null;
public DhakkanObject openDhakkan1;
public DhakkanObject openDhakkan2;
public DhakkanObject currentDhakkanClicked;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.double_blast_game);
gameGrid = (GridView) findViewById(R.id.dhakkanGrid);
dhakkanList = new ArrayList<DhakkanObject>();
createDhakkanList(2, 2);
gameGrid.setAdapter(new DhakkanGrid(getBaseContext(), dhakkanList));
}
public void createDhakkanList(int numberOfCol, int numberOfRows)
{
for(int i = 0; i < (numberOfRows * numberOfCol); i++)
{
DhakkanObject temp = new DhakkanObject(getBaseContext());
temp.gotoState(temp.closedState);
temp.setScaleType(ScaleType.CENTER_CROP);
dhakkanList.add(temp);
}
gameGrid.setNumColumns(numberOfCol);
gameGrid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
}
public class DhakkanGrid extends BaseAdapter
{
public Context context;
public List<DhakkanObject> dhakkhanList;
public DhakkanGrid (Context contexttemp, List<DhakkanObject> objectlist)
{
this.context = contexttemp;
dhakkanList = objectlist;
}
public int getCount() {
// TODO Auto-generated method stub
return dhakkanList.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return dhakkanList.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return dhakkanList.get(position);
}
}
}
Код макета:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/game_bg"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/Score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/pauseMenu"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="false"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/Moves"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/pauseMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_marginRight="10dp"
android:text="Button" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
</FrameLayout>
<GridView
android:id="@+id/dhakkanGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:numColumns="3" >
</GridView>
</RelativeLayout>
</LinearLayout>
В результате очень disproportioned сетки. Значение представления изображения растягивается по ширине экспоненциально.
Можете ли вы сказать мне, что я делаю неправильно? Поскольку я не могу изменить размер элементов.
Вместо этого я использовал setImageResource, и это, похоже, усугубит проблему. Теперь и высота, и ширина становятся растянутыми! –