Android Open Source - dClock U I Helper






From Project

Back to project page dClock.

License

The source code is released under:

Copyright (c) 2012 Yaroslav Tatarenko All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are me...

If you think the Android project dClock 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

package com.dclock.utils;
/*w w w.  ja  v a2  s . c o m*/
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.lang.ref.SoftReference;
import java.util.Hashtable;

public final class UIHelper
{
        private static final String TagIdentifier = "UIHelper";
        private static final Hashtable<String, SoftReference<Typeface>> fontCache = new Hashtable<String, SoftReference<Typeface>>();

        public static void setCustomFont(View view, Context ctx, AttributeSet attrs, int[] attributeSets, int fontId)
        {
            TypedArray a = ctx.obtainStyledAttributes(attrs, attributeSets);
            String customFont = a.getString(fontId);
            setCustomFont(view, ctx, customFont);
            a.recycle();
        }

        private static boolean setCustomFont(View view, Context ctx, String asset)
        {
            if (asset == null || asset.isEmpty())
            {
                return false;
            }

            Typeface tf = null;
            try {
                tf = getFont(ctx, asset);
                if (view instanceof TextView)
                {
                    ((TextView) view).setTypeface(tf);
                } else
                {
                    ((Button) view).setTypeface(tf);
                }
            } catch (Exception e)
            {
                Log.e(TagIdentifier, "Could not get typeface: " + asset, e);
                return false;
            }

            return true;
        }

        public static Typeface getFont(Context c, String name)
        {
            synchronized (fontCache)
            {
                if (fontCache.get(name) != null)
                {
                    SoftReference<Typeface> ref = fontCache.get(name);
                    if (ref.get() != null)
                    {
                        return ref.get();
                    }
                }

                Typeface typeface = Typeface.createFromAsset(
                        c.getAssets(),
                        "fonts/" + name
                );
                fontCache.put(name, new SoftReference<Typeface>(typeface));

                return typeface;
            }
        }
}




Java Source Code List

com.dclock.ClockActivity.java
com.dclock.ClockService.java
com.dclock.ClockSettingActivity.java
com.dclock.ClockWidget.java
com.dclock.utils.UIHelper.java
com.dclock.views.FontableTextView.java