【问题标题】:Using Custom ImageView items in GridView Android在 GridView Android 中使用自定义 ImageView 项目
【发布时间】: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


    【解决方案1】:

    您在任何地方都使用过setBackgroundResource。请改用setImageResource

    【讨论】:

    • 我改用了 setImageResource ,这似乎使问题更加严重。现在高度和宽度都被拉伸了!
    【解决方案2】:

    我不确定是否理解您的问题。您可能应该在描述中张贴一张图片。

    我会尝试覆盖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 的大小时,imageView 都会检查底层图像尺寸并扩展其高度以保持正确的比例,假设第一次宽度很好。

    【讨论】:

    • 我在帖子中添加了截图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多