在GridView Android中使用自定义ImageView项

时间:2015-02-04 10:13:32

标签: android gridview android-custom-view

我正在开发一款Android内存游戏。

现在我需要创建一个上限网格,我使用自定义Imageview作为网格中的项目。

这是扩展Imageview的Cap Object Class。

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>

结果是一个高度不成比例的网格。这意味着图像视图的宽度呈指数式拉伸。

enter image description here

你能告诉我我做错了什么吗?因为我似乎无法调整项目的大小。

2 个答案:

答案 0 :(得分:0)

您到处都使用过setBackgroundResource。请改用setImageResource

答案 1 :(得分:0)

我不确定你的问题。您应该在描述中发布图片。

我会尝试覆盖onMeasure method。如上所述,您可以调整ImageView的大小。 首先使用getMeasuredWidthgetMeasuredHeight来获取ImageView的维度。然后使用getDrawable获取图像并获取图像的尺寸。 现在,您拥有正确的图像比例,将它们应用到ImageView。 要应用新尺寸,请使用method setMeasuredDimension

您应该首先确定应调整哪个维度。但我无法帮助您制作应用图片。

编辑1: 在你的DhakkanObject中,添加这个函数:

@Override
public void onMeasure(int prefWidth, int prefHeight){
   Drawable d = getDrawable();
   if(d!=null){
      int width = getMeasuredWidth(prefWidth);
      int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());

      int prefWidthView = resolveSizeAndState(width, prefWidth, 0);
      int prefHeightView = resolveSizeAndState(height, prefHeight, 0);
      setMeasuredDimension(prefWidthView, prefHeightView);
   }
   else{
      super.onMeasured(prefWidth,prefHeight);
   }
}

每次调整图像视图大小时,imageView会检查基础图像尺寸并扩展其高度以保持正确的比例,假设第一次宽度良好。