我正在制作一个应用程序,我想要显示高分辨率图片的画廊For.eg(1920 X 1200)。当我使用此高分辨率图片设置items.add(new Item("Red", R.drawable.wall1));
时,它会崩溃。如何实现这一目标。
请高手帮帮我。提前谢谢。
public class ImageGalleryActivity extends Activity {
String strCategoryName = null;
GridView gridView;
Utils utils;
private int columnWidth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_gallery);
Intent intent;
intent = getIntent();
if (intent != null) {
strCategoryName = intent.getStringExtra("category");
}
gridView = (GridView) findViewById(R.id.gridView_cards);
gridView.setAdapter(new MyAdapter(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.image_gallery, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class MyAdapter extends BaseAdapter {
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public MyAdapter(Context context) {
inflater = LayoutInflater.from(context);
items.add(new Item("Red", R.drawable.wall1));
items.add(new Item("Magenta", R.drawable.img10));
items.add(new Item("Dark Gray", R.drawable.img11));
items.add(new Item("Gray", R.drawable.img12));
items.add(new Item("Green", R.drawable.img13));
items.add(new Item("Cyan", R.drawable.img14));
items.add(new Item("Dark Gray", R.drawable.img11));
items.add(new Item("Gray", R.drawable.img12));
items.add(new Item("Dark Gray", R.drawable.img11));
items.add(new Item("Gray", R.drawable.img12));
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i) {
return items.get(i);
}
@Override
public long getItemId(int i) {
return items.get(i).drawableId;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;
if(v == null) {
v = inflater.inflate(R.layout.frame_layout_square_image_view, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView)v.getTag(R.id.picture);
name = (TextView)v.getTag(R.id.text);
Item item = (Item)getItem(i);
picture.setImageResource(item.drawableId);
name.setText(item.name);
return v;
}
private class Item {
final String name;
final int drawableId;
Item(String name, int drawableId) {
this.name = name;
this.drawableId = drawableId;
}
}
}
Log cat Error。
05-12 08:19:59.004: E/AndroidRuntime(16159): FATAL EXCEPTION: main
05-12 08:19:59.004: E/AndroidRuntime(16159): java.lang.OutOfMemoryError
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.content.res.Resources.loadDrawable(Resources.java:1965)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.content.res.Resources.getDrawable(Resources.java:660)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.ImageView.resolveUri(ImageView.java:616)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.ImageView.setImageResource(ImageView.java:349)
05-12 08:19:59.004: E/AndroidRuntime(16159): at com.example.paperfreepost.MyAdapter.getView(ImageGalleryActivity.java:155)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.GridView.makeAndAddView(GridView.java:1341)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.GridView.makeRow(GridView.java:341)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.GridView.fillDown(GridView.java:283)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.GridView.fillFromTop(GridView.java:417)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.GridView.layoutChildren(GridView.java:1229)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.View.layout(View.java:14008)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.View.layout(View.java:14008)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.View.layout(View.java:14008)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.View.layout(View.java:14008)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.View.layout(View.java:14008)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.Choreographer.doFrame(Choreographer.java:532)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.os.Handler.handleCallback(Handler.java:725)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.os.Handler.dispatchMessage(Handler.java:92)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.os.Looper.loop(Looper.java:137)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-12 08:19:59.004: E/AndroidRuntime(16159): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 08:19:59.004: E/AndroidRuntime(16159): at java.lang.reflect.Method.invoke(Method.java:511)
05-12 08:19:59.004: E/AndroidRuntime(16159): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-12 08:19:59.004: E/AndroidRuntime(16159): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-12 08:19:59.004: E/AndroidRuntime(16159): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您需要调整图像大小。更好的方法是将图像解码为位图,并将位图设置为ImageView。例如:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), item.drawableId, opts);
picture.setImageBitmap (bitmap);
答案 1 :(得分:1)
添加此方法
public Bitmap decodeImage(int resourceId) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), resourceId, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 100; // you are free to modify size as your requirement
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeResource(getResources(), resourceId, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
从适配器
调用它picture.setImageBitmap((decodeImage(item.drawableId));
而不是这一行
picture.setImageResource(item.drawableId);
答案 2 :(得分:0)
您正在展示图库?网格中的多个图像?
您可能希望在网格中显示缩略图,并且触控显示高分辨率图像。无法同时渲染多个高分辨率图像。
答案 3 :(得分:0)
// try this way,hope this will help you...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_gallery);
gridView = (GridView) findViewById(R.id.gridView_cards);
Intent intent;
intent = getIntent();
if (intent != null) {
strCategoryName = intent.getStringExtra("category");
}
List<Item> items = new List<Item>();
items.add(new Item("Red", R.drawable.wall1));
items.add(new Item("Magenta", R.drawable.img10));
items.add(new Item("Dark Gray", R.drawable.img11));
items.add(new Item("Gray", R.drawable.img12));
items.add(new Item("Green", R.drawable.img13));
items.add(new Item("Cyan", R.drawable.img14));
items.add(new Item("Dark Gray", R.drawable.img11));
items.add(new Item("Gray", R.drawable.img12));
items.add(new Item("Dark Gray", R.drawable.img11));
items.add(new Item("Gray", R.drawable.img12));
gridView.setAdapter(new MyAdapter(this,items));
}
class MyAdapter extends BaseAdapter {
private List<Item> items;
private Context context;
public MyAdapter(Context context, List<Item> items) {
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i) {
return items.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.frame_layout_square_image_view, null, false);
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.picture = (ImageView) convertView.findViewById(R.id.picture);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.picture.setImageResource(items.get(position).drawableId);
holder.text.setText(items.get(position).name);
return convertView;
}
class ViewHolder {
TextView text;
ImageView picture;
}
}