Gallery问题,求指教

MonsterStar 2013-09-25 02:50:42
用gallery和switcher写了个浏览图片的程序,能够正常运行,但是在滑到后面的图片后会出错终止
这是我的源码,大神们帮忙看看
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>


<ImageSwitcher android:id="@+id/switcher"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_alignParentTop="true" android:layout_centerHorizontal="true"
/>
<Gallery android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="60px"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:background="#55000000"
android:gravity="center_vertical"
android:spacing="16dp"
/>
</RelativeLayout>
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class GalleryActivity extends Activity implements OnItemSelectedListener,
ViewFactory{
/** Called when the activity is first created. */
//private Gallery gallery;
private ImageSwitcher mSwitcher;

private int[] mimage_s = new int[]
{ R.drawable.image_s_1, R.drawable.image_s_2, R.drawable.image_s_3, R.drawable.image_s_4,
R.drawable.image_s_5, R.drawable.image_s_6, R.drawable.image_s_7};
private int[] mimages = new int[]
{ R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4,
R.drawable.image5, R.drawable.image6, R.drawable.image7};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
mSwitcher = (ImageSwitcher)findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
Gallery g = (Gallery)findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
}

@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
return i;
}

@Override
public void onItemSelected(AdapterView<?> adapter, View v, int position,
long id) {
// TODO Auto-generated method stub
mSwitcher.setImageResource(mimages[position]);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
public class ImageAdapter extends BaseAdapter{
public ImageAdapter(Context c)
{
mContext = c;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return mimage_s.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(mContext);
i.setImageResource(mimage_s[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(R.drawable.picture_frame);
return i;
}
private Context mContext;
}

}
...全文
385 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
MonsterStar 2013-09-26
  • 打赏
  • 举报
回复
引用 2 楼 youngc527 的回复:
估计是内存溢出了
那要怎么弄啊
MonsterStar 2013-09-26
  • 打赏
  • 举报
回复

这是运行出来的界面 下面的gallery就不太正常 感觉应该是靠左的
前几张图片没问题 点到后面几张就出错了
MonsterStar 2013-09-26
  • 打赏
  • 举报
回复
引用 10 楼 lieri111 的回复:
图片多,内存溢出,百度“android图片缓存”
的确是内存溢出 说是用Option 但是用了报错 具体代码应该怎么修改呢
passself 2013-09-26
  • 打赏
  • 举报
回复
图片多,内存溢出,百度“android图片缓存”
tantahe 2013-09-26
  • 打赏
  • 举报
回复
eclipse有个LogCat的标签,在DDMS的devices里点一下你的设备或者模拟器,LogCat这里就能看到log
MonsterStar 2013-09-26
  • 打赏
  • 举报
回复
引用 7 楼 tantahe 的回复:
你要把LogCat里面的log贴出来才好分析。
应该怎么看??我是个新手
tantahe 2013-09-26
  • 打赏
  • 举报
回复
你要把LogCat里面的log贴出来才好分析。
EM265 2013-09-26
  • 打赏
  • 举报
回复
兄弟:Gallery都已经过时了,还用? 建议你用ViewPager+FragmentStatePagerAdapter吧!
MonsterStar 2013-09-26
  • 打赏
  • 举报
回复
还没解决,顶啊顶
Darcy杨 2013-09-25
  • 打赏
  • 举报
回复
估计是内存溢出了
tantahe 2013-09-25
  • 打赏
  • 举报
回复
我在模拟器上试了,不会报错啊。你把错误的log贴上来看看。

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧