我正在尝试在运行时从assets文件夹加载图像后在网格视图中显示图像。目录中的所有图像具有不同的尺寸和大小。在Android 2.3的较低版本中,图像无法正确呈现。在运行较旧Android版本的设备中,图像重叠,大图像被裁剪。我尝试过给出不同的布局参数,但它仍然不起作用。在较新版本的android中,如kitkat(4.4)和棒棒糖(5.0),图像显示完美。有什么问题?这是我的代码。
newselectframe.java public class newselectframe扩展了Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newselectframe);
View header = findViewById(R.id.textView);
header.setVisibility(View.VISIBLE);
final GridView gridView = ((GridView)(findViewById(R.id.layout1)));
Context context = getApplicationContext();
gridView.setAdapter(new ImageAdapter(this));
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context context;
private String[] list;
public ImageAdapter(Context c) {
context = c;
try {
list = context.getAssets().list("frames");
} catch (IOException e) {
e.printStackTrace();
}
}
public int getCount() {
return list.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 img;
if (convertView == null) {
img = new ImageView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
img.setLayoutParams(new GridView.LayoutParams(params));
img.setScaleType(ImageView.ScaleType.CENTER);
img.setPadding(8, 8, 8, 8);
} else {
img = (ImageView) convertView;
}
try {
InputStream ims = context.getAssets().open("frames/" + list[position]);
//Drawable d = Drawable.createFromStream(ims, null);
Bitmap bitmap = BitmapFactory.decodeStream(ims);
img.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
}