【问题标题】:Android: Set Random colour background on createAndroid:在创建时设置随机颜色背景
【发布时间】:2014-11-13 09:51:39
【问题描述】:

我想要的是,当我加载我的应用程序时,它会从存储在名为 colours.xml 的值 xml 文件中的预定义字符串列表中随机获得某种彩色背景。

我目前拥有的是一种颜色集,作为在 Eclipse 中使用 gui 编辑器通过字符串颜色代码定义的背景。

我这辈子都不知道如何让背景随机选择 9 个字符串之一并在每次激活 Activity 时显示它。

这方面的指导非常宝贵。

【问题讨论】:

    标签: android random colors background oncreate


    【解决方案1】:

    在colors.xml中

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <item name="blue" type="color">#FF33B5E5</item>
        <item name="purple" type="color">#FFAA66CC</item>
        <item name="green" type="color">#FF99CC00</item>
        <item name="orange" type="color">#FFFFBB33</item>
        <item name="red" type="color">#FFFF4444</item>
        <item name="darkblue" type="color">#FF0099CC</item>
        <item name="darkpurple" type="color">#FF9933CC</item>
        <item name="darkgreen" type="color">#FF669900</item>
        <item name="darkorange" type="color">#FFFF8800</item>
        <item name="darkred" type="color">#FFCC0000</item>
    
        <integer-array name="androidcolors">
            <item>@color/blue</item>
            <item>@color/purple</item>
            <item>@color/green</item>
            <item>@color/orange</item>
            <item>@color/red</item>
            <item>@color/darkblue</item>
            <item>@color/darkpurple</item>
            <item>@color/darkgreen</item>
            <item>@color/darkorange</item>
            <item>@color/darkred</item>
        </integer-array>
    
    </resources> 
    

    在 onCreate() 中

    int[] androidColors = getResources().getIntArray(R.array.androidcolors);
    int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];
    view.setBackgroundColor(randomAndroidColor);
    

    【讨论】:

      【解决方案2】:

      有一个比提供的更好的答案。

      如果您想要一个真正随机颜色,从 res 文件中“随机”选择并不能证明几乎一样健壮。

      改为使用此代码 sn-p:

        Random rnd = new Random();
          currentStrokeColor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
      

      【讨论】:

      • 我的值是 -118692481300,超出整数范围?
      【解决方案3】:

      我想我可以找到一种简单的方法,但需要多长时间才能实现,您从定义的颜色数组中选择随机颜色,然后将该字符串颜色解析为您的背景。

      <?xml version="1.0" encoding="utf-8"?>
      <resources>
          <color name="bright_pink">#FF007F</color>
          <color name="red">#FF0000</color>
          <color name="orange">#FF7F00</color>
          <color name="yellow">#FFFF00</color>
          <color name="chartreuse">#7FFF00</color>
          <color name="green">#00FF00</color>
          <color name="spring_green">#00FF7F</color>
          <color name="cyan">#00FFFF</color>
          <color name="azure">#007FFF</color>
          <color name="blue">#0000FF</color>
          <color name="violet">#7F00FF</color>
          <color name="magenta">#FF00FF</color>
      <array name="rainbow">
          <item>@color/bright_pink</item>
          <item>@color/red</item>
          <item>@color/orange</item>
          <item>@color/yellow</item>
          <item>@color/chartreuse</item>
          <item>@color/green</item>
          <item>@color/spring_green</item>
          <item>@color/cyan</item>
          <item>@color/azure</item>
          <item>@color/blue</item>
          <item>@color/violet</item>
          <item>@color/magenta</item>
      </array>
      

      比这个java代码

      String[] array = context.getResources().getStringArray(R.array.animals_array);
      String randomStr = array[new Random().nextInt(array.length)];
      
      //here you define your layout `
      
      LinearLayout myLayout = (LinearLayout) findViewById(R.id.that_linear);
      
      myLayout.setBackgroundColor(Color.parseColor(randomStr));
      

      【讨论】:

      • @Phil3992 刚刚添加了定义,亲爱的,检查一下。
      【解决方案4】:

      您可以将变量Random rnd = new Random(); 设置为随机数(将生成一个介于 0 和 1 之间的伪随机数)。那么你可以说:

          if (rnd < 0.09) {
              //pick first colour
          } else if (rnd >= 0.09 && rnd < 0.18) {
              //pick second colour
          } else if (rnd >= 0.18 && rnd < 0.27) {
              //pick second colour    
          } else // etc etc up to 1.0 when you will have 9 options, each with an equal chance of randomly being picked
      

      这样,随机数rnd 将确定每次调用onCreate() 时背景的颜色。

      【讨论】:

        【解决方案5】:

        您可以使用ColorGenerator 类获取随机的color.code

        下面给出的sn-p

        ColorGenerator generator = ColorGenerator.MATERIAL;
        
        int color=generator.getRandomColor();
        

        在这里你可以使用视图参考来设置颜色

        mUserName.setText("Suraj");
        
        mUserName.setTextColor(color);  //it will populate name with random color each time you open your activity
        

        【讨论】:

          【解决方案6】:

          试试这个简单又容易的方法

          private boolean isOne = false, isTwo = false, isThree = false, isFour = false, isFive = false;
          
          if(position==0){
                      holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color1));
                      holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color1));
                      isTwo = true;
                  }else if(isOne){
                      holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color1));
                      holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color1));
                      isOne = false;
                      isTwo = true;
                  }else if(isTwo){
                      holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color2));
                      holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color2));
                      isTwo = false;
                      isThree = true;
                  }else if(isThree){
                      holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color3));
                      holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color3));
                      isThree = false;
                      isFour = true;
                  }else if(isFour){
                      holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color4));
                      holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color4));
                      isFour = false;
                      isFive = true;
                  }else if(isFive){
                      holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color5));
                      holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color5));
                      isFive = false;
                      isOne = true;
                  }
          

          【讨论】:

            猜你喜欢
            • 2020-03-13
            • 1970-01-01
            • 2015-04-26
            • 1970-01-01
            • 1970-01-01
            • 2013-10-14
            • 2014-02-12
            • 2013-08-04
            • 1970-01-01
            相关资源
            最近更新 更多