Android 随机显示imageView背景图

Android 随机显示imageView背景图

在xml文件中加入imageView控件,id可自定义。
 <ImageView
        android:id="@+id/bg1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        />
在MainActivity.Java文件中加入随机图片代码。
    private ImageView image;//这个要写在方法外面,不要写错位置了。
  
    image=(ImageView) findViewById(R.id.bg1);
        int[] ImageArray = new int[]{R.drawable.q1,R.drawable.q2};//两张图片,位置在res/drawable/下,注意命名方式
        Random random = new Random();
        random.nextInt(2);//随机函数,0-1 ,不包括2
        
        for(int i=0;i<2;i++){
          int    randomnumber = random.nextInt(2);//随机数0  1 用作图片数组下标
        }
        image.setBackgroundResource(ImageArray[randomImage]);//设置背景图

我做的是随机奖项,大致效果这样:背景奖项是随机的,奖项就是一图片,图片是找的,还没做好。在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值