Jonas Shinaniganz
Jonas Shinaniganz

Reputation: 179

android studio drawable images iteration

i put some images into the res/drawable folder. They are named (s1.png, s2.png, s3png.., 2n.png).

I want to loop (and then process) them. I would do that like:

    for (int i = 1; i < n; i++) {
        HavingFunWithPNGS(R.drawable.s + IntToStr('i'));
    }

Ofcourse thats not how it works. How does it? Thanks in advance.

Edit: My coreproblem is, to convert the filename-strings to the corresponding RessourceID's that android-studio assigns.

Upvotes: 1

Views: 1635

Answers (3)

SilentKnight
SilentKnight

Reputation: 14031

Try to call HavingFunWithPNGS(ResUtil.getInstance().getDrawableIdByName(this, String.valueOf("s"+i))). And below are codes of ResUtil:

public class ResUtil {
private static ResUtil instance;

private ResUtil() {
}

public final static ResUtil getInstance() {
    if (instance == null) {
        instance = new ResUtil();
    }
    return instance;
}

private final int getIdByNames(Context ctx, String resType, String fileName) {
    if (ctx == null || resType == null || fileName == null || "".equals(resType) || "".equals(fileName)) {
        return 0;
    }
    int id = ctx.getResources().getIdentifier(fileName, resType, ctx.getPackageName());
    return id;
}

public final int getAnimIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "anim", fileName);
}

public final int getAnimatorIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "animator", fileName);
}

public final int getArrayIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "array", fileName);
}

public final int getAttrIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "attr", fileName);
}

public final int getBoolIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "bool", fileName);
}

public final int getColorIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "color", fileName);
}

public final int getDimenIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "dimen", fileName);
}

public final int getDrawableIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "drawable", fileName);
}

public final int getFractionIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "fractiont", fileName);
}

public final int getIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "id", fileName);
}

public final int getIntegerIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "integer", fileName);
}

public final int getInterpolatorIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "interpolator", fileName);
}

public final int getLayoutIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "layout", fileName);
}

public final int getMenuIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "menu", fileName);
}

public final int getMipmapIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "mipmap", fileName);
}

public final int getPluralsIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "plurals", fileName);
}

public final int getRawIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "raw", fileName);
}

public final int getStringIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "string", fileName);
}

public final int getStyleIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "style", fileName);
}

public final int getStyleableIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "styleable", fileName);
}

public final int getTransiitonIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "transition", fileName);
}

public final int getXmlIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "xml", fileName);
}
}

Upvotes: 0

Oguz Babaoglu
Oguz Babaoglu

Reputation: 299

Expanding on @mrek's answer, if you are using shrinkResources don't forget to add your ids to a keep file. Otherwise they will be removed.

//file res/raw/keep.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@drawable/s*,@drawable/2n"/>

" * " is a wildcard, so this would cover s1, s2, s3 etc.

See http://tools.android.com/tech-docs/new-build-system/resource-shrinking for details

getResources() and getPackageName() are methods of Context. You will need a reference to a context to use these.

Upvotes: 1

mrek
mrek

Reputation: 1888

If you want to get drawable id, do it this way (passing it as a String imageName):

int id = getResources().getIdentifier(imageName, type, package);

If you want to see the code, please look at this answer. You should pass the image name in String variable, so I think you should rather use it this way:

String imageName = "s" + i;

because it looks better, you don't need to convert anything (java will do it automatically) and it is easily readable.

Upvotes: 0

Related Questions