有一個活動,從可繪製的文件夾中讀取圖像,並在幻燈片放映視圖中顯示它們。下面是我的代碼。現在我想從我的SD卡或內部存儲器中顯示圖像到viewpager。在下面的代碼中應該做些什麼改變才能讀取SD卡或內部存儲器中的圖像,並將它們以幻燈片的形式顯示在viewpager中。從SD卡或內部存儲顯示圖像查看尋呼機android
活動
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);
viewPager.setAdapter(viewPagerAdapter);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTimerTask(), 3000,3000);
}
public class MyTimerTask extends TimerTask {
@Override
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (viewPager.getCurrentItem() == 0) {
viewPager.setCurrentItem(1);
} else if (viewPager.getCurrentItem() == 1) {
viewPager.setCurrentItem(2);
} else if (viewPager.getCurrentItem() == 2) {
viewPager.setCurrentItem(3);
} else if (viewPager.getCurrentItem() == 3) {
viewPager.setCurrentItem(4);
} else {
viewPager.setCurrentItem(0);
}
}
;
});
}
}
}
ViewPagerAdapter
public class ViewPagerAdapter extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
private Integer [] images = {R.drawable.index1,R.drawable.index2,R.drawable.index3,R.drawable.index4,R.drawable.index5};
public ViewPagerAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return images.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.custom_layout, null);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
imageView.setImageResource(images[position]);
ViewPager vp = (ViewPager) container;
vp.addView(view, 0);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager vp = (ViewPager) container;
View view = (View) object;
vp.removeView(view);
}
}
檢查了這一點:https://stackoverflow.com/a/11237382/5402482 –
太複雜了,林困惑 – emin
@emin,首先需要基本上有一個函數來將圖像加載爲位圖。之後,你可以把它們放在任何你想要的地方。 –