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);
}
}
}
}