簡體   English   中英

在 Android 中的 ImageView 中加載圖像

[英]Load Images in ImageView in Android

在我的應用程序中......有一些圖像,如 temp1.jpg、temp2.​​jpg .....upto temp35.jpg,

所以在單擊按鈕時,我想在 ImageView 中加載一張一張的圖像......我想做:

cnt=1;
imagename="temp" + cnt + ".jpg";
cnt++;

所以我的困惑是“無論如何都可以從字符串(圖像名稱變量)(如 temp1.jpg 等)加載圖像視圖中的圖像。”

你可以試試這個:

int cnt = 1;
//Bitmap bitmap = BitmapFactory.decodeFile("temp" + cnt + ".jpg");
int imageResource = getResources().getIdentifier("drawable/temp" + cnt + ".jpg", null, getPackageName());
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imageResource);
imageView.setImageBitmap(bitmap);
cnt++;

希望這就是你要找的。

為什么不像

File f = new File(PathToFiles + "/temp" + cnt + ".jpg");
if (f.exists()) {
  Drawable d = Drawable.createFromPath(f);
  imageview.setImageDrawable(d);
}

我實施了以下解決方案,它對我有用:

while(cnt!=n)
{
 String icon="temp" + cnt;
 int resID =
 getResources().getIdentifier(icon,"drawable","testing.Image_Demo");
 imageView.setImageResource(resID);
 cnt++; 
}

我不知道這是否是最好的解決方案,但您可以制作一個將圖像名稱映射到資源的哈希表。

Hashtable map;
map.put("temp1", R.drawable.temp1) // assuming temp1.jpg is in /drawable

然后你可以從一個drawable加載ImageView。

 String imageName = "temp" + n;
 Drawable d = getResources().getDrawable((int)map[imageName]);
 ImageView i = new ImageView(this);
 i.setImageResource(d);

這個對我有用:

Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.my_image)
.copy(Bitmap.Config.ARGB_8888, true);
Canvas c = new Canvas(myBitmap);
// etc.  ....... 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM