0

我有一个带有 ImageViews (64) 的二维数组的游戏。我相信这不是一个有效的代码,也给我带来了滚动问题(我不能只水平或垂直自由移动)。我想要类似 Minesweeper Clasic 的东西:

https://www.youtube.com/watch?v=6n5FOdgDmI0&feature=player_embedded

我应该使用什么样的小部件来放置单元格?也许是 GridView?我正在尝试使用 gridview 和 tablelayout,但我仍然无法自由滚动。

谢谢。

4

1 回答 1

0

我用一个 onTouchListener 和一个

    gridview.setOnTouchListener(new OnTouchListener() { 
        public boolean onTouch(View v, MotionEvent event) {
            int desplazamiento[]={0,0};
            campo.getLocationOnScreen(desplazamiento);
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mx = event.getX();
                    my = event.getY();
                    return false;
                case MotionEvent.ACTION_MOVE:
                    curX = event.getX();
                    curY = event.getY();
                    campo.scrollBy((int) (mx - curX), (int) (my - curY));                 
                    mx = curX;
                    my = curY;
                    return false;
                case MotionEvent.ACTION_UP:
                    curX = event.getX();
                    curY = event.getY();
                    campo.scrollBy((int) (mx - curX), (int) (my - curY));
                    return false;
            }
            return false;
        }
    });

    zoom.setOnZoomInClickListener(new OnClickListener() {       
        public void onClick(View v) {
            adaptador.incZoom(fondo, gridview);
            gridview.setAdapter(adaptador);     
        }
    });

    zoom.setOnZoomOutClickListener(new OnClickListener() {          
        public void onClick(View v) {
            adaptador.decZoom(fondo, gridview);
            gridview.setAdapter(adaptador); 
        }
    });
}

并扩展 BaseAdapter 我放了两种缩放方法:

公共类 ImageAdapter 扩展 BaseAdapter {

public ImageAdapter(Context context) {
    mContext = context;
    metrics = mContext.getResources().getDisplayMetrics();
    float dp = 80f;
    float fpixels = metrics.density * dp;
    CUADRO = (int) (metrics.density * dp + 0.5f);
}

public int getCount() {
    return 64;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
@SuppressLint("NewApi")
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {  // if it's not recycled, initialize some attributes         
        imagen = new ImageView(mContext);
        imagen.setLayoutParams(new GridView.LayoutParams(CUADRO, CUADRO));
        imagen.setScaleType(ImageView.ScaleType.FIT_XY);
        imagen.setPadding(0, 0, 0, 0);
    } else {
        imagen = (ImageView) convertView;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        imagen.setBackground(drawables[position]);

    } else {
        imagen.setBackgroundDrawable(drawables[position]);         
    }
    imagen.setImageResource(agua[position]);
    return imagen;
} 

public void incZoom(ImageView fondo, GridView terreno) {
    int incremento = CUADRO*10/100;
    CUADRO = CUADRO+incremento; 
    terreno.getLayoutParams().height=8*CUADRO;
    terreno.getLayoutParams().width=8*CUADRO;
    fondo.getLayoutParams().height=fondo.getHeight()*110/100;
    fondo.getLayoutParams().width=fondo.getWidth()*110/100; 
}

public void decZoom(ImageView fondo, GridView terreno) {
    int incremento = - CUADRO*10/100;
    CUADRO = CUADRO+incremento;
    terreno.getLayoutParams().height=8*CUADRO;
    terreno.getLayoutParams().width=8*CUADRO;
    fondo.getLayoutParams().height=fondo.getHeight()*90/100;
    fondo.getLayoutParams().width=fondo.getWidth()*90/100;
}

}

于 2012-09-22T17:51:31.157 回答