Question

I have an Activity which is used as an image gallery and works quite well. But it loads images from integer array which has references from drawables. And what I want to do is getting images from internet and store them in internal memory then use them in my gallery.

Here is my code below. Any help will be appreciated.

import okan.apps.tabbar.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

 public class Activities extends Activity implements ViewFactory {

ImageSwitcher imageSwitcher ;

Integer[] imageList = {
        R.drawable.gallery,
        R.drawable.menu,
        R.drawable.promotion,
        R.drawable.info,
        R.drawable.activities       
};

int curIndex=0;
int downX,upX;

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activities_main);

     imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
     imageSwitcher.setFactory(this);
     imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
     imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));

     imageSwitcher.setImageResource(imageList[curIndex]);
     imageSwitcher.setOnTouchListener(new OnTouchListener() {
     public boolean onTouch(View v, MotionEvent event) {

         if (event.getAction() == MotionEvent.ACTION_DOWN) {
             downX = (int) event.getX(); 
             Log.i("event.getX()", " downX " + downX);
             return true;
         } 

         else if (event.getAction() == MotionEvent.ACTION_UP) {
             upX = (int) event.getX(); 
             Log.i("event.getX()", " upX " + downX);
             if (upX - downX > 100) {

                 //curIndex  current image index in array viewed by user
                 curIndex--;
                 if (curIndex < 0) {
                     curIndex = imageList.length-1;
                 }

                 imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(Activities.this,R.anim.slide_in_left));
                 imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(Activities.this,R.anim.slide_out_right));

                 imageSwitcher.setImageResource(imageList[curIndex]);
                 //GalleryActivity.this.setTitle(curIndex);
             } 

             else if (downX - upX > -100) {

                 curIndex++;
                 if (curIndex > 4) {
                     curIndex = 0;
                 }

                 imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(Activities.this,R.anim.slide_in_right));
                 imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(Activities.this,R.anim.slide_out_left));

                 imageSwitcher.setImageResource(imageList[curIndex]);
                 //GalleryActivity.this.setTitle(curIndex);
             }
                 return true;
             }
             return false;
         }
     });
}
public View makeView() {
    ImageView i = new ImageView(this);  
    i.setScaleType(ImageView.ScaleType.FIT_CENTER);
    return i;
}
}
Était-ce utile?

La solution

Use something like https://github.com/nostra13/Android-Universal-Image-Loader . Or you can check the code in https://github.com/cyrilmottier/GreenDroid . Personally I liked the first one since it was focused only on image loading.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top