我在网上搜到一个滚动显示图片的代码(JAVA),但是只能读取本地图片滚动播放:
package com.LabGuider;import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;public class ImageTranslatActivity extends Activity {
/** Called when the activity is first created. */
public ImageView imageView;
public ImageView imageView2;
public Animation animation1;
public Animation animation2;
public TextView text;
public boolean juage = true;
public int images[] = new int[] { R.drawable.a, R.drawable.b,R.drawable.c,R.drawable.d, R.drawable.e
//R.drawable.changer, R.drawable.dataline, R.drawable.preffitication
};public int count = 0;
public Handler handler = new Handler();
public Runnable runnable = new Runnable() {
// @Override
public void run() {
// TODO Auto-generated method stub
AnimationSet animationSet1 = new AnimationSet(true);
AnimationSet animationSet2 = new AnimationSet(true);
imageView2.setVisibility(0);
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
-1f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f);
ta.setDuration(2000);
animationSet1.addAnimation(ta);
animationSet1.setFillAfter(true);
ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
0f, Animation.RELATIVE_TO_SELF, 0f);
ta.setDuration(2000);
animationSet2.addAnimation(ta);
animationSet2.setFillAfter(true);
//iamgeView 出去 imageView2 进来
imageView.startAnimation(animationSet1);
imageView2.startAnimation(animationSet2);
imageView.setBackgroundResource(images[count % 5]);
count++;
imageView2.setBackgroundResource(images[count % 5]);
text.setText(String.valueOf(count));
if (juage)
handler.postDelayed(runnable, 6000);
Log.i("handler", "handler");
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); imageView = (ImageView)this.findViewById(R.id.imageView);
imageView2 = (ImageView) findViewById(R.id.imageView2);
text=(TextView)findViewById(R.id.text);
text.setText(String.valueOf(count));
//将iamgeView先隐藏,然后显示
imageView2.setVisibility(4);
handler.postDelayed(runnable, 2000);
}
public void onPause() {
juage = false;
super.onPause();
}}为了能够动态从网络或服务器加载一批图片并滚动播放,我在代码中加入了一点改动,为的就是能从服务器读取一批图片:JAVA代码:
package com.LabGuider;import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
//import android.view.Window;
//import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;public class ImageTranslatActivity extends Activity {
/** Called when the activity is first created. */
public ImageView imageView;
public ImageView imageView2;
public Animation animation1;
public Animation animation2;
public TextView text;
public boolean juage = true;
String url_1 = "http://114.212.82.54/a.jpg"; //假设从服务器读取一批已知文件名的图片
String url_2 = "http://114.212.82.54/b.jpg";
String url_3 = "http://114.212.82.54/c.jpg";
String url_4 = "http://114.212.82.54/d.jpg";
String url_5 = "http://114.212.82.54/e.jpg";
//得到可用的图片
Bitmap bitmap_1 = getHttpBitmap(url_1);
Bitmap bitmap_2 = getHttpBitmap(url_2);
Bitmap bitmap_3 = getHttpBitmap(url_3);
Bitmap bitmap_4 = getHttpBitmap(url_4);
Bitmap bitmap_5 = getHttpBitmap(url_5);
public Bitmap images[] = new Bitmap[] {bitmap_1,bitmap_2,bitmap_3,bitmap_4,bitmap_5
// R.drawable.changer, R.drawable.dataline, R.drawable.preffitication
};public int count = 0;
public Handler handler = new Handler();
public Runnable runnable = new Runnable() {
// @Override
public void run() {
AnimationSet animationSet1 = new AnimationSet(true);
AnimationSet animationSet2 = new AnimationSet(true);
imageView2.setVisibility(0);
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
-1f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f);
ta.setDuration(2000);
animationSet1.addAnimation(ta);
animationSet1.setFillAfter(true);
ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
0f, Animation.RELATIVE_TO_SELF, 0f);
ta.setDuration(2000);
animationSet2.addAnimation(ta);
animationSet2.setFillAfter(true);
//iamgeView 出去 imageView2 进来
imageView.startAnimation(animationSet1);
imageView2.startAnimation(animationSet2);
imageView.setImageBitmap(images[count % 5]);
count++;
imageView2.setImageBitmap(images[count % 5]);
text.setText(String.valueOf(count));
if (juage)
handler.postDelayed(runnable, 6000);
Log.i("handler", "handler");
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//得到可用的图片
// String url = "http://114.212.82.54/a.jpg";
//Bitmap bitmap = getHttpBitmap(url);
//imageView.setImageBitmap(bitmap);
// imageView.setImageBitmap(bitmap_1);
imageView = (ImageView)this.findViewById(R.id.imageView);
//得到可用的图片
// imageView2.setImageBitmap(bitmap_2);
imageView2 = (ImageView)this.findViewById(R.id.imageView2);
text=(TextView)findViewById(R.id.text);
text.setText(String.valueOf(count));
//将iamgeView先隐藏,然后显示
imageView2.setVisibility(4);
handler.postDelayed(runnable, 2000);
}
public void onPause() {
juage = false;
super.onPause();
}public static Bitmap getHttpBitmap(String url){ URL myFileURL;
Bitmap bitmap=null;
try{
myFileURL = new URL(url);
//获得连接
HttpURLConnection conn=(HttpURLConnection)myFileURL.openConnection();
//设置超时时间为6000毫秒,conn.setConnectionTiem(0);表示没有时间限制
conn.setConnectTimeout(6000);
//连接设置获得数据流
conn.setDoInput(true);
//不使用缓存
conn.setUseCaches(false);
//这句可有可无,没有影响
//conn.connect();
//得到数据流
InputStream is = conn.getInputStream();
//解析得到图片
bitmap = BitmapFactory.decodeStream(is);
//关闭数据流
is.close();
}catch(Exception e){
e.printStackTrace();
}
return bitmap;
}}
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/rl"> <ImageView android:id="@+id/imageView" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/rl"
/>
<ImageView android:id="@+id/imageView2" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/rl"
/>
<TextView android:id="@+id/text" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imageView"/>
</RelativeLayout>但是用模拟器模拟就什么都不显示了,最后系统提示无响应。。
求各位大神指定一下小妹
package com.LabGuider;import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;public class ImageTranslatActivity extends Activity {
/** Called when the activity is first created. */
public ImageView imageView;
public ImageView imageView2;
public Animation animation1;
public Animation animation2;
public TextView text;
public boolean juage = true;
public int images[] = new int[] { R.drawable.a, R.drawable.b,R.drawable.c,R.drawable.d, R.drawable.e
//R.drawable.changer, R.drawable.dataline, R.drawable.preffitication
};public int count = 0;
public Handler handler = new Handler();
public Runnable runnable = new Runnable() {
// @Override
public void run() {
// TODO Auto-generated method stub
AnimationSet animationSet1 = new AnimationSet(true);
AnimationSet animationSet2 = new AnimationSet(true);
imageView2.setVisibility(0);
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
-1f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f);
ta.setDuration(2000);
animationSet1.addAnimation(ta);
animationSet1.setFillAfter(true);
ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
0f, Animation.RELATIVE_TO_SELF, 0f);
ta.setDuration(2000);
animationSet2.addAnimation(ta);
animationSet2.setFillAfter(true);
//iamgeView 出去 imageView2 进来
imageView.startAnimation(animationSet1);
imageView2.startAnimation(animationSet2);
imageView.setBackgroundResource(images[count % 5]);
count++;
imageView2.setBackgroundResource(images[count % 5]);
text.setText(String.valueOf(count));
if (juage)
handler.postDelayed(runnable, 6000);
Log.i("handler", "handler");
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); imageView = (ImageView)this.findViewById(R.id.imageView);
imageView2 = (ImageView) findViewById(R.id.imageView2);
text=(TextView)findViewById(R.id.text);
text.setText(String.valueOf(count));
//将iamgeView先隐藏,然后显示
imageView2.setVisibility(4);
handler.postDelayed(runnable, 2000);
}
public void onPause() {
juage = false;
super.onPause();
}}为了能够动态从网络或服务器加载一批图片并滚动播放,我在代码中加入了一点改动,为的就是能从服务器读取一批图片:JAVA代码:
package com.LabGuider;import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
//import android.view.Window;
//import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;public class ImageTranslatActivity extends Activity {
/** Called when the activity is first created. */
public ImageView imageView;
public ImageView imageView2;
public Animation animation1;
public Animation animation2;
public TextView text;
public boolean juage = true;
String url_1 = "http://114.212.82.54/a.jpg"; //假设从服务器读取一批已知文件名的图片
String url_2 = "http://114.212.82.54/b.jpg";
String url_3 = "http://114.212.82.54/c.jpg";
String url_4 = "http://114.212.82.54/d.jpg";
String url_5 = "http://114.212.82.54/e.jpg";
//得到可用的图片
Bitmap bitmap_1 = getHttpBitmap(url_1);
Bitmap bitmap_2 = getHttpBitmap(url_2);
Bitmap bitmap_3 = getHttpBitmap(url_3);
Bitmap bitmap_4 = getHttpBitmap(url_4);
Bitmap bitmap_5 = getHttpBitmap(url_5);
public Bitmap images[] = new Bitmap[] {bitmap_1,bitmap_2,bitmap_3,bitmap_4,bitmap_5
// R.drawable.changer, R.drawable.dataline, R.drawable.preffitication
};public int count = 0;
public Handler handler = new Handler();
public Runnable runnable = new Runnable() {
// @Override
public void run() {
AnimationSet animationSet1 = new AnimationSet(true);
AnimationSet animationSet2 = new AnimationSet(true);
imageView2.setVisibility(0);
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
-1f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f);
ta.setDuration(2000);
animationSet1.addAnimation(ta);
animationSet1.setFillAfter(true);
ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
0f, Animation.RELATIVE_TO_SELF, 0f);
ta.setDuration(2000);
animationSet2.addAnimation(ta);
animationSet2.setFillAfter(true);
//iamgeView 出去 imageView2 进来
imageView.startAnimation(animationSet1);
imageView2.startAnimation(animationSet2);
imageView.setImageBitmap(images[count % 5]);
count++;
imageView2.setImageBitmap(images[count % 5]);
text.setText(String.valueOf(count));
if (juage)
handler.postDelayed(runnable, 6000);
Log.i("handler", "handler");
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//得到可用的图片
// String url = "http://114.212.82.54/a.jpg";
//Bitmap bitmap = getHttpBitmap(url);
//imageView.setImageBitmap(bitmap);
// imageView.setImageBitmap(bitmap_1);
imageView = (ImageView)this.findViewById(R.id.imageView);
//得到可用的图片
// imageView2.setImageBitmap(bitmap_2);
imageView2 = (ImageView)this.findViewById(R.id.imageView2);
text=(TextView)findViewById(R.id.text);
text.setText(String.valueOf(count));
//将iamgeView先隐藏,然后显示
imageView2.setVisibility(4);
handler.postDelayed(runnable, 2000);
}
public void onPause() {
juage = false;
super.onPause();
}public static Bitmap getHttpBitmap(String url){ URL myFileURL;
Bitmap bitmap=null;
try{
myFileURL = new URL(url);
//获得连接
HttpURLConnection conn=(HttpURLConnection)myFileURL.openConnection();
//设置超时时间为6000毫秒,conn.setConnectionTiem(0);表示没有时间限制
conn.setConnectTimeout(6000);
//连接设置获得数据流
conn.setDoInput(true);
//不使用缓存
conn.setUseCaches(false);
//这句可有可无,没有影响
//conn.connect();
//得到数据流
InputStream is = conn.getInputStream();
//解析得到图片
bitmap = BitmapFactory.decodeStream(is);
//关闭数据流
is.close();
}catch(Exception e){
e.printStackTrace();
}
return bitmap;
}}
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/rl"> <ImageView android:id="@+id/imageView" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/rl"
/>
<ImageView android:id="@+id/imageView2" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/rl"
/>
<TextView android:id="@+id/text" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imageView"/>
</RelativeLayout>但是用模拟器模拟就什么都不显示了,最后系统提示无响应。。
求各位大神指定一下小妹
可以使用service服务下载图片,然后将图片添加的list中,使用IntentService服务!
下载完成后通知主程序取出来显示到界面上