如何用滑动库显示随机gif图像?

时间:2017-04-12 09:23:07

标签: android animated-gif android-glide

我想用Glide库显示随机gif图像。

我有四张gif图片。每次我想在应用程序打开时显示不同的gif图像(四个gif图像中)?

对于带滑行的单个gif图像,我使用了以下代码 -

<ImageView
android:id="@+id/my_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

activiy_main

<div class="navbar-collapse collapse show dropdown" id="navbarCollapse" data-hover="dropdown" data-animations="fadeInDown fadeInRight fadeInUp fadeInLeft" aria-expanded="true">
        <ul class="nav navbar-nav mr-auto mainmenu">
          <li class="nav-item ml-2 mr-2 dropdown ">
            <a class="nav-link" href="#">Hotel 111111</a>
            <span class="dropdown-toggle caret" data-toggle="dropdown" role="button" aria-expanded="false"></span>
            <ul class="dropdown-menu dropdownhover-bottom">
              <li class="dropdown-item"><a class="nav-link" href="#">Geschichte und Tradition</a></li>
              <li class="dropdown-item"><a class="nav-link" href="#">Inklusiv-Leistungen</a></li>
              <li class="dropdown-item dropdown-submenu">
                  <a class="nav-link" href="#">Wohnen 111111</a>
                    <span class="dropdown-toggle caret" data-toggle="dropdown" role="button" aria-expanded="false"></span>
                    <ul class="dropdown-menu dropdownhover-right">
                            <li class="dropdown-item"><a class="nav-link" href="#">Action</a></li>
                            <li class="dropdown-item dropdown-submenu show">
                                <a href="#" class="nav-link">Dropdown</a>
                                <span class="dropdown-toggle caret" data-toggle="dropdown" role="button" aria-expanded="true"></span>
                                <ul class="dropdown-menu dropdownhover-right">
                                    <li class="dropdown-item dropdown-submenu">
                                        <a href="#" class="nav-link">Dropdown</a>
                                        <span class="dropdown-toggle caret" data-toggle="dropdown" role="button" aria-expanded="true"></span>
                                        <ul class="dropdown-menu dropdownhover-right">
                                            <li class="dropdown-item"><a href="#">Action</a></li>
                                            <li class="dropdown-item"><a href="#">Another action</a></li>
                                            <li class="dropdown-item"><a href="#">Something else here</a></li>
                                            <li class="dropdown-item"><a href="#">Separated link</a></li>
                                            <li class="dropdown-item"><a href="#">One more separated link</a></li>
                                        </ul>
                                    </li>
                                </ul>
                            </li>
                        </ul>
              </li>
              <li class="dropdown-item"><a class="nav-link" href="#">Pauscalen</a></li>
            </ul>
          </li>
          <li class="nav-item ml-2 mr-2 dropdown">
            <a class="nav-link js-activated" href="#">Reiten</a>
            <span class="dropdown-toggle caret" data-toggle="dropdown" role="button" aria-expanded="false"></span>
            <ul class="dropdown-menu dropdownhover-left">
              <li class="dropdown-item"><a class="nav-link" href="#">Geschie und Tradition</a></li>
              <li class="dropdown-item"><a class="nav-link" href="#">Reitnteuer</a></li>
              <li class="dropdown-item"><a class="nav-link" href="#">Uere Pferde</a></li>
              <li class="dropdown-item"><a class="nav-link" href="#">Reitunterricht</a></li>
              <li class="dropdown-item"><a class="nav-link" href="#">Reithalle</a></li>
              <li class="dropdown-item dropdown">
                  <a class="nav-link js-activated" href="#">Reiten</a>
                  <span class="dropdown-toggle caret" data-toggle="dropdown" role="button" aria-expanded="true"></span>
                  <ul class="dropdown-menu dropdownhover-right">
                    <li class="dropdown-item"><a class="nav-link" href="#">Geschi Tradition</a></li>
                    <li class="dropdown-item"><a class="nav-link" href="#">Reitanteuer</a></li>
                    <li class="dropdown-item"><a class="nav-link" href="#"> Pferde</a></li>
                    <li class="dropdown-item"><a class="nav-link" href="#">Reitunterricht</a></li>
                    <li class="dropdown-item"><a class="nav-link" href="#">Reithalle</a></li>
                  </ul>
              </li>
            </ul>
          </li>
          <li class="nav-item ml-2 mr-2 dropdown">
            <a class="nav-link" href="#">Kinder</a>
             <span class="dropdown-toggle caret" data-toggle="dropdown" role="button" aria-expanded="true"></span>
            <ul class="dropdown-menu dropdownhover-bottom">
              <li class="dropdown-item"><a class="nav-link" href="#">Kintreuung</a></li>
              <li class="dropdown-item"><a class="nav-link" href="#">Krstammtisch</a></li>
              <li class="dropdown-item"><a class="nav-link" href="#">-Aktivprogramm</a></li>
            </ul>
          </li>
          <li class="nav-item ml-2 mr-2 dropdown">
            <a class="nav-link" href="#">Wellness</a>
            <span class="dropdown-toggle caret" data-toggle="dropdown" role="button" aria-expanded="true"></span>
            <ul class="dropdown-menu dropdownhover-bottom">
              <li class="dropdown-item"><a class="nav-link" href="#">SA- und Fitnessbereich</a></li>
              <li class="dropdown-item"><a class="nav-link" href="#">S-Kinder</a></li>
              <li class="dropdown-item"><a class="nav-link" href="#">SA-Anwendungen</a></li>
              <li class="dropdown-item"><a class="nav-link" href="#">Badesee</a></li>
            </ul>
          </li>
          <li class="nav-item ml-2 mr-2 dropdown">
            <a class="nav-link" href="#">Aktive</a>
            <span class="dropdown-toggle caret" data-toggle="dropdown" role="button" aria-expanded="true"></span>
            <ul class="dropdown-menu dropdownhover-bottom">
              <li class="dropdown-item"><a class="nav-link" href="#">Sommer</a></li>
              <li class="dropdown-item"><a class="nav-link" href="#">Winter</a></li>
            </ul>
          </li>
          <li class="nav-item ml-2 mr-2"><a class="nav-link" href="#"> Region</a></li>
        </ul>

      </div>
</nav>

2 个答案:

答案 0 :(得分:0)

制作一系列drawable:

 private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,

    };

然后随机选择图像:

Random random = new Random();
int indexToGetImageFrom = random.nextInt(sizeOfYourArray);

上面的代码会为您生成一个随机数。 Random类的nextInt方法生成一个介于0(包括)和给定参数(不包括)之间的数字。

在滑翔库中使用:

Glide.with(this).load(mThumbIds [i])。into(imageViewTarget);其中i是indexToGetImageFrom

每次生成新号码并显示新的图像视图时。

答案 1 :(得分:0)

您应该在值目录res/values/arrays.xml

中创建一系列drawable
<array name="gif_drawables">
    <item>@drawable/gif_1</item>
    <item>@drawable/gif_2</item>
    <item>@drawable/gif_3</item>
    <item>@drawable/gif_4</item>
</array>

然后只需选择它:

TypedArray images = getResources().obtainTypedArray(R.array.gif_drawables);
int choice = (int) (Math.random() * images.length());
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView);
Glide.with(this).load(images.getResourceId(choice, R.drawable.gif_1)).asGif().into(imageViewTarget);
images.recycle();

在这个答案中也提到:how to select from resources randomly (R.drawable.xxxx)

这是做什么的:

  • 创建一个XML数组(因为你知道你想要多少个gif)
  • 使用所述数组创建TypedArray对象。
  • 然后使用Math类根据TypedArray的长度生成随机整数。
  • 根据该选择,然后根据该位置获取资源ID(在本例中称为choice
  • 最后为了帮助进行内存管理,它会在使用后回收数组。

扩展我的评论:

您还应该使用Glide 3.0引入的asGif()函数。