你有一个活动,它从drawable文件夹中读取图像并在viewpager中显示它们。下面是我的代码。现在我想将SD卡中的图像显示到viewpager中。应在下面的代码中进行哪些更改,以便从SD卡读取图像并在viewpager中显示它们。
public class TestClass extends Activity {
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.testclass);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra);
ViewPager myPager = (ViewPager) findViewById(R.id.viewpager_testclass);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
private int imageArra[] = {
R.drawable.a01,
R.drawable.a02,
R.drawable.a03
};
这里是PagerAdapter
public class ViewPagerAdapter extends PagerAdapter {
Activity activity;
int imageArray[];
public ViewPagerAdapter(Activity act, int[] imgArra) {
imageArray = imgArra;
activity = act;
}
public int getCount() {
return imageArray.length;
}
public Object instantiateItem(View collection, int position) {
ImageView view = new ImageView(activity);
view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
view.setScaleType(ScaleType.FIT_XY);
view.setBackgroundResource(imageArray[position]);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
答案 0 :(得分:0)
尝试更改view.setBackgroundResource(imageArray [position]); 到 view.setImageResource(imageArray [position]);
因为Background资源不受ScaleType
的影响