我正在寻找一种方法来构建类似于 ViewFlipper 的图像滑块 - 用户通过用手指向左或向右滑动来导航图像。ViewFlipper 的问题在于,当我从 Web 下载大量图像时,应用程序的内存不足。我还使用 AsyncTask 将图像加载到 ViewFlipper 中,但我觉得我需要做很多复杂的工作才能使这项工作像我想象的那样工作。是否已经有针对此类问题的解决方案,我可以在我的应用程序中修改和实施?
这是我目前拥有的:
package com.example.viewflippertest;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class MainActivity extends Activity {
private ViewFlipper vf;
private float lastX;
public String[] photos = {
"http://ilijaveselica.com/Uploads/Photos/538.jpg",
"http://ilijaveselica.com/Uploads/Photos/528.jpg",
"http://ilijaveselica.com/Uploads/Photos/541.jpg",
"http://ilijaveselica.com/Uploads/Photos/542.jpg",
"http://ilijaveselica.com/Uploads/Photos/543.jpg",
"http://ilijaveselica.com/Uploads/Photos/544.jpg",
"http://ilijaveselica.com/Uploads/Photos/514.jpg",
"http://ilijaveselica.com/Uploads/Photos/511.jpg",
"http://ilijaveselica.com/Uploads/Photos/545.jpg"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vf = (ViewFlipper) findViewById(R.id.view_flipper);
new BackgroundAsyncTask().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction())
{
case MotionEvent.ACTION_DOWN:
{
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP:
{
float currentX = touchevent.getX();
if (lastX < currentX)
{
if (vf.getDisplayedChild()==0)
break;
vf.setInAnimation(this, R.anim.in_from_left);
vf.setOutAnimation(this, R.anim.out_to_right);
vf.showPrevious();
}
if (lastX > currentX)
{
if (vf.getDisplayedChild()==vf.getChildCount()-1)
break;
vf.setInAnimation(this, R.anim.in_from_right);
vf.setOutAnimation(this, R.anim.out_to_left);
vf.showNext();
}
break;
}
case MotionEvent.ACTION_MOVE:
{
float tempX = touchevent.getX();
int scrollX = (int) (tempX - lastX);
//vf.scrollBy(scrollX, 0);
break;
}
}
return false;
}
public static Object fetch(String address) throws MalformedURLException,
IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
public static Drawable ImageOperations(String url) {
try {
InputStream is = (InputStream) fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
}
}
private class BackgroundAsyncTask extends AsyncTask<Integer, Integer, Integer> {
Map<String,Drawable> imagesMap = new HashMap<String, Drawable>();
int photosCounter = 0;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
//myProgress = 0;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
//progressBar.setProgress(values[0]);
}
@SuppressLint({ "NewApi", "NewApi" })
@Override
protected Integer doInBackground(Integer... params) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// TODO Auto-generated method stub
final ProgressBar spinner = (ProgressBar) findViewById(R.id.spinner);
for(int i=0;i<photos.length;i++)
{
Drawable image =ImageOperations(photos[i]);
imagesMap.put(String.valueOf(i), image);
photosCounter++;
if(i == 2)
break;
}
spinner.getHandler().post(new Runnable() {
public void run() {
spinner.setVisibility(View.GONE);
}
});
return null;
}
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
for(int i=0;i<photosCounter;i++)
{
final ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageDrawable(imagesMap.get(String.valueOf(i)));
vf.addView(imageView);
}
}
}
}