0

我有一个 Activity 用作图片库并且效果很好。但它从具有可绘制对象引用的整数数组中加载图像。我想做的是从互联网上获取图像并将它们存储在内部存储器中,然后在我的画廊中使用它们。

下面是我的代码。任何帮助将不胜感激。

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

1 回答 1

0

使用类似https://github.com/nostra13/Android-Universal-Image-Loader的东西。或者您可以查看https://github.com/cyrilmottier/GreenDroid中的代码。我个人喜欢第一个,因为它只专注于图像加载。

于 2012-04-19T21:52:58.453 回答