Android Open Source - Genius-Android Genius Text View






From Project

Back to project page Genius-Android.

License

The source code is released under:

Apache License

If you think the Android project Genius-Android 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 (C) 2014 Qiujuer <qiujuer@live.cn>
 * WebSite http://www.qiujuer.net/*  ww  w. j a  v a  2s.  c  om*/
 * Created 12/30/2014
 * Changed 01/14/2015
 * Version 2.0.0
 *
 * 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 net.qiujuer.genius.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.widget.TextView;

import net.qiujuer.genius.Attributes;
import net.qiujuer.genius.GeniusUI;
import net.qiujuer.genius.R;

/**
 * Created by Qiujuer
 * on 2014/12/30.
 */
public class GeniusTextView extends TextView implements Attributes.AttributeChangeListener {
    private int mTextColor = 2;
    private int mBackgroundColor = Attributes.INVALID;
    private int mCustomBackgroundColor = Attributes.INVALID;
    private Attributes mAttributes;

    public GeniusTextView(Context context) {
        super(context);
        init(null);
    }

    public GeniusTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public GeniusTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    @SuppressWarnings("deprecation")
    private void init(AttributeSet attrs) {

        if (mAttributes == null)
            mAttributes = new Attributes(this, getResources());

        if (attrs != null) {

            // Set if has own attrs
            mAttributes.initHasOwnAttrs(getContext(), attrs);

            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.GeniusTextView);

            // Getting common attributes
            int customTheme = a.getResourceId(R.styleable.GeniusTextView_g_theme, Attributes.DEFAULT_THEME);
            mAttributes.setThemeSilent(customTheme, getResources());

            mAttributes.setFontFamily(a.getString(R.styleable.GeniusTextView_g_fontFamily));
            mAttributes.setFontWeight(a.getString(R.styleable.GeniusTextView_g_fontWeight));
            mAttributes.setFontExtension(a.getString(R.styleable.GeniusTextView_g_fontExtension));

            mAttributes.setRadius(a.getDimensionPixelSize(R.styleable.GeniusTextView_g_cornerRadius, Attributes.DEFAULT_RADIUS));
            mAttributes.setBorderWidth(a.getDimensionPixelSize(R.styleable.GeniusTextView_g_borderWidth, Attributes.DEFAULT_BORDER_WIDTH));

            // Getting view specific attributes
            mTextColor = a.getInt(R.styleable.GeniusTextView_g_textColor, mTextColor);
            mBackgroundColor = a.getInt(R.styleable.GeniusTextView_g_backgroundColor, mBackgroundColor);
            mCustomBackgroundColor = a.getInt(R.styleable.GeniusTextView_g_customBackgroundColor, mCustomBackgroundColor);

            a.recycle();
        }

        if (!mAttributes.isHasOwnBackground()) {
            GradientDrawable gradientDrawable = new GradientDrawable();
            if (mBackgroundColor != Attributes.INVALID) {
                gradientDrawable.setColor(mAttributes.getColor(mBackgroundColor));
            } else if (mCustomBackgroundColor != Attributes.INVALID) {
                gradientDrawable.setColor(mCustomBackgroundColor);
            } else {
                gradientDrawable.setColor(Color.TRANSPARENT);
            }
            gradientDrawable.setCornerRadius(mAttributes.getRadius());
            gradientDrawable.setStroke(mAttributes.getBorderWidth(), mAttributes.getColor(mTextColor));
            setBackgroundDrawable(gradientDrawable);
        }

        // Setting the text color only if there is no android:textColor attribute used
        if (!mAttributes.isHasOwnTextColor()) setTextColor(mAttributes.getColor(mTextColor));

        // Check for IDE preview render
        if (!this.isInEditMode()) {
            Typeface typeface = GeniusUI.getFont(getContext(), mAttributes);
            if (typeface != null) setTypeface(typeface);
        }
    }

    @Override
    public Attributes getAttributes() {
        return mAttributes;
    }

    @Override
    public void onThemeChange() {
        init(null);
    }
}




Java Source Code List

net.qiujuer.genius.ApplicationTest.java
net.qiujuer.genius.Attributes.java
net.qiujuer.genius.GeniusUI.java
net.qiujuer.genius.Genius.java
net.qiujuer.genius.animation.TouchEffectAnimator.java
net.qiujuer.genius.animation.TouchEffect.java
net.qiujuer.genius.app.BlurKit.java
net.qiujuer.genius.app.BlurNative.java
net.qiujuer.genius.app.UIKitHandlerPoster.java
net.qiujuer.genius.app.UIKitSyncPost.java
net.qiujuer.genius.app.UIKit.java
net.qiujuer.genius.command.CommandExecutor.java
net.qiujuer.genius.command.CommandService.java
net.qiujuer.genius.command.Command.java
net.qiujuer.genius.nettool.DnsResolve.java
net.qiujuer.genius.nettool.NetModel.java
net.qiujuer.genius.nettool.Ping.java
net.qiujuer.genius.nettool.Telnet.java
net.qiujuer.genius.nettool.TraceRouteContainer.java
net.qiujuer.genius.nettool.TraceRouteThread.java
net.qiujuer.genius.nettool.TraceRoute.java
net.qiujuer.genius.sample.ApplicationTest.java
net.qiujuer.genius.sample.CaseActivity.java
net.qiujuer.genius.sample.MainActivity.java
net.qiujuer.genius.sample.TwoActivity.java
net.qiujuer.genius.util.FixedList.java
net.qiujuer.genius.util.GeniusException.java
net.qiujuer.genius.util.HashUtils.java
net.qiujuer.genius.util.LogWriter.java
net.qiujuer.genius.util.Log.java
net.qiujuer.genius.util.Tools.java
net.qiujuer.genius.widget.GeniusButton.java
net.qiujuer.genius.widget.GeniusCheckBox.java
net.qiujuer.genius.widget.GeniusTextView.java