0
我有一個用作圖像庫的活動,效果很好。但它從具有可繪製引用的整數數組中加載圖像。我想要做的是從互聯網上獲取圖像,並將它們存儲在內部存儲器中,然後在我的圖庫中使用它們。從互聯網獲取圖像並將它們動態存儲在內部存儲器中Android
這是我的代碼如下。任何幫助將不勝感激。
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;
}
}
這些對於我想要做的事情來說相當複雜。基本上,從內部存儲器提供圖像緩衝而不是從drawable引用我的整型數組就足夠了。我可以處理其餘部分。 – osayilgan 2012-04-19 21:59:33
因此,如果我能夠正確理解您,則無法將從互聯網下載的圖像稱爲資源中的圖像。因此不必使用int []數組,而必須使用drawable或bitmap數組。使用setImageBitmap(或者是類似的)來設置imageview的圖像。 – Siyamed 2012-04-19 22:51:49
將網址內容轉換爲圖片請檢查這些庫代碼或像http://stackoverflow.com/questions/3090650/android-loading-an-image-from-the-web-with-asynctask。你必須在後臺線程中下載圖像並在主線程中調用ui change(setImageBitmap)。 – Siyamed 2012-04-19 22:55:36