热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何从drawable文件夹动态获取图像?-howtogetimagesdynamicallyfromdrawablefolder

Ihaveanarraylikethis.我有一个这样的数组。intimage[]{R.drawable.d002_p001,R.drawable.d002_p002,R.dr

I have an array like this.

我有一个这样的数组。

int image[] = {R.drawable.d002_p001,R.drawable.d002_p002,R.drawable.d002_p003,
                   R.drawable.d002_p004,R.drawable.d002_p005,R.drawable.d002_p006};

Right now I have 6 images so I am statically given the name.

现在我有6个图像,所以我静态地给它命名。

If I have some 50 images I cant give each and every file name in array so it needs to be dynamic how can I achieve this.

如果我有50个图像,我不能给数组中的每个文件名,所以它必须是动态的,我怎么才能实现这个。

9 个解决方案

#1


106  

You can use getIdentifier()

您可以使用getIdentifier()

for (int j = 1; j <6; j++) {
   Drawable drawable = getResources().getDrawable(getResources()
                  .getIdentifier("d002_p00"+j, "drawable", getPackageName()));
}

#2


12  

You can also use this:

你也可以用这个:

int res = getResources().getIdentifier(":drawable/abc", null, null);

#3


6  

Something like this could work

像这样的东西可以用

Field[] drawables = android.R.drawable.class.getFields();
for (Field f : drawables) {
    try {
        System.out.println("R.drawable." + f.getName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

#4


2  

public static Drawable getImage(Context context, String name) {
        return context.getResources().getDrawable(context.getResources().getIdentifier(name, "drawable", context.getPackageName()));
}

#5


2  

Use the following line for getting drawable dynamically:

使用以下行动态绘制:

Drawable drawable = this.getResources().getDrawable(R.drawable.yourDrawableID);

This will give you the desired Drawable.

这会给你想要的画。

#6


1  

String[] names = new String []{"yout names..."};
    for(String n: names) {
        Utils.GetDrawableByName(n,this);
    }

public class Utils {
public static Drawable GetDrawableByName(String name,Activity context){
    Resources res = context.getResources();
    return res.getDrawable(res.getIdentifier(name,"drawable",context.getPackageName()));
    }
}

#7


0  

We can take advantage of Imageview setImageResource as this will efficient than drawable seems, refer below code for the same.

我们可以利用Imageview setImageResource,因为这比drawable看起来更有效,请参考下面的代码。

The below code can be used to show the image like gif incase if you have the multiple split image of gif. Just split the gif into individual png from a online tool and put image in the drawable like the below order

下面的代码可以用来显示gif这样的图像,如果你有gif的多个分割图像。只需将gif从一个在线工具中拆分为单独的png,并按如下顺序将图像放入可绘制的文件中

image_1.png, image_2.png, etc.

image_1。png、image_2。png等。

Have the handler to change the image dynamically.

让处理程序动态地改变图像。

int imagePosition = 1;
    Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            public void run() {
                updateImage();
            }
        };




    public void updateImage() {

                appInstance.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        int resId = getResources().getIdentifier("image_" + imagePosition, "drawable", appInstance.getPackageName());
                        gifImageViewDummy.setImageResource(resId);
                        imagePosition++;
    //Consider you have 30 image for the anim
                        if (imagePosition == 30) {
//this make animation play only once
                            handler.removeCallbacks(runnable);

                        } else {
    //You can define your own time based on the animation
                            handler.postDelayed(runnable, 50);
                        }

//to make animation to continue use below code and remove above if else
// if (imagePosition == 30)
//imagePosition = 1;
// handler.postDelayed(runnable, 50);
// 
                    }
                });
              }

#8


0  

package com.example.studio.snakes;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

int[] dices = {
        R.drawable.one,
        R.drawable.two,
        R.drawable.three,
        R.drawable.four,
        R.drawable.five,
        R.drawable.six,
};


public void rollTapped(View view){
 Log.i("Button","Button Tapped");
    Random rand = new Random();
    int randomnum = rand.nextInt(6);
    Log.i("Random","Random number is " + randomnum );
    ImageView dice=findViewById(R.id.imageView2);
    dice.setImageResource(dices[randomnum]);


}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
}

#9


-10  

use this code to create array and later use that array

使用此代码创建数组,然后使用该数组

int image[] = new int[50];
for (int i = 1 ; i <= 50 ; i++)
{
images[i]  = "R.drawable.d002_p00"+i;
}

main thing you have to take care is the file name must start with "d002_p00" this and after there is digit 1 to 50

你要注意的主要事情是文件名必须以“d002_p00”开头,在数字1到50之后


推荐阅读
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • java drools5_Java Drools5.1 规则流基础【示例】(中)
    五、规则文件及规则流EduInfoRule.drl:packagemyrules;importsample.Employ;ruleBachelorruleflow-group ... [详细]
  • 工作经验谈之-让百度地图API调用数据库内容 及详解
    这段时间,所在项目中要用到的一个模块,就是让数据库中的内容在百度地图上展现出来,如经纬度。主要实现以下几点功能:1.读取数据库中的经纬度值在百度上标注出来。2.点击标注弹出对应信息。3 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 标题: ... [详细]
  • 超级简单加解密工具的方案和功能
    本文介绍了一个超级简单的加解密工具的方案和功能。该工具可以读取文件头,并根据特定长度进行加密,加密后将加密部分写入源文件。同时,该工具也支持解密操作。加密和解密过程是可逆的。本文还提到了一些相关的功能和使用方法,并给出了Python代码示例。 ... [详细]
  • 在本教程中,我们将看到如何使用FLASK制作第一个用于机器学习模型的RESTAPI。我们将从创建机器学习模型开始。然后,我们将看到使用Flask创建AP ... [详细]
author-avatar
我叫yyson_836
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有