Example usage for android.graphics Typeface defaultFromStyle

List of usage examples for android.graphics Typeface defaultFromStyle

Introduction

In this page you can find the example usage for android.graphics Typeface defaultFromStyle.

Prototype

public static Typeface defaultFromStyle(@Style int style) 

Source Link

Document

Returns one of the default typeface objects, based on the specified style

Usage

From source file:Main.java

public static Bitmap drawTextToBitmap(Context context, int resId, String text) {

    Resources resources = context.getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = BitmapFactory.decodeResource(resources, resId);

    Bitmap.Config bitmapConfig = bitmap.getConfig();
    if (bitmapConfig == null)
        bitmapConfig = Bitmap.Config.ARGB_8888;
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(context.getResources().getColor(android.R.color.white));
    paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    paint.setTextSize((int) (12 * scale));

    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width()) / 4;
    int y = (bitmap.getHeight() + bounds.height()) / 5;

    canvas.drawText(text, x * scale, y * scale, paint);

    return bitmap;
}

From source file:com.taobao.weex.utils.TypefaceUtil.java

public static void applyFontStyle(Paint paint, int style, int weight, String family) {
    int oldStyle;
    Typeface typeface = paint.getTypeface();
    if (typeface == null) {
        oldStyle = 0;/*from  ww w.j  a  v  a  2s.  com*/
    } else {
        oldStyle = typeface.getStyle();
    }

    int want = 0;
    if ((weight == Typeface.BOLD) || ((oldStyle & Typeface.BOLD) != 0 && weight == WXStyle.UNSET)) {
        want |= Typeface.BOLD;
    }

    if ((style == Typeface.ITALIC) || ((oldStyle & Typeface.ITALIC) != 0 && style == WXStyle.UNSET)) {
        want |= Typeface.ITALIC;
    }

    if (family != null) {
        typeface = getOrCreateTypeface(family, want);
    }

    if (typeface != null) {
        paint.setTypeface(Typeface.create(typeface, want));
    } else {
        paint.setTypeface(Typeface.defaultFromStyle(want));
    }
}

From source file:com.cls.sugutomo.map.ClusterOP.java

public ClusterOP(Resources resources) {
    baseBitmaps = new Bitmap[res.length];
    for (int i = 0; i < res.length; i++) {
        baseBitmaps[i] = BitmapFactory.decodeResource(resources, res[i]);
    }/*from   w w  w .  j ava2  s. c o m*/
    paint.setColor(Color.WHITE);
    paint.setTextAlign(Align.CENTER);
    paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    paint.setTextSize(resources.getDimension(R.dimen.text_size));
}

From source file:com.QuarkLabs.BTCeClient.adapters.OrdersBookAdapter.java

@SuppressLint("InflateParams")
@Override//from w  ww  .j  a va 2s .  c  o  m
public View getView(int position, View convertView, ViewGroup parent) {
    View v;
    if (convertView == null) {
        v = mInflater.inflate(R.layout.ordersbook_item, parent, false);
    } else {
        v = convertView;
    }
    JSONArray value = mData.optJSONArray(position);
    double price = value.optDouble(0);
    double volume = value.optDouble(1);
    TextView text1 = (TextView) v.findViewById(R.id.orderBookPrice);
    TextView text2 = (TextView) v.findViewById(R.id.ordersBookVolume);
    text1.setText(String.valueOf(price));
    text2.setText(String.valueOf(volume));
    if (volume == mMaxValue) {
        text1.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        text2.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    } else {
        text1.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        text2.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
    }

    return v;
}

From source file:edu.sfsu.cs.orange.ocr.language.TranslateAsyncTask.java

@Override
protected synchronized void onPostExecute(Boolean result) {
    super.onPostExecute(result);

    if (result) {
        //Log.i(TAG, "SUCCESS");
        if (targetLanguageTextView != null) {
            targetLanguageTextView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL), Typeface.NORMAL);
        }/*from  w  w w  . j a va 2 s . c om*/
        textView.setText(translatedText);
        textView.setVisibility(View.VISIBLE);
        textView.setTextColor(activity.getResources().getColor(R.color.translation_text));

        // Crudely scale betweeen 22 and 32 -- bigger font for shorter text
        int scaledSize = Math.max(22, 32 - translatedText.length() / 4);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, scaledSize);

    } else {
        Log.e(TAG, "FAILURE");
        targetLanguageTextView.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC), Typeface.ITALIC);
        targetLanguageTextView.setText("Unavailable");

    }

    // Turn off the indeterminate progress indicator
    if (progressView != null) {
        progressView.setVisibility(View.GONE);
    }
}

From source file:widgets.Graphical_Trigger.java

public Graphical_Trigger(tracerengine Trac, Activity context, String address, String name, int id, int dev_id,
        String stat_key, String url, String usage, String parameters, String model_id, int widgetSize,
        int session_type, int place_id, String place_type, SharedPreferences params) throws JSONException {
    super(context, Trac, id, name, "", usage, widgetSize, session_type, place_id, place_type, mytag, container);
    this.address = address;
    this.Tracer = Trac;
    this.url = url;
    this.id = id;
    this.context = context;
    this.usage = usage;
    this.myself = this;
    this.session_type = session_type;
    this.dev_id = dev_id;
    this.place_id = place_id;
    this.place_type = place_type;
    mytag = "Graphical_Trigger(" + dev_id + ")";
    this.params = params;
    login = params.getString("http_auth_username", null);
    password = params.getString("http_auth_password", null);

    //get parameters
    JSONObject jparam = new JSONObject(parameters.replaceAll("&quot;", "\""));

    if (jparam != null) {
        try {//from w  w  w . j av  a2s .  c  o  m
            command = jparam.getString("command");
            usable = true;
        } catch (Exception e) {
            usable = false;
            e.printStackTrace();
        }
    }

    String[] model = model_id.split("\\.");
    type = model[0];

    //first seekbar on/off
    trigger = new Graphical_Trigger_Button(context);
    trigger.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
    trigger.setOnClickListener(this);

    //unusable
    unusable = new TextView(context);
    unusable.setText(R.string.unusable);
    unusable.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
    unusable.setTextColor(Color.BLACK);
    unusable.setTextSize(14);
    unusable.setPadding(0, 0, 15, 0);

    if (usable == true) {
        LL_featurePan.addView(trigger);
    } else {
        LL_featurePan.addView(unusable);
    }

}

From source file:com.nma.util.sdcardtrac.SearchableActivity.java

public static int getTextHeight(Context context, CharSequence text, int textSize, int deviceWidth) { //, Typeface typeface,int padding) {
    TextView textView = new TextView(context);
    textView.setPadding(0, 0, 0, 0);//w  w w .  j a  v a2  s  .  c  om
    textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
    textView.setText(text, TextView.BufferType.SPANNABLE);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(deviceWidth, View.MeasureSpec.AT_MOST);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(widthMeasureSpec, heightMeasureSpec);
    return textView.getMeasuredHeight();
}

From source file:com.raspi.chatapp.util.Notification.java

private Bitmap getLargeIcon(int bgColor, char letter, float width, boolean round) {
    Bitmap b = Bitmap.createBitmap((int) width, (int) width, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);

    RectF mInnerRectF = new RectF();
    mInnerRectF.set(0, 0, width, width);
    mInnerRectF.offset(0, 0);/*from w  ww  . ja  va  2 s  . co m*/

    Paint mBgPaint = new Paint();
    mBgPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    mBgPaint.setStyle(Paint.Style.FILL);
    mBgPaint.setColor(bgColor);

    TextPaint mTitleTextPaint = new TextPaint();
    mTitleTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    mTitleTextPaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    mTitleTextPaint.setTextAlign(Paint.Align.CENTER);
    mTitleTextPaint.setLinearText(true);
    mTitleTextPaint.setColor(Color.WHITE);
    mTitleTextPaint.setTextSize(width * 0.8f);

    float centerX = mInnerRectF.centerX();
    float centerY = mInnerRectF.centerY();

    int xPos = (int) centerX;
    int yPos = (int) (centerY - (mTitleTextPaint.descent() + mTitleTextPaint.ascent()) / 2);

    if (round)
        c.drawOval(mInnerRectF, mBgPaint);
    else
        c.drawRect(mInnerRectF, mBgPaint);
    c.drawText(String.valueOf(letter), xPos, yPos, mTitleTextPaint);

    return b;
}

From source file:android.support.v7.widget.SwitchCompat.java

/**
 * Sets the typeface and style in which the text should be displayed on the
 * switch, and turns on the fake bold and italic bits in the Paint if the
 * Typeface that you provided does not have all the bits in the
 * style that you specified.//  ww  w . java2  s  .  c o m
 */
public void setSwitchTypeface(Typeface tf, int style) {
    if (style > 0) {
        if (tf == null) {
            tf = Typeface.defaultFromStyle(style);
        } else {
            tf = Typeface.create(tf, style);
        }

        setSwitchTypeface(tf);
        // now compute what (if any) algorithmic styling is needed
        int typefaceStyle = tf != null ? tf.getStyle() : 0;
        int need = style & ~typefaceStyle;
        mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0);
        mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0);
    } else {
        mTextPaint.setFakeBoldText(false);
        mTextPaint.setTextSkewX(0);
        setSwitchTypeface(tf);
    }
}

From source file:com.facebook.FacebookButtonBase.java

private void parseTextAttributes(final Context context, final AttributeSet attrs, final int defStyleAttr,
        final int defStyleRes) {
    final int colorResources[] = { android.R.attr.textColor, };
    final TypedArray colorAttrs = context.getTheme().obtainStyledAttributes(attrs, colorResources, defStyleAttr,
            defStyleRes);//w w  w.ja  va  2  s. co  m
    try {
        setTextColor(colorAttrs.getColor(0, Color.WHITE));
    } finally {
        colorAttrs.recycle();
    }
    final int gravityResources[] = { android.R.attr.gravity, };
    final TypedArray gravityAttrs = context.getTheme().obtainStyledAttributes(attrs, gravityResources,
            defStyleAttr, defStyleRes);
    try {
        setGravity(gravityAttrs.getInt(0, Gravity.CENTER));
    } finally {
        gravityAttrs.recycle();
    }
    final int attrsResources[] = { android.R.attr.textSize, android.R.attr.textStyle, android.R.attr.text, };
    final TypedArray a = context.getTheme().obtainStyledAttributes(attrs, attrsResources, defStyleAttr,
            defStyleRes);
    try {
        setTextSize(TypedValue.COMPLEX_UNIT_PX, a.getDimensionPixelSize(0, 0));
        setTypeface(Typeface.defaultFromStyle(a.getInt(1, Typeface.BOLD)));
        setText(a.getString(2));
    } finally {
        a.recycle();
    }
}