Example usage for android.widget TextView buildDrawingCache

List of usage examples for android.widget TextView buildDrawingCache

Introduction

In this page you can find the example usage for android.widget TextView buildDrawingCache.

Prototype

@Deprecated
public void buildDrawingCache(boolean autoScale) 

Source Link

Document

Forces the drawing cache to be built if the drawing cache is invalid.

If you call #buildDrawingCache() manually without calling #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true) , you should cleanup the cache by calling #destroyDrawingCache() afterwards.

Note about auto scaling in compatibility mode: When auto scaling is not enabled, this method will create a bitmap of the same size as this view.

Usage

From source file:com.gsbabil.antitaintdroid.UtilityFunctions.java

public int captureBitmapCache(String in) {
    TextView tv = (TextView) MyApp.context.findViewById(R.id.ocrTextview);
    String tvText = tv.getText().toString();
    float tvTextSize = tv.getTextSize();
    int tvColor = tv.getCurrentTextColor();
    Bitmap bitmap = null;//from   www  .  j av  a2 s .co  m

    tv.setTextSize(36);
    tv.setTextColor(Color.CYAN);
    tv.setTypeface(Typeface.SANS_SERIF);

    tv.setText(in);

    while (bitmap == null) {
        // http://stackoverflow.com/questions/2339429/android-view-getdrawingcache-returns-null-only-null
        tv.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());

        tv.setDrawingCacheEnabled(true);
        tv.buildDrawingCache(true);
        bitmap = Bitmap.createBitmap(tv.getDrawingCache());
        tv.destroyDrawingCache();
        tv.setDrawingCacheEnabled(false);
    }

    FileOutputStream fos = null;
    int res = -1;
    try {
        fos = new FileOutputStream(currentDirectory() + "/files/" + MyApp.SCREENSHOT_FILENAME);
        if (fos != null) {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
            fos.close();
            res = 0;
        }
    } catch (Throwable e) {
        Log.i(MyApp.TAG, e.getMessage().toString());
        res = -1;
    }

    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, tvTextSize);
    tv.setTypeface(Typeface.MONOSPACE);
    tv.setText(tvText);
    tv.setTextColor(tvColor);
    return res;
}

From source file:com.mydatingapp.ui.base.SkBaseInnerActivity.java

public void setActionBarLogoCounter(int count) {
    ImageView logo = (ImageView) findViewById(android.R.id.home);

    if (count < 1) {
        currentLogoCounter = 0;//ww w  . jav  a  2s  .c o m
        logo.setVisibility(View.GONE);
        return;
    } else {
        logo.setVisibility(View.VISIBLE);
    }

    currentLogoCounter = count;

    TextView v = new TextView(getApp());
    v.setText(new Integer(count).toString());
    v.setBackgroundResource(R.drawable.sidebar_menu_counterbg);
    v.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
    v.setDrawingCacheEnabled(true);
    v.setTextColor(Color.WHITE);
    v.setPadding(SKDimensions.convertDpToPixel(6, getApp()), 0, SKDimensions.convertDpToPixel(6, getApp()), 0);

    ActionBar.LayoutParams paramsExample = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.WRAP_CONTENT, 1);
    paramsExample.setMargins(0, 0, 0, 0);
    v.setHeight(SKDimensions.convertDpToPixel(20, getApp()));
    v.setLayoutParams(paramsExample);

    // this is the important code :)
    // Without it the view will have a dimension of 0,0 and the bitmap will be null
    v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

    v.buildDrawingCache(true);
    Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);

    logo.setImageDrawable(new BitmapDrawable(getApp().getResources(), b));
    logo.setPadding(SKDimensions.convertDpToPixel(3, getApp()), 0, 0, 0);
}