1

我正在构建一个应用程序,里面有一个迷你 slotMachine 游戏。问题是我对插槽的每一列都使用了一个 GridView ,对于某些设备(不是全部),插槽的第一个符号上方有一个来自无处的空间......

插槽的表是 3 行 X 5 列。

空间不是来自符号宽度和高度的计算,因为我发现两个设备具有完全相同的分辨率和密度(Galaxy tab 10.1 和 Galaxy note 10.1),间距是不同的:0 代表一个,5 代表另一个。

有什么帮助吗?

我的主要活动:

public class MainActivity extends Activity{
    SlotView slot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        slot=new SlotView(this);
        slot.refreshSlotView("AAAAAAAAAAAAAAA");       

    }
}

插槽视图:

public class SlotView {
    private ArrayList<GridAdapter> adapter=new ArrayList<GridAdapter>();
    public float H_RATIO=2/3; //height ratio of the grid based on the device
    public float W_RATIO=5/6; //width ratio of the grid based on the device

    Activity activity;

    String path = "file:///android_asset/Slot/Slot-";

    public SlotView(Context c){
        activity = (Activity) c;

        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);

        final int hDisplay=dm.heightPixels;
        int wImage=(1024*hDisplay)/768; //width of the central image
        float heightGrid=(hDisplay*2)/3; //grid height
        float widthGrid=((wImage*5)/6); //grid width

        float heightSymbol=(heightGrid-(8))/3; // symbol height (3 symbols for column with a spacing of 4 px)

        //relative layout for the container table
        RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams((int)widthGrid, (int)heightGrid);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);

        // grid of the slot
        LinearLayout grid_layout=(LinearLayout)activity.findViewById(R.id.layout_grid);
        grid_layout.setLayoutParams(params);
        grid_layout.setGravity(Gravity.TOP);

        // gridview of the columns 
        LinearLayout.LayoutParams params_grid=new LinearLayout.LayoutParams((int)heightSymbol, (int)heightGrid);
        for(int i=0; i<5; i++){ // five columns
            GridView slot=new GridView(activity);
            slot.setScrollContainer(false);
            slot.setVerticalScrollBarEnabled(false);
            slot.setHorizontalScrollBarEnabled(false);
            if(i<4) {
                params_grid.setMargins(0,0,4,0); // spacing between each column
            }
            slot.setLayoutParams(params_grid);
            slot.setVerticalSpacing(4);
            slot.setPadding(0, 0, 0, 0);
            slot.setColumnWidth((int)heightSymbol);
            slot.setNumColumns(GridView.AUTO_FIT);  
            slot.setGravity(Gravity.CENTER);
            GridAdapter grid_adapter=new GridAdapter(activity, (int)heightSymbol, (int)heightSymbol);
            adapter.add(grid_adapter);
            slot.setAdapter(grid_adapter);  
            grid_layout.addView(slot);
        }
    }

    public void refreshSlotView(String configTris){
        for(int pos=0; pos<5; pos++){
            String[] mThumbIds=new String[3];
            int z=0;
            for(int i=pos; z<3 && i<configTris.length(); i=i+5){

                char letter=configTris.charAt(i);   

                mThumbIds[z]=path + letter + ".png";
                z++;
            }
            adapter.get(pos).setArrayOfImage(mThumbIds);
            adapter.get(pos).notifyDataSetChanged();
        }
    }
}

适配器:

public class GridAdapter extends BaseAdapter {
    private Context mContext;
    private String[] mThumbIds={};
    private int w;
    private int h;

    public GridAdapter(Context c, int w, int h) {
        mContext = c;
        this.w=w;
        this.h=h;
    }

    public int getCount() {
        return mThumbIds.length;
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        View view=convertView;
        ImageView imageView;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
            view = inflater.inflate(R.layout.slot_element, null);
            RelativeLayout rel=(RelativeLayout)view.findViewById(R.id.rel);
            rel.setLayoutParams(new GridView.LayoutParams(w, h));

            imageView=(ImageView)view.findViewById(R.id.image);
            RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(w, h);
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            imageView.setLayoutParams(params);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        }else{
            imageView = (ImageView) view.findViewById(R.id.image);
        }
        Picasso.with(mContext).load(mThumbIds[position]).into(imageView); // puts the image in the imageview
        return view;
    }
    public void setArrayOfImage(String[] images){
        this.mThumbIds=images;
    }

    public void showSymbolAtIndex(char letter, int position){

        mThumbIds[position]="file:///android_asset/" + "Slot/Slot-" + letter + ".png";
        notifyDataSetChanged();
    }
}

这是 MainActivity 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/layout_grid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="horizontal" >
    </LinearLayout>

</RelativeLayout>

和 slot_element 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rel"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY" />

</RelativeLayout>

现在是结果: 第一张图片

这就是现有间距的证明(如果我向上拖动其中一列): 第二张图片

4

0 回答 0