我在互联网上搜索了这个问题,我找不到任何好的东西.
我确实看过Android Developer网站,但他们
将图像从URL加载到ImageVIEw中.
这是我的代码:
Openfactsscreen活动:
public class Openfactsscreen extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_openfactsscreen); VIEwPager vIEwPager = (VIEwPager) findVIEwByID(R.ID.vIEw_pager); ImageAdapter adapter = new ImageAdapter(this); vIEwPager.setAdapter(adapter); } public voID openmenu(VIEw vIEw){ Intent intent = new Intent(this, SendFact.class); startActivity(intent); } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.openfactsscreen, menu); return true; }}
ImageAdapter.java:
public class ImageAdapter extends PagerAdapter { Context context; private int[] galImages = new int[] { R.drawable.factcard2, R.drawable.factcard8, R.drawable.factcard4 }; ImageAdapter(Context context){ this.context=context; } @OverrIDe public int getCount() { return galImages.length; } @OverrIDe public boolean isVIEwFromObject(VIEw vIEw, Object object) { return vIEw == ((ImageVIEw) object); } @OverrIDe public Object instantiateItem(VIEwGroup container, int position) { ImageVIEw imageVIEw = new ImageVIEw(context); int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium); imageVIEw.setpadding(padding, padding, padding, padding); imageVIEw.setScaleType(ImageVIEw.ScaleType.CENTER_INSIDE); imageVIEw.setimageResource(galImages[position]); ((VIEwPager) container).addVIEw(imageVIEw, 0); return imageVIEw; } @OverrIDe public voID destroyItem(VIEwGroup container, int position, Object object) { ((VIEwPager) container).removeVIEw((ImageVIEw) object); }}
activity_openfactsscreen:
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:background="@drawable/backgroundotheractivitIEs" > <androID.support.v4.vIEw.VIEwPager androID:ID="@+ID/vIEw_pager" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:layout_below="@+ID/imageVIEw1" /> <ImageVIEw androID:ID="@+ID/imageVIEw1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParenttop="true" androID:layout_centerHorizontal="true" androID:src="@drawable/factsscreen_layout" /> <Imagebutton androID:ID="@+ID/imagebutton1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentleft="true" androID:layout_alignParenttop="true" androID:layout_marginleft="23dp" androID:layout_margintop="18dp" androID:background="@drawable/emailbutton" androID:onClick="openmenu" /> <Imagebutton androID:ID="@+ID/imagebutton2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_aligntop="@+ID/imagebutton1" androID:layout_toRightOf="@+ID/imagebutton1" androID:background="@drawable/informationbutton" /></relativeLayout>
一切正常,如果有人知道一个好的网站,或者如果有人想帮助我,这将是伟大的!
无论如何,谢谢你的时间,美好的一天!
解决方法:
在你的课程结束时加上这个
public class ImageAdapter extends PagerAdapter { Context context; private int[] galImages = new int[] { R.drawable.factcard2, R.drawable.factcard8, R.drawable.factcard4 }; ImageAdapter(Context context){ this.context=context; } @OverrIDe public int getCount() { return galImages.length; } @OverrIDe public boolean isVIEwFromObject(VIEw vIEw, Object object) { return vIEw == ((ImageVIEw) object); } @OverrIDe public Object instantiateItem(VIEwGroup container, int position) { ImageVIEw imageVIEw = new ImageVIEw(context); int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium); imageVIEw.setpadding(padding, padding, padding, padding); imageVIEw.setScaleType(ImageVIEw.ScaleType.CENTER_INSIDE); //imageVIEw.setimageResource(galImages[position]); new LoadImage(imageVIEw).execute("Your URL"); ((VIEwPager) container).addVIEw(imageVIEw, 0); return imageVIEw; } @OverrIDe public voID destroyItem(VIEwGroup container, int position, Object object) { ((VIEwPager) container).removeVIEw((ImageVIEw) object); } private class LoadImage extends AsyncTask<String, String, Bitmap> { ImageVIEw img=null; public LoadImage(ImageVIEw img){ this.img=img; } @OverrIDe protected voID onPreExecute() { super.onPreExecute(); } protected Bitmap doInBackground(String... args) { Bitmap bitmap=null; try { bitmap = BitmapFactory.decodeStream((inputStream)new URL(args[0]).getContent()); } catch (Exception e) { e.printstacktrace(); } return bitmap; } protected voID onPostExecute(Bitmap image) { if(image != null){ img.setimageBitmap(image); } } }` }
总结 以上是内存溢出为你收集整理的android – 如何将URL中的图像加载到viewpager中全部内容,希望文章能够帮你解决android – 如何将URL中的图像加载到viewpager中所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)