Android Open Source - Android-Iconics Iconics Drawable Old






From Project

Back to project page Android-Iconics.

License

The source code is released under:

Apache License

If you think the Android project Android-Iconics listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Copyright 2013 Joan Zapata//w ww  .  java 2  s .co  m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mikepenz.iconics;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;

import com.mikepenz.iconics.typeface.IIcon;
import com.mikepenz.iconics.typeface.ITypeface;
import com.mikepenz.iconics.utils.Utils;

/**
 * Embed an icon into a Drawable that can be used as TextView icons, or ActionBar icons.
 * new IconDrawable(context, IconValue.icon_star)
 * .colorRes(R.color.white)
 * .actionBarSize();
 * If you don't set the size of the drawable, it will use the size
 * that is given to him. Note that in an ActionBar, if you don't
 * set the size explicitly it uses 0, so please use actionBarSize().
 */
@Deprecated
public class IconicsDrawableOld extends Drawable {

    public static final int ANDROID_ACTIONBAR_ICON_SIZE_DP = 24;

    private final Context context;

    private final IIcon icon;

    private TextPaint paint;

    private int size = -1;

    private int alpha = 255;


    /**
     * Create an IconDrawable.
     * Just give it the icon-identifier
     *
     * @param context Your activity or application context.
     * @param icon    The icon identifier without icon- (font must be registered)
     */
    public IconicsDrawableOld(Context context, String icon) {
        this.context = context;

        ITypeface font = Iconics.findFont(icon.substring(0, 3));
        icon = icon.replace("-", "_");
        this.icon = font.getIcon(icon);

        paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        paint.setTypeface(font.getTypeface(context));
        paint.setStyle(Paint.Style.STROKE);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setUnderlineText(false);
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
    }

    /**
     * Create an IconDrawable.
     *
     * @param context Your activity or application context.
     * @param icon    The icon you want this drawable to display.
     */
    public IconicsDrawableOld(Context context, IIcon icon) {
        this.context = context;
        this.icon = icon;

        //Get font by icon
        ITypeface font = Iconics.findFont(icon.getName().substring(0, 3));

        paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        paint.setTypeface(font.getTypeface(context));
        paint.setStyle(Paint.Style.STROKE);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setUnderlineText(false);
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
    }

    /**
     * Create an IconDrawable.
     *
     * @param font    The font to use for this drawable
     * @param context Your activity or application context.
     * @param icon    The icon you want this drawable to display.
     */
    public IconicsDrawableOld(Context context, ITypeface font, IIcon icon) {
        this.context = context;
        this.icon = icon;
        paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        paint.setTypeface(font.getTypeface(context));
        paint.setStyle(Paint.Style.STROKE);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setUnderlineText(false);
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
    }

    /**
     * Set the size of this icon to the standard Android ActionBar.
     *
     * @return The current IconDrawable for chaining.
     */
    public IconicsDrawableOld actionBarSize() {
        return sizeDp(ANDROID_ACTIONBAR_ICON_SIZE_DP);
    }

    /**
     * Set the size of the drawable.
     *
     * @param dimenRes The dimension resource.
     * @return The current IconDrawable for chaining.
     */
    public IconicsDrawableOld sizeRes(int dimenRes) {
        return sizePx(context.getResources().getDimensionPixelSize(dimenRes));
    }

    /**
     * Set the size of the drawable.
     *
     * @param size The size in density-independent pixels (dp).
     * @return The current IconDrawable for chaining.
     */
    public IconicsDrawableOld sizeDp(int size) {
        return sizePx(Utils.convertDpToPx(context, size));
    }

    /**
     * Set the size of the drawable.
     *
     * @param size The size in pixels (px).
     * @return The current IconDrawable for chaining.
     */
    public IconicsDrawableOld sizePx(int size) {
        this.size = size;
        setBounds(0, 0, size, size);
        invalidateSelf();
        return this;
    }

    /**
     * Set the color of the drawable.
     *
     * @param color The color, usually from android.graphics.Color or 0xFF012345.
     * @return The current IconDrawable for chaining.
     */
    public IconicsDrawableOld color(int color) {
        paint.setColor(color);
        invalidateSelf();
        return this;
    }

    /**
     * Set the color of the drawable.
     *
     * @param colorRes The color resource, from your R file.
     * @return The current IconDrawable for chaining.
     */
    public IconicsDrawableOld colorRes(int colorRes) {
        paint.setColor(context.getResources().getColor(colorRes));
        invalidateSelf();
        return this;
    }

    /**
     * Set the alpha of this drawable.
     *
     * @param alpha The alpha, between 0 (transparent) and 255 (opaque).
     * @return The current IconDrawable for chaining.
     */
    public IconicsDrawableOld alpha(int alpha) {
        setAlpha(alpha);
        invalidateSelf();
        return this;
    }

    @Override
    public int getIntrinsicHeight() {
        return size;
    }

    @Override
    public int getIntrinsicWidth() {
        return size;
    }

    @Override
    public void draw(Canvas canvas) {
        paint.setTextSize(getBounds().height());
        Rect textBounds = new Rect();
        String textValue = String.valueOf(icon.getCharacter());
        paint.getTextBounds(textValue, 0, 1, textBounds);
        float textBottom = (getBounds().height() - textBounds.height()) / 2f + textBounds.height() - textBounds.bottom;
        canvas.drawText(textValue, getBounds().width() / 2f, textBottom, paint);
    }

    @Override
    public boolean isStateful() {
        return true;
    }

    @Override
    public boolean setState(int[] stateSet) {
        int oldValue = paint.getAlpha();
        int newValue = Utils.isEnabled(stateSet) ? alpha : alpha / 2;
        paint.setAlpha(newValue);
        return oldValue != newValue;
    }

    @Override
    public void setAlpha(int alpha) {
        this.alpha = alpha;
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        paint.setColorFilter(cf);
    }

    @Override
    public void clearColorFilter() {
        paint.setColorFilter(null);
    }

    @Override
    public int getOpacity() {
        return this.alpha;
    }

    /**
     * Sets paint style.
     *
     * @param style to be applied
     */
    public void setStyle(Paint.Style style) {
        paint.setStyle(style);
    }


    /**
     * Creates a BitMap to use in Widgets or anywhere else
     *
     * @return bitmap to set
     */
    public Bitmap toBitmap() {
        if (size == -1) {
            this.actionBarSize();
        }

        final Bitmap bitmap = Bitmap.createBitmap(this.getIntrinsicWidth(), this.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);

        this.setStyle(Paint.Style.FILL);

        final Canvas canvas = new Canvas(bitmap);
        this.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        this.draw(canvas);

        return bitmap;
    }
}




Java Source Code List

android.view.IconicsButton.java
android.view.IconicsTextView.java
com.mikepenz.google_material_typeface_library.GoogleMaterial.java
com.mikepenz.iconics.IconicsDrawableOld.java
com.mikepenz.iconics.IconicsDrawable.java
com.mikepenz.iconics.Iconics.java
com.mikepenz.iconics.sample.CustomApplication.java
com.mikepenz.iconics.sample.MainActivity.java
com.mikepenz.iconics.sample.PlaygroundActivity.java
com.mikepenz.iconics.sample.adapter.IconAdapter.java
com.mikepenz.iconics.sample.typeface.CustomFont.java
com.mikepenz.iconics.typeface.FontAwesome.java
com.mikepenz.iconics.typeface.IIcon.java
com.mikepenz.iconics.typeface.ITypeface.java
com.mikepenz.iconics.utils.IconicsTypefaceSpan.java
com.mikepenz.iconics.utils.Utils.java
com.mikepenz.meteocons_typeface_library.Meteoconcs.java