0

网格视图图像不适合 hdpi 和 xhdpi。它适合 mdpi 模拟器和设备。我的图像尺寸如下(它遵循 3:4:6:8 的比例)。

ldpi 366*225 mdpi 488*300 //它适合模拟器和设备屏幕 hdpi 732*450 Xhdpi 976*600

第一个图像(MDPI)正确适合屏幕,第二个图像(XHDPI 与 HDPI 相同)不适合?

MDPI

XHDPI

菜单活动:

GridView gridView = (GridView) findViewById(R.id.gridview);

    gridView.setAdapter(new ImageAdapter(this));


    gridView.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_MOVE){
                return true;
            }
            return false;
        }
    });

    gridView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
    gridView.setVerticalScrollBarEnabled(false);

ImageAdapter.java:

public class ImageAdapter extends BaseAdapter {

    private Context mcontext;

    public ImageAdapter(Context c) {
        mcontext=c;
    }

    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) {

        ImageView imageView ;

        if(convertView==null)
        {
            imageView = new ImageView(mcontext);
            //imageView.inflate(mcontext, R.layout.mainmenu_griditem,null);
            ImageView.inflate(mcontext, R.layout.mainmenu_griditem,null);

            WindowManager wm = (WindowManager) mcontext.getSystemService(Context.WINDOW_SERVICE);

            Display display = wm.getDefaultDisplay();
            DisplayMetrics outMetrics = new DisplayMetrics ();
            display.getMetrics(outMetrics);

            float density  = mcontext.getResources().getDisplayMetrics().density;

            Log.i("image","density->"+density);
            float dpHeight = outMetrics.heightPixels / density;
            Log.i("image","dpHeight->"+dpHeight);
            float dpWidth  = outMetrics.widthPixels / density;
            Log.i("image","dpWidth->"+dpWidth);

            int width=(int) (dpWidth/2);
            int height=(int) (dpHeight/2.3);

            Log.i("image","Image Width->"+width);
            Log.i("image","Image height->"+height);

            //int width=(int) (dpWidth);
            //int height=(int) (dpHeight);

            //imageView.setLayoutParams(new GridView.LayoutParams(width,height));


            imageView.setLayoutParams(new GridView.LayoutParams(width,height));





            //imageView.setLayoutParams(new GridView.LayoutParams(parent.getWidth()/2,parent.getHeight()/2));


            //imageView.setLayoutParams(new GridView.LayoutParams(imageView.getWidth(),imageView.getHeight()));

            //imageView.setBackgroundColor(000000);
            //imageView.setLayoutParams(new GridView.LayoutParams(400,200));
            //imageView.setLayoutParams(new GridView.LayoutParams());
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            //imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            //imageView.setPadding(40,40,40,40);
        }
        else
        {
            imageView=(ImageView)convertView;
        }


        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }


    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.mainmenu1, R.drawable.mainmenu2,
            R.drawable.mainmenu3, R.drawable.mainmenu4
    };
}

我尝试了很多但没有帮助。

mainmenugriditem.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView android:id="@+id/mm_grid_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

       />
    <TextView android:id="@+id/mm_grid_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:labelFor="@id/mm_grid_image"/>

</RelativeLayout>

MDPI 的 Logcat 输出:(Emulator-7" WSVGA-tablet-1024*600:mdpi)

Logcat-MDPI

XHDPI 的 Logcat 输出:(Emulator-Galaxy Nexus-4.65"-720*1280:xhdpi)

Logcat-XHDPI

4

2 回答 2

0

嗨,如果不是这样创建的话,您是否为不同的屏幕尺寸创建了单独的文件夹

布局-小

大版面

布局-xlarge

因此,您将布局 XML 文件复制到这些文件夹中,并在 Palette 窗口中检查 Xhdpi 的预览,默认屏幕尺寸为 10",以便您可以在布局中调整网格视图。

于 2013-03-06T04:13:38.207 回答
0

我为 MDPI 分辨率设计了位图(因为 MDPI 中 1 px=1 dp)。然后使用http://labs.skinkers.com/content/android_dp_px_calculator/转换为其他分辨率桶

修改以下代码:

改变这个:

                Log.i("image","density->"+density);
                float dpHeight = outMetrics.heightPixels / density;
                Log.i("image","dpHeight->"+dpHeight);
                float dpWidth  = outMetrics.widthPixels / density;
                Log.i("image","dpWidth->"+dpWidth);

至 :

            float dpHeight = outMetrics.heightPixels;
            float dpWidth  = outMetrics.widthPixels;
于 2013-03-21T01:46:25.407 回答