网格视图图像不适合 hdpi 和 xhdpi。它适合 mdpi 模拟器和设备。我的图像尺寸如下(它遵循 3:4:6:8 的比例)。
ldpi 366*225 mdpi 488*300 //它适合模拟器和设备屏幕 hdpi 732*450 Xhdpi 976*600
第一个图像(MDPI)正确适合屏幕,第二个图像(XHDPI 与 HDPI 相同)不适合?
菜单活动:
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)
XHDPI 的 Logcat 输出:(Emulator-Galaxy Nexus-4.65"-720*1280:xhdpi)