Can a pack of icons in png be used in Android? [closed]

4

I'm learning Android and I'm doing an application of time. Depending on the time I do I want to put a different icon, my question is if you can use the packs of icons that come in PNG in Android, or if I have to have an icon of each type in a different PNG. And if possible, from the activity how each PNG icon would be accessed. I enclose a sample of the packs to which I refer.

Thank you very much.

    
asked by Francisco Pagan 22.11.2016 в 08:31
source

3 answers

3

Using Library

I found an example very similar to what you want to do using a library, it's in this page .

This page explains how to use a library to use the FontIcon using the files < strong> .ttf and .css of icon packs that can be downloaded from the page for example.

Resources

What it does next is to open the .css in which the icons are defined in the following way:

.ic-quote:before {
    content: "\e000";
}
.ic-users:before {
    content: "\e001";
}
.ic-info:before {
    content: "\e002";
}
.ic-edit:before {
    content: "\e003";
}
.ic-delete:before {
    content: "\e004";
}

These are styles with unicode characters of our icons. We will rewrite them as string resources. For this, in the res/values/ folder, an .xml file will be created that is called, for example, icons.xml

The styles of the .css will be rewritten as string resources remaining as follows:

<?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="ic_quote">&#xe000;</string>
        <string name="ic_users">&#xe001;</string>
        <string name="ic_info">&#xe002;</string>
        <string name="ic_edit">&#xe003;</string>
        <string name="ic_delete">&#xe004;</string>
    </resources>

Note: You now have an automated parser available: FonIcon prepare for Android

The file with the icons will be placed inside the asserts folder:

assets/icons.ttf

Gradle

Add in build.gradle :

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.shamanland:fonticon:0.1.8'
}

Code

There are two important classes: FontIconDrawable and FontIconTypefaceHolder . The first extends the Drawable abstract class, and the second is only for the static storage of the Typeface instance loaded from the .ttf file.

The Typeface file will be initialized in the Application.onCreate () method:

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        FontIconTypefaceHolder.init(getAssets(), "icons.ttf");
    }
}

The use of FontIconDrawable is very simple, for example in Fragment.onCreateView() :

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
    Drawable[] icons = new Drawable[]{
        FontIconDrawable.inflate(getResources(), R.xml.ic_quote),
        FontIconDrawable.inflate(getResources(), R.xml.ic_users),
        FontIconDrawable.inflate(getResources(), R.xml.ic_info),
    };

    // use drawables for any purpose
}

You have a complete example in GitHub

Unused Library

On the other hand I think that this could be done without the use of libraries

Using Font Awesome how it says this StackOverflow response in English

  • Copy fontawesome-webfont.ttf into the asserts folder
  • Find the character entities for the icons you wanted, using this page
  • Create an entry in strings.xml for each icon. For example, for the heart:

    <string name="icon_heart">&#xf004;</string>
    
  • Reference said entry in the view of your xml design

     <Button
         android:id="@+id/like"
         style="?android:attr/buttonStyleSmall"
         ...
         android:text="@string/icon_heart" />
    
  • Load the source in the onCreate method and set it to the appropriate Views:

    Typeface font = Typeface.createFromAsset( getAssets(), "fontawesome-webfont.ttf" );
    ...
    Button button = (Button)findViewById( R.id.like );
    button.setTypeface(font);
    
  • On the other hand I think it could be done using .svg files. But I have not found anything yet, I will continue investigating the subject.

    Some steps to transform the .png to .svg

    The first thing you should do is transform the .png with all the icons to .svg. There are many online tools and services to do this, you can use this for example. Remember to specify the Monochrome option when converting the .png.

        
    answered by 22.11.2016 / 15:35
    source
    1

    In the drawable folder, right click and then go to Show in explorer there only you paste the images and then android studio synchronizes it only Works!

    In the activity you add a ImageView

    //Para acceder a el.
    ImageView image = (ImageView) findViewById(R.id.imageView);
    //Para modificar la imagen.
    image.setImageResource(R.drawable.nublado);
    //Por ejemplo si tienes mas imagens como: soleado, nublado, lluvioso.
    //Es el nombre del .png
    image.setImageResource(R,drawable.soleado);
    
        
    answered by 22.11.2016 в 09:07
    0

    1.- I understand that in Android in the folder of your project called drawable: It is where the image files (JPG or PNG) and image descriptors in XML are stored. You can put the images that you want there, be it from bookstores or not. In this link you can find more information.

    2.- Another valid option could be that you use an image library that lets you customize them, so that you can insert your own images as if they were a library.

        
    answered by 22.11.2016 в 09:11