Java Code Examples for android.widget.TextView#draw()

The following examples show how to use android.widget.TextView#draw() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextResize.java    From animation-samples with Apache License 2.0 6 votes vote down vote up
private static Bitmap captureTextBitmap(TextView textView) {
    Drawable background = textView.getBackground();
    textView.setBackground(null);
    int width = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
    int height = textView.getHeight() - textView.getPaddingTop() - textView.getPaddingBottom();
    if (width == 0 || height == 0) {
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.translate(-textView.getPaddingLeft(), -textView.getPaddingTop());
    textView.draw(canvas);
    textView.setBackground(background);
    return bitmap;
}
 
Example 2
Source File: DefaultIconGenerator.java    From google-maps-clustering with Apache License 2.0 6 votes vote down vote up
@NonNull
private BitmapDescriptor createClusterIcon(int clusterBucket) {
    @SuppressLint("InflateParams")
    TextView clusterIconView = (TextView) LayoutInflater.from(mContext)
            .inflate(R.layout.map_cluster_icon, null);
    clusterIconView.setBackground(createClusterBackground());
    clusterIconView.setTextColor(mIconStyle.getClusterTextColor());
    clusterIconView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
            mIconStyle.getClusterTextSize());

    clusterIconView.setText(getClusterIconText(clusterBucket));

    clusterIconView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    clusterIconView.layout(0, 0, clusterIconView.getMeasuredWidth(),
            clusterIconView.getMeasuredHeight());

    Bitmap iconBitmap = Bitmap.createBitmap(clusterIconView.getMeasuredWidth(),
            clusterIconView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(iconBitmap);
    clusterIconView.draw(canvas);

    return BitmapDescriptorFactory.fromBitmap(iconBitmap);
}
 
Example 3
Source File: TextResize.java    From atlas with Apache License 2.0 5 votes vote down vote up
private static Bitmap captureTextBitmap(TextView textView) {
    Drawable background = textView.getBackground();
    textView.setBackground(null);
    int width = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
    int height = textView.getHeight() - textView.getPaddingTop() - textView.getPaddingBottom();
    if (width == 0 || height == 0) {
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.translate(-textView.getPaddingLeft(), -textView.getPaddingTop());
    textView.draw(canvas);
    textView.setBackground(background);
    return bitmap;
}
 
Example 4
Source File: TextResize.java    From android-instant-apps with Apache License 2.0 5 votes vote down vote up
private static Bitmap captureTextBitmap(TextView textView) {
    Drawable background = textView.getBackground();
    textView.setBackground(null);
    int width = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
    int height = textView.getHeight() - textView.getPaddingTop() - textView.getPaddingBottom();
    if (width == 0 || height == 0) {
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.translate(-textView.getPaddingLeft(), -textView.getPaddingTop());
    textView.draw(canvas);
    textView.setBackground(background);
    return bitmap;
}
 
Example 5
Source File: TextSizeTransition.java    From android-login with MIT License 5 votes vote down vote up
private static Bitmap captureTextBitmap(TextView textView) {
  Drawable background = textView.getBackground();
  textView.setBackground(null);
  int width = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
  int height = textView.getHeight() - textView.getPaddingTop() - textView.getPaddingBottom();
  if (width == 0 || height == 0) {
    return null;
  }
  Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);
  canvas.translate(-textView.getPaddingLeft(), -textView.getPaddingTop());
  textView.draw(canvas);
  textView.setBackground(background);
  return bitmap;
}
 
Example 6
Source File: Generators.java    From glide-support with The Unlicense 5 votes vote down vote up
/** OP's original implementation fixed for real centering */
public static Bitmap imageWithText(Context context, Bitmap bitmap, GenerateParams params) {
	TextView view = new TextView(context);
	view.setText(params.text);
	view.setTextColor(params.color);
	view.setBackgroundColor(params.background);
	view.setTypeface(null, Typeface.BOLD);
	view.setGravity(Gravity.CENTER);
	view.setTextSize(20);
	Canvas canvas = new Canvas(bitmap);
	view.measure(makeMeasureSpec(canvas.getWidth(), EXACTLY), makeMeasureSpec(canvas.getHeight(), EXACTLY));
	view.layout(0, 0, canvas.getWidth(), canvas.getHeight());
	view.draw(canvas);
	return bitmap;
}
 
Example 7
Source File: ReflectionActivity.java    From AndroidDemo with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	final FrameLayout root = new FrameLayout(this);
	setContentView(root);

	final TextView textView = new TextView(this);
	textView.setText("test");
	textView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
	textView.setTextColor(Color.WHITE);
	textView.setTextSize(40);
	textView.setGravity(Gravity.CENTER_HORIZONTAL);
	textView.setBackgroundColor(Color.RED);
	textView.setPadding(100, 20, 100, 20);

	final int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
	textView.measure(measureSpec, measureSpec);
	textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
	final Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888);
	final Canvas c = new Canvas(b);
	textView.draw(c);

	final ImageView imageView = new ImageView(this);
	imageView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
	imageView.setImageBitmap(createReflection(b, 1, 10));
	root.addView(imageView);
}
 
Example 8
Source File: AndroidTextDrawer.java    From settlers-remake with MIT License 4 votes vote down vote up
private int findLineFor(String string) {
	int line = findExistingString(string);
	if (line >= 0) {
		return line;
	}

	int width = (int) Math.ceil(computeWidth(string) + 25);
	renderer = new TextView(context.getAndroidContext());
	renderer.setTextColor(Color.WHITE);
	renderer.setSingleLine(true);
	renderer.setTextSize(TypedValue.COMPLEX_UNIT_PX, getScaledSize());
	renderer.setText(string);

	int firstLine = findLineToUse();
	// System.out.println("string cache miss for " + string +
	// ", allocating new line: " + firstLine);
	int lastLine = firstLine;

	for (int x = 0; x < width; x += TEXTURE_WIDTH) {
		if (x == 0) {
			line = firstLine;
		} else {
			line = findLineToUse();
			nextTile[lastLine] = line;
			linestrings[line] = null;
			linewidths[line] = -1;
		}
		// important to not allow cycles.
		lastused[line] = Integer.MAX_VALUE;
		// just to be sure.
		nextTile[line] = -1;

		// render the new text to that line.
		Bitmap bitmap = Bitmap.createBitmap(TEXTURE_WIDTH, lineheight, Bitmap.Config.ALPHA_8);
		Canvas canvas = new Canvas(bitmap);
		renderer.layout(0, 0, width, lineheight);
		canvas.translate(-x, 0);
		renderer.draw(canvas);
		// canvas.translate(50, .8f * lineheight);
		int points = lineheight * TEXTURE_WIDTH;
		ByteBuffer alpha8 = ByteBuffer.allocateDirect(points);
		bitmap.copyPixelsToBuffer(alpha8);
		ByteBuffer updateBuffer;
		if(context instanceof GLES20DrawContext) {
			updateBuffer = ByteBuffer.allocateDirect(points*4);
			for(int i = 0;i != points;i++) {
				updateBuffer.putInt(0xFFFFFF00|alpha8.get(i));
			}
		} else {
			updateBuffer = alpha8;
		}
		updateBuffer.rewind();
		context.updateFontTexture(texture, 0, line*lineheight, TEXTURE_WIDTH, lineheight, updateBuffer);
		lastLine = line;
	}
	lastused[firstLine] = lastUsedCount++;
	linestrings[firstLine] = string;
	linewidths[firstLine] = width;

	checkInvariants();
	return firstLine;
}