从assets文件夹中检索多个图像

时间:2013-10-30 03:10:37

标签: android button imageview android-viewpager android-assets

我无法从资源文件夹加载图片。我想要做的是在用户点击图像将切换的按钮后。我的资产文件夹中有6个图像,我想知道如何在每次点击后加载新图像。

基本上我想要一个后退和下一个按钮,它将滚动图像。我想用android-view寻呼机实现它。这有可能实现吗?在网上有什么教程可以告诉我这个吗?现在我打电话

private int [] images = {R.drawable.airplane, R.drawable.bike,...

但我现在想将所有内容存储在资产文件夹中

public class MainActivity extends Activity {

private int [] images = {R.drawable.airplane, R.drawable.bike, R.drawable.boat,
        R.drawable.bus2,R.drawable.car,R.drawable.train};

private ViewPager mViewPager;
private MyPagerAdapter myPagerAdapter;
private Button nextBtn, preBtn;
private TextView positionText;
private int imageCount =images.length;

//HashMap<Integer, String> map = new HashMap<Integer, String>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /* Initialize every object */

    //String whatString = map.get(R.drawable.airplane);

    myPagerAdapter = new MyPagerAdapter();

    nextBtn = (Button)findViewById(R.id.nextBtn);
    preBtn = (Button)findViewById(R.id.prevBtn);

    positionText= (TextView)findViewById(R.id.position);

    mViewPager =(ViewPager)findViewById(R.id.viewPager);
    mViewPager.setAdapter(myPagerAdapter);


    /* ViewPager's setCurrentItem(int) method is used to set first image */

    mViewPager.setCurrentItem(0);
    positionText.setText("1 of "+ imageCount);


    /* OnPageChangeListener */
    mViewPager.setOnPageChangeListener(new OnPageChangeListener()
    {

        @Override
        public void onPageSelected(int position) {

            positionText.setText((position+1) +" of "+imageCount);

            if (position==imageCount-1) {
                nextBtn.setEnabled(false);
            }
            else {
                nextBtn.setEnabled(true);
            }
            if (position==0){
                preBtn.setEnabled(false);
            }
            else {
                preBtn.setEnabled(true);
            }
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, 
                int positionOffsetPixels) {

        }

        @Override
        public void onPageScrollStateChanged (int state) {

        }
    });

    /* OnClickListener on buttons to change the image in viewpager */

    nextBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            mViewPager.setCurrentItem(mViewPager.getCurrentItem()+1);
        }


    });

    preBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            mViewPager.setCurrentItem(mViewPager.getCurrentItem()-1);
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/* Pager Adapter for ViewPager */

private class MyPagerAdapter extends PagerAdapter {

    public MyPagerAdapter() {}


    @Override
    public int getCount() {
        return imageCount;

    }

    @Override
    public View instantiateItem(ViewGroup container, int position){

    ImageView mImageView = new ImageView(MainActivity.this);
    mImageView.setImageResource(images[position]);

    // add ImageView to ViewPager and return it

    container.addView(mImageView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

    return mImageView;

}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {

    container.removeView((View) object);
}


@Override 
public boolean isViewFromObject(View view, Object object){
    return view == object;
}

}

}

0 个答案:

没有答案