Reputation: 305
package com.example.imagechange;
public class MainActivity extends Activity
{
ImageView imageView;
int []imageArray={R.drawable.a0,R.drawable.a1,R.drawable.a2,R.drawable.a3};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageView1);
final Handler handler = new Handler();
Runnable runnable = new Runnable()
{
int i=0;
public void run()
{
imageView.setImageResource(imageArray[i]);
i++;
if(i>imageArray.length-1)
{
i=0;
}
handler.postDelayed(this, 3000); //for interval...
}
};
handler.postDelayed(runnable, 1000); //for initial delay..
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MY ACTIVITY_MAIN :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="@string/app_name"
android:layout_marginTop="56dp"
android:src="@drawable/a0" />
</RelativeLayout>
So in the above code ,I have copied the images in the drawable and have changed it periodically but I need to get the images from the url
and display it in the ImageView
.
Upvotes: 4
Views: 2251
Reputation: 53
ImageView MyImageView;
int []imageArray={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
MyImageView=(ImageView)findViewById(R.id.imageView1);
/*here the countdown start for images*/
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
int i=0;
public void run() {
MyImageView.setImageResource(imageArray[i]);
i++;
if(i>imageArray.length-1) {
i=0;
}
handler.postDelayed(this, 500); //for interval...
}
};
handler.postDelayed(runnable, 2000); //for initial delay..
/*here the button click counter start */
}
Make first drawable folder in re>layouts>drawable
, and place images a.png, b.png, etc. in it.
Upvotes: 2
Reputation: 7087
Try following code: First you need to get the image path from URL then pass that URL to this function.
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Nothing too complicated – simply create a URL object using the “src” string (i.e. String src = “http://thinkandroid.wordpress.com”), connect to it, and use Android’s BitmapFactory class to decode the input stream. The result should be your desired Bitmap object! And after that:
imageView.setImageBitmap(bitmap);
Upvotes: 0
Reputation: 14633
Your code sets the image from a resource in the line
imageView.setImageResource(imageArray[i]);
What you want to do instead is use UrlImageViewHelper to load the image from a URL.
Upvotes: 0