gpt4 book ai didi

android - 如何从 URL 加载图像到 viewpager

转载 作者:太空宇宙 更新时间:2023-11-03 12:32:55 25 4
gpt4 key购买 nike

我已经在互联网上搜索了这个问题,但找不到任何好的答案。我确实看过 Android Developer现场可是他们将图像从 URL 加载到 ImageView 中。

这是我的代码:

Openfactsscreen Activity :

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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28295694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com