1

嗨有一个活动,它从可绘制文件夹中读取图像并在 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;
 }
}
4

1 回答 1

0

Try change view.setBackgroundResource(imageArray[position]); to view.setImageResource(imageArray[position]);

because Background resource not effected by ScaleType

于 2013-06-04T17:06:51.543 回答