回答
將名爲img_0在你的資源來img_n /一些圖像繪製的文件夾
佈局(RES /佈局/ rnd_images .XML):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<ImageView
android:id="@+id/imgRandom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
代碼:
package com.example.app;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class MainActivity
extends Activity
{
final Random rnd = new Random();
@Override
protected void onCreate(
final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.rnd_images);
final ImageView img = (ImageView) findViewById(R.id.imgRandom);
// I have 3 images named img_0 to img_2, so...
final String str = "img_" + rnd.nextInt(2);
img.setImageDrawable
(
getResources().getDrawable(getResourceID(str, "drawable",
getApplicationContext()))
);
}
protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0)
{
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
}
else
{
return ResourceID;
}
}
}
請注意,您必須rnd.nextInt(2)設置爲rnd.nextInt(最大值 - 1),因爲RND從0
完美!非常感謝@vyger – mali
不客氣,親愛的! –
開始考慮你有drawable
10個圖像與名稱格式爲 your_image_1
,your_image_2
。 ...高達your_image_10
你可以在應用程序的開始,每次
public void onCreate(Bundle instance){
//....
Random r = new Random();
int randomNumber = r.nextInt(10 - 1) + 1;
ImageView image = (ImageView) findViewById(R.id.image);
String imageName = "your_image_" + randomNumber;
image_ID = getResources().getIdentifier(imageName, "drawable", getPackageName());
image.setBackgroundResource(image_ID);
}
不工作!謝謝。 – mali
可以顯示在啓動時爲@Saqib隨機圖像使用下面的代碼的隨機圖像設置ImageView
提到,或者你可以循環顯示圖像,即在第一次啓動時,您可以顯示第一個圖像e,然後在第二個,然後重複這個循環。爲此,您需要做的就是優先存儲整數,並在每次啓動應用程序時遞增整數的值,並優先存儲更新的值。
- 1. 每次啓動Android應用程序時顯示啓動畫面
- 2. 每次啓動時更改應用程序的啓動圖像
- 3. 每次應用程序啓動時顯示初始視圖
- 4. 推送通知每次在應用程序啓動時顯示
- 5. 在Android上每次啓動應用程序只顯示一次警告
- 6. 每天顯示的隨機圖像
- 7. 如何在wpf應用程序啓動時顯示圖像?
- 8. 在Android中啓動應用程序時出現隨機應用程序崩潰
- 9. 啓動畫面不顯示每次啓動的應用程序Iphone
- 10. 每次啓動應用程序時使用不同圖像的Android啓動畫面
- 11. 顯示圖像的Android應用程序
- 12. 如何在每次手機開機時自動啓動我的應用程序?
- 13. 第二次顯示不同的視圖應用程序啓動
- 14. Android OpenGL應用程序隨機重新啓動手機
- 15. 每次啓動新應用程序
- 16. jQuery的顯示隨機記錄每次
- 17. Android ListView getView隨機顯示圖像
- 18. 啓動iOS應用程序並顯示圖像的步驟
- 19. 隨機顯示隨機圖像jquery
- 20. 如何在首次啓動應用程序時顯示UIViewController?
- 21. 如何在首次啓動應用程序時顯示頁面
- 22. 僅在第一次啓動應用程序時顯示UIAlertView
- 23. 如何在第一次應用程序啓動時顯示pushviewcontroller?
- 24. android啓動應用程序啓動時的相機活動或同時啓動
- 25. ASHX圖像處理程序隨機顯示/隱藏圖像
- 26. 顯示默認圖標啓動器的Android應用程序
- 27. 每次都在同一活動上啓動Android應用程序
- 28. Netbeans CUnit每次啓動應用程序時運行一次
- 29. 啓動映像不顯示啓動應用程序
- 30. 存儲圖像,並顯示它,當我再次啓動應用程序
這不是SO的工作原理。首先,你可以谷歌的一些想法,嘗試一下,如果它仍然無法回來,並告訴我們你做了什麼。 –