我在一个区域中遇到问题,我想在4张图片之间进行“自动幻灯片放映”(从网址中下载) 我希望淡入淡出并淡出图片之间的变化。 我唯一能做的就是幻灯片放映:
淡入图片,然后“重置”视图空白,然后淡入新图片..
而不是: 淡入图片,然后淡入新图像.. 或:淡入图片,然后在新图像中淡出然后空白..
你有什么想法吗?这是我的代码://我稍微减少了“获取URL位图”..
public class SliderActivity extends Activity {
public int currentimageindex=0;
Timer timer;
TimerTask task;
ImageView slidingimage;
public boolean ok;
public Bitmap bm;
public Bitmap bm0,bm1,bm2,bm3;
String url11 = "https://www.weblink/pic1.png";
String url22 = "https://www.weblink/pic2.png";
String url33 = "https://www.weblink/pic3.png";
String url44 = "https://www.weblink/pic4.png";
String[] URLlink = {url11,url22,url33,url44};
Bitmap[] IMAGE_IDS = { bm0, bm1, bm2,bm3 };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mygame);
final Handler mHandler = new Handler();
downloadPicToArray();
final Runnable mUpdateResults = new Runnable() {
public void run() {
AnimateandSlideShowout();
AnimateandSlideShow();
}
};
int delay = 1; // delay.. 1 sec = 1000.
int period = 20000; // repeat every...1 sec. (2000)
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
}, delay, period);
}
private void downloadPicToArray() {
try {
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
bm0 = loadBitmap(URLlink[0], bmOptions);
bm1 = loadBitmap(URLlink[1], bmOptions);
bm2 = loadBitmap(URLlink[2], bmOptions);
bm3 = loadBitmap(URLlink[3], bmOptions);
}
catch (...){
}
IMAGE_IDS[0] = bm0;
IMAGE_IDS[1] = bm1;
IMAGE_IDS[2] = bm2;
IMAGE_IDS[3] = bm3;
}
public static Bitmap loadBitmap(String URL, BitmapFactory.Options options) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in, null, options);
if (in == null){
bitmap = null;
}
in.close();
} catch (IOException e1) {
bitmap = null;
}
return bitmap;
}
private static InputStream OpenHttpConnection(String strURL)
throws IOException {
InputStream inputStream = null;
URL url = new URL(strURL);
URLConnection conn = url.openConnection();
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
} catch (Exception ex) {
inputStream = null;
}
return inputStream;
}
public void onClick(View v) {
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
private void AnimateandSlideShow() {
Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
Animation fadeinimage = AnimationUtils.loadAnimation(this, R.anim.fade_in);
Animation fadeoutimage = AnimationUtils.loadAnimation(this, R.anim.fade_out);
slidingimage = (ImageView)findViewById(R.id.ImageView3_Left);
//slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
slidingimage.setImageBitmap(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
slidingimage.startAnimation(rotateimage);
currentimageindex++;}
private void AnimateandSlideShowout() {
Animation fadeoutimage = AnimationUtils.loadAnimation(this, R.anim.fade_out);
slidingimage = (ImageView)findViewById(R.id.ImageView3_Left);
slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
slidingimage.startAnimation(fadeoutimage);
}
}
动画就像这样:
//fade out
<?xml version="1.0" encoding="utf-8" ?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="2000">
</alpha>
</set>
//fade in
<?xml version="1.0" encoding="utf-8" ?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000">
</alpha>
</set>