Android Open Source - FisgoDroid Notifications Indicator Drawable






From Project

Back to project page FisgoDroid.

License

The source code is released under:

The smiley icons bundled with this application belong to Meneame.NET and are licensed under the Creative Commons by-sa 3.0 license. For more information, please visit http://creativecommons.org/licens...

If you think the Android project FisgoDroid 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 net.meneame.fisgodroid.notifications;
/*from  w ww . jav  a 2s . c om*/
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.util.TypedValue;

public class NotificationsIndicatorDrawable extends Drawable
{
    private int mNumNotifs = 0;
    private Drawable mDefaultDrawable;
    private Paint mBorderPaint;
    private Paint mCirclePaint;
    private Paint mTextPaint;
    private Rect mTextBounds;

    public NotificationsIndicatorDrawable(int borderColor, int backgroundColor, int textColor, Drawable defaultDrawable)
    {
        mDefaultDrawable = defaultDrawable.getConstantState().newDrawable();

        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(backgroundColor);
        mCirclePaint.setStyle(Style.FILL);

        mBorderPaint = new Paint();
        mBorderPaint.setAntiAlias(true);
        mBorderPaint.setColor(borderColor);
        mBorderPaint.setStyle(Style.STROKE);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(textColor);
        mTextPaint.setStyle(Style.FILL);
        mTextPaint.setTextAlign(Align.CENTER);
        mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);

        mTextBounds = new Rect();
    }

    public void setNotificationCount(int count)
    {
        mNumNotifs = count;
    }

    public int getNotificationCount()
    {
        return mNumNotifs;
    }

    @Override
    public void draw(Canvas canvas)
    {
        Rect bounds = getBounds();
        if ( mNumNotifs == 0 )
        {
            mDefaultDrawable.draw(canvas);
        }
        else
        {
            final float width = bounds.width();
            canvas.drawCircle(bounds.exactCenterX(), bounds.exactCenterY(), width * 0.5f, mCirclePaint);
            canvas.drawCircle(bounds.exactCenterX(), bounds.exactCenterY(), width * 0.5f, mBorderPaint);

            // Get the target width from the square that fits the circle
            final float targetWidth = (float) Math.sqrt(width * width * 0.5f);

            final String text = "" + mNumNotifs;
            setTextSizeToFit(text, mTextPaint, targetWidth, targetWidth);
            mTextPaint.getTextBounds(text, 0, text.length(), mTextBounds);
            canvas.drawText(text, bounds.exactCenterX(), bounds.exactCenterY() + (mTextBounds.height() / 2), mTextPaint);
        }
    }

    private static void setTextSizeToFit(String text, Paint textPaint, float targetWidth, float targetHeight)
    {
        float sizeHigh = 250.0f;
        float sizeLow = 2.0f;
        float current;
        final Rect bounds = new Rect();

        textPaint.getTextBounds(text, 0, text.length(), bounds);

        DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
        final float maxDiff = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, metrics);

        // Specialize the algorithm depending on the biggest dimension
        if ( bounds.width() > bounds.height() )
        {
            float width;
            do
            {
                if ( Math.abs(sizeHigh - sizeLow) < 2 )
                {
                    break;
                }
                
                current = (float) Math.floor((sizeLow + sizeHigh) * 0.5f);
                textPaint.setTextSize(current);
                width = textPaint.measureText(text);

                if ( Math.abs(targetWidth - width) < maxDiff )
                {
                    break;
                }

                if ( width > targetWidth )
                {
                    sizeHigh = current;
                }
                else
                {
                    sizeLow = current;
                }
            }
            while (true);
        }
        else
        {
            float height;
            do
            {
                if ( Math.abs(sizeHigh - sizeLow) < 2 )
                {
                    break;
                }
                
                current = (float) Math.floor((sizeLow + sizeHigh) * 0.5f);
                textPaint.setTextSize(current);
                textPaint.getTextBounds(text, 0, text.length(), bounds);
                height = bounds.height();

                if ( Math.abs(targetHeight - height) < maxDiff )
                {
                    break;
                }

                if ( height > targetHeight )
                {
                    sizeHigh = current;
                }
                else
                {
                    sizeLow = current;
                }
            }
            while (true);
        }
    }

    @Override
    public int getOpacity()
    {
        return PixelFormat.TRANSPARENT;
    }

    @Override
    public void setAlpha(int arg0)
    {
        mDefaultDrawable.setAlpha(arg0);
        mCirclePaint.setAlpha(arg0);
        mTextPaint.setAlpha(arg0);
    }

    @Override
    public void setColorFilter(ColorFilter arg0)
    {
        mDefaultDrawable.setColorFilter(arg0);
        mCirclePaint.setColorFilter(arg0);
        mTextPaint.setColorFilter(arg0);
    }

    @Override
    public int getIntrinsicWidth()
    {
        return mDefaultDrawable.getIntrinsicWidth();
    }

    @Override
    public int getIntrinsicHeight()
    {
        return mDefaultDrawable.getIntrinsicHeight();
    }

    @Override
    public void setBounds(Rect bounds)
    {
        super.setBounds(bounds);
        mDefaultDrawable.setBounds(bounds);
    }

    @Override
    public void setBounds(int left, int top, int right, int bottom)
    {
        super.setBounds(left, top, right, bottom);
        mDefaultDrawable.setBounds(left, top, right, bottom);
    }
}




Java Source Code List

jp.tomorrowkey.android.GifDecoder.java
net.meneame.fisgodroid.AnimatedGifDrawable.java
net.meneame.fisgodroid.ChatActivity.java
net.meneame.fisgodroid.ChatBaseView.java
net.meneame.fisgodroid.ChatBubbleView.java
net.meneame.fisgodroid.ChatLineView.java
net.meneame.fisgodroid.ChatMessage.java
net.meneame.fisgodroid.ChatType.java
net.meneame.fisgodroid.DynamicTextView.java
net.meneame.fisgodroid.FisgoScheduler.java
net.meneame.fisgodroid.FisgoService.java
net.meneame.fisgodroid.FisgodroidApplication.java
net.meneame.fisgodroid.FriendshipStatus.java
net.meneame.fisgodroid.HttpService.java
net.meneame.fisgodroid.IHttpService.java
net.meneame.fisgodroid.ImageUpload.java
net.meneame.fisgodroid.LogSaver.java
net.meneame.fisgodroid.LoginActivity.java
net.meneame.fisgodroid.LoginStatus.java
net.meneame.fisgodroid.Notifications.java
net.meneame.fisgodroid.ProfileActivity.java
net.meneame.fisgodroid.SettingsActivity.java
net.meneame.fisgodroid.SmileyPickerView.java
net.meneame.fisgodroid.SmileySpan.java
net.meneame.fisgodroid.Smiley.java
net.meneame.fisgodroid.Smileys.java
net.meneame.fisgodroid.ThreeStateChecboxHackView.java
net.meneame.fisgodroid.UserProfileFetcher.java
net.meneame.fisgodroid.UserProfile.java
net.meneame.fisgodroid.adapters.BubblesChatAdapter.java
net.meneame.fisgodroid.adapters.ChatMessageAdapter.java
net.meneame.fisgodroid.adapters.LegacyChatAdapter.java
net.meneame.fisgodroid.notifications.ElementAdapter.java
net.meneame.fisgodroid.notifications.NotificationElement.java
net.meneame.fisgodroid.notifications.NotificationView.java
net.meneame.fisgodroid.notifications.NotificationsIndicatorDrawable.java
net.meneame.fisgodroid.notifications.NotificationsLayout.java
net.meneame.fisgodroid.notifications.NotificationsPoller.java