网格视图图像不适合hdpi和xhdpi。它适用于mdpi仿真器和设备。我的图像大小低于(它遵循3:4:6:8的比例)。
ldpi 366 * 225 mdpi 488 * 300 //它适合模拟器和设备屏幕 hdpi 732 * 450 Xhdpi 976 * 600
第一张图像(MDPI)是否适合屏幕,第二张图像(与HDPI相同的XHDPI)不合适?
MenuActivity:
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)
答案 0 :(得分:0)
您是否为不同的屏幕尺寸创建了分隔文件夹,如果不是像这样创建它
<强>布局小强>
<强>布局-大强>
<强>布局-XLARGE 强>
然后将布局XML文件复制到这些文件夹中,并在Palette窗口中检查Xhdpi的预览,默认屏幕大小为10“,以便您可以在布局中调整网格视图。
答案 1 :(得分: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);
TO:
float dpHeight = outMetrics.heightPixels;
float dpWidth = outMetrics.widthPixels;