how to show next image on button click if i have image urls in an array
click .. i think que is clear pls help me i am new in android and java pls pls..
**private String[] imageList = {"http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/martin_di_girolamo._diosas/198915-1-esl-AR/MARTIN_DI_GIROLAMO._Diosas.jpg","http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/jorge_macchi._la_espera/198929-1-esl-AR/JORGE_MACCHI._La_espera.jpg"};**
public class SequencerActivity extends Activity implements OnClickListener
{
private int imageCounter = 0;
private ImageView imageLoader;
**private String[] imageList = {"http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/martin_di_girolamo._diosas/198915-1-esl-AR/MARTIN_DI_GIROLAMO._Diosas.jpg","http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/jorge_macchi._la_espera/198929-1-esl-AR/JORGE_MACCHI._La_espera.jpg"};**
public void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.parent_frame);//this one is the common parent layout for all image views
super.onCreate(savedInstanceState);
/*requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);*/
//int image1 = R.drawable.image_w_lbl_0;
开发者_StackOverflow//imageLoader.setImageResource(image1);
ImageButton next = (ImageButton) findViewById(R.id.next);
ImageButton back = (ImageButton) findViewById(R.id.back);
next.setOnClickListener(this);
back.setOnClickListener(this);
back.setEnabled(false);
//show the default image
this.loadImage(imageList[imageCounter]);
}
@Override
public void onClick(View v)
{
}
private void loadImage(int imagePath)
{
imageLoader.setImageResource(imagePath);
}
}
If you have String
values in your imageList
array representing resource names,
private String[] imageList = { "image_wo_lbl_0", "image_wo_lbl_1", "image_wo_lbl_2" };
then you can modify the loadImage
method:
private void loadImage(final String imagePath)
{
imageLoader.setImageResource(getResources().
getIdentifier(imagePath, "drawable", "your.application.package"));
}
If you have urls stored in the imageList
array
private String[] imageList = { "file:///somedir/IMAG0001.jpg",
"file:///otherdir/IMAG0002.jpg",
"file:///somedir/IMAG0003.jpg" };
you can use
private void loadImage(final String imagePath)
{
imageLoader.setImageURI(Uri.parse(imagePath));
}
When loading images from the web (storing their urls in the imageList
):
private String[] imageList = { "http://somedomain/IMAG0001.jpg",
"http://otherdomain/IMAG0002.jpg",
"http://somedomain/IMAG0003.jpg" };
[...]
private void loadImage(String imagePath)
{
try
{
final URL url = new URL(imagePath);
final InputStream inputStream = (InputStream)url.getContent();
Drawable drawable = Drawable.createFromStream(inputStream, "src");
imageLoader.setImageDrawable(drawable);
}
catch (Exception e)
{
Log.e("Error", "loadImage", e);
}
}
For this to work, don't forget to add the android.permission.INTERNET
permission to your application in the `androidManifest.xml!
@rekaszeru i had used
private void loadImage(String imagePath)
{
try {
/* Open a new URL and get the InputStream to load data from it. */
URL aURL = new URL(imagePath);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
/* Buffered is always good for a performance plus. */
BufferedInputStream bis = new BufferedInputStream(is);
/* Decode url-data to a bitmap. */
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
/* Apply the Bitmap to the ImageView that will be returned. */
imageLoader.setImageBitmap(bm);
//bigView.setImageBitmap(bm);
// bigView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageLoader.setImageBitmap(bm);
} catch (IOException e) {
// i.setImageResource(R.drawable.error);
Log.e("DEBUGTAG", "Remote Image Exception", e);
}
You should take only one Imageview and set image using setImageResource properties and increase the countervalue when click on next button . You can also use imageswitcher control for solving this problem.
You can refer to this SO question. Every time your press you have to call that method and change the URL by passing the next element in the array.
精彩评论