0

我正在寻找一种方法来构建类似于 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);  
                }   

            }


       }
}
4

2 回答 2

1

您应该在下面的链接中有效地加载您的位图,提供完整的指导,如何有效地从网络下载图像

http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

于 2012-10-12T14:52:44.107 回答
1

您需要缓存并可能调整图像大小,请参阅:

http://developer.android.com/training/displaying-bitmaps/index.html

一个完整的例子。

于 2012-10-12T14:53:57.770 回答