发现用这个控件滑动图片好卡啊
有什么解决办法吗,真机调试也是卡
有什么解决办法吗,真机调试也是卡
2016-08-18
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView==null){
viewHolder=new ViewHolder();
convertView=myInflater.inflate(R.layout.activity_main, null);
viewHolder.image.setBackgroundResource(src[position%src.length]);
viewHolder.image.setLayoutParams(new Gallery.LayoutParams(200, 200));//缩略图的宽和高
viewHolder.image.setScaleType(ScaleType.FIT_XY);//缩放模式,横向缩放
convertView.setTag(viewHolder);
}else {
viewHolder=(ViewHolder) convertView.getTag();
}
return convertView;
}
class ViewHolder{
public ImageView image;
}
public class ImageAdapter extends BaseAdapter{ private int[]src; private Context context; public ImageAdapter(int[]src,Context context) { this.context=context; this.src=src; } //返回数据源的数量 // 无限循环显示图片 public int getCount() { return Integer.MAX_VALUE; } //告诉适配器取得目前容器中数据Id和对象 @Override public Object getItem(int arg0) { // TODO 自动生成的方法存根 return src[arg0]; } @Override public long getItemId(int position) { // TODO 自动生成的方法存根 return position; } //取得目前欲显示的图像View,传入数组ID值对图像进行返回 @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView image=new ImageView(context);//由于这里没有Activity所以在构造函数里返回上下文 //image.setBackgroundResource(src[position]);加载对象资源 //无限循环时加载的图片的指数position肯定会超过设定的数量,所以做求余处理 image.setBackgroundResource(src[position%src.length]); image.setLayoutParams(new Gallery.LayoutParams(200, 200));//缩略图的宽和高 image.setScaleType(ScaleType.FIT_XY);//缩放模式,横向缩放 return image; } }
public class MainActivity extends Activity implements OnItemSelectedListener,ViewFactory{ private Gallery gallery; private int[]src={R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4, R.drawable.pic5,R.drawable.pic6,R.drawable.pic7,R.drawable.pic8,R.drawable.pic9 }; private ImageSwitcher imageSwitch; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gallery=(Gallery) findViewById(R.id.gallery); imageSwitch=(ImageSwitcher) findViewById(R.id.imageSwitcher); ImageAdapter adapter=new ImageAdapter(src, this);//为适配器使用命名 gallery.setAdapter(adapter);//加载适配器 gallery.setOnItemSelectedListener(this); imageSwitch.setFactory(this); //添加安卓自带的动画效果,淡入淡出 imageSwitch.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); imageSwitch.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out)); } @Override public View makeView() { ImageView image=new ImageView(this); image.setScaleType(ScaleType.FIT_CENTER);//显示图片为等比例显示缩放,并居中 return image; } @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { imageSwitch.setBackgroundResource(src[arg2%src.length]);//加载当前Gallery中显示的图片 } @Override public void onNothingSelected(AdapterView<?> arg0) { } }
举报