【发布时间】:2015-02-04 10:13:32
【问题描述】:
我正在用 android 开发一个记忆游戏。
现在我需要创建一个大写网格,并且我正在使用自定义 Imageview 作为网格中的项目。
这是我扩展 Imageview 的 Cap 对象类。
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>
结果是一个高度不成比例的网格。这意味着图像视图的宽度呈指数级增长。
你能告诉我我做错了什么吗?因为我似乎无法调整项目的大小。
【问题讨论】:
标签: android gridview android-custom-view