简体   繁体   English

随机设置背景图片-Android

[英]Randomly set background image - Android

I have a background set on a login screen, and I want to randomly set the background every time the app is loaded from a pile of images in my drawables folder. 我在登录屏幕上设置了背景,并且每次从我的drawables文件夹中的一堆图像加载应用程序时,我都希望随机设置背景。 However, my app crashes with every attempt at writing code to do this. 但是,每次尝试编写代码来执行此操作时,我的应用程序都会崩溃。

I have my array defined in layout/strings.xml: 我在layout / strings.xml中定义了我的数组:

<?xml version="1.0" encoding="utf-8"?>

<string name="app_name">MyFSU</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>

<style name="DefaultButtonText">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#979797</item>
    <item name="android:gravity">center</item>
    <item name="android:layout_margin">3dp</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textSize">20sp</item>
    <item name="android:shadowColor">#f9f9f9</item>
    <item name="android:shadowDx">1</item>
    <item name="android:shadowDy">1</item>
    <item name="android:shadowRadius">1</item>
</style>

<array name="myImages">
   <item>@drawable/a</item>
   <item>@drawable/b</item>
   <item>@drawable/c</item>
   <item>@drawable/d</item>
   <item>@drawable/e</item>
</array>

And then here's my code to actually generate a random image: 然后这是我的代码,实际生成一个随机图像:

public class MainActivity extends ActionBarActivity {

final RelativeLayout background = (RelativeLayout) findViewById(R.id.back);
Resources res = getResources();
final TypedArray myImages = res.obtainTypedArray(R.array.myImages);
final Random random = new Random();

public void backgrounds(View v) {
    int randomInt = random.nextInt(myImages.length());
    int drawableID = myImages.getResourceId(randomInt, -1);
    background.setBackgroundResource(drawableID);
}

Logcat is showing a null pointer exception at <init> , but I have no clue where that is or how to fix it. Logcat在<init>处显示一个空指针异常,但我不知道该在哪里或如何修复它。 :( :(

Does anyone have any suggestions? 有没有人有什么建议?

You should move 你应该搬家

final RelativeLayout background = (RelativeLayout) findViewById(R.id.back);
Resources res = getResources();
final TypedArray myImages = res.obtainTypedArray(R.array.myImages);

inside onCreate , after setContentView . 里面onCreate ,之后setContentView setContentView(int), inflate and place the inflated view inside the Activity view's hierarchy, allowing you to look for R.id.back , in your case, inside it setContentView(int),将膨胀后的视图膨胀并将其放置在“ Activity视图的层次结构中,从而允许您在其中查看R.id.back

i would make a method that takes a min and max value: 我会做一个方法,需要一个最小值和最大值:

pseudo code 伪码

public static int generateRandomInt(int min,int max)
    {
        return min + (int)(Math.random() * ((max - min) + 1));
    }

public void backgrounds(View v) {
    int randomInt = generateRandomInt(0,myImages.length()-1);
    int drawableID = myImages.getResourceId(randomInt);
    v.setBackgroundResource(getResources().getDrawable(drawableID)); // assuming your array is of resource ids
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM