Java Code Examples for android.text.Layout#draw()

The following examples show how to use android.text.Layout#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: TextureWarmer.java    From litho with Apache License 2.0 5 votes vote down vote up
@Override
public void handleMessage(Message msg) {
  if (mPicture == null) {
    return;
  }

  final Canvas canvas;

  try {
    switch (msg.what) {
      case WARM_LAYOUT:
        final Layout layout = ((WeakReference<Layout>) msg.obj).get();

        if (layout == null) {
          break;
        }

        canvas =
            mPicture.beginRecording(layout.getWidth(), LayoutMeasureUtil.getHeight(layout));

        layout.draw(canvas);
        mPicture.endRecording();
        break;
      case WARM_DRAWABLE:
        final WarmDrawable warmDrawable = ((WeakReference<WarmDrawable>) msg.obj).get();

        if (warmDrawable == null) {
          break;
        }

        canvas = mPicture.beginRecording(warmDrawable.width, warmDrawable.height);
        warmDrawable.drawable.draw(canvas);
        mPicture.endRecording();
        break;
    }
  } catch (Exception e) {
    // Nothing to do here. This is a best effort. No real problem if it fails.
  }
}
 
Example 2
Source File: WXTextDomObject.java    From ucar-weex-core with Apache License 2.0 5 votes vote down vote up
/**
 * As warming up TextLayoutCache done in the DOM thread may manipulate UI operation,
 there may be some exception, in which case the exception is ignored. After all,
 this is just a warm up operation.
 * @return false for warm up failure, otherwise returns true.
 */
private boolean warmUpTextLayoutCache(Layout layout) {
  boolean result;
  try {
    layout.draw(DUMMY_CANVAS);
    result = true;
  } catch (Exception e) {
    WXLogUtils.eTag(TAG, e);
    result = false;
  }
  return result;
}
 
Example 3
Source File: WXTextView.java    From ucar-weex-core with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  canvas.save();
  Layout layout= getTextLayout();
  if(layout!=null){
    canvas.translate(getPaddingLeft(),getPaddingTop());
    layout.draw(canvas);
  }
  canvas.restore();
}
 
Example 4
Source File: WXTextDomObject.java    From weex-uikit with MIT License 5 votes vote down vote up
/**
 * As warming up TextLayoutCache done in the DOM thread may manipulate UI operation,
 there may be some exception, in which case the exception is ignored. After all,
 this is just a warm up operation.
 * @return false for warm up failure, otherwise returns true.
 */
private boolean warmUpTextLayoutCache(Layout layout) {
  boolean result;
  try {
    layout.draw(DUMMY_CANVAS);
    result = true;
  } catch (Exception e) {
    WXLogUtils.eTag(TAG, e);
    result = false;
  }
  return result;
}
 
Example 5
Source File: WXTextView.java    From weex-uikit with MIT License 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  canvas.save();
  Layout layout= getTextLayout();
  if(layout!=null){
    canvas.translate(getPaddingLeft(),getPaddingTop());
    layout.draw(canvas);
  }
  canvas.restore();
}
 
Example 6
Source File: StepperIndicator.java    From stepper-indicator with MIT License 5 votes vote down vote up
/**
 * x and y anchored to top-middle point of StaticLayout
 */
public static void drawLayout(Layout layout, float x, float y,
                              Canvas canvas, TextPaint paint) {
    canvas.save();
    canvas.translate(x, y);
    layout.draw(canvas);
    canvas.restore();
}
 
Example 7
Source File: WXTextView.java    From weex with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  canvas.save();
  Layout layout= getTextLayout();
  if(layout!=null){
    canvas.translate(getPaddingLeft(),getPaddingTop());
    layout.draw(canvas);
  }
  canvas.restore();
}
 
Example 8
Source File: GlyphWarmerImpl.java    From TextLayoutBuilder with Apache License 2.0 5 votes vote down vote up
@Override
public void handleMessage(Message msg) {
  Layout layout = (Layout) msg.obj;
  try {
    Canvas canvas =
        mPicture.beginRecording(
            LayoutMeasureUtil.getWidth(layout), LayoutMeasureUtil.getHeight(layout));
    layout.draw(canvas);
    mPicture.endRecording();
  } catch (Exception e) {
    // Do nothing.
  }
}
 
Example 9
Source File: ReflowText.java    From android-proguards with Apache License 2.0 5 votes vote down vote up
private Bitmap createBitmap(@NonNull ReflowData data, @NonNull Layout layout) {
    Bitmap bitmap = Bitmap.createBitmap(
            data.bounds.width(), data.bounds.height(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.translate(data.textPosition.x, data.textPosition.y);
    layout.draw(canvas);
    return bitmap;
}
 
Example 10
Source File: CustomViewAccessibilityActivity.java    From codeexamples-android with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.save();
    canvas.translate(getPaddingLeft(), getPaddingRight());
    Layout switchText = mChecked ? mOnLayout : mOffLayout;
    switchText.draw(canvas);
    canvas.restore();
}
 
Example 11
Source File: Switch.java    From PreferenceFragment with Apache License 2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // Draw the switch
    int switchLeft = mSwitchLeft;
    int switchTop = mSwitchTop;
    int switchRight = mSwitchRight;
    int switchBottom = mSwitchBottom;

    mTrackDrawable.setBounds(switchLeft, switchTop, switchRight, switchBottom);
    mTrackDrawable.draw(canvas);

    canvas.save();

    mTrackDrawable.getPadding(mTempRect);
    int switchInnerLeft = switchLeft + mTempRect.left;
    int switchInnerTop = switchTop + mTempRect.top;
    int switchInnerRight = switchRight - mTempRect.right;
    int switchInnerBottom = switchBottom - mTempRect.bottom;
    canvas.clipRect(switchInnerLeft, switchTop, switchInnerRight, switchBottom);

    mThumbDrawable.getPadding(mTempRect);
    final int thumbPos = (int) (mThumbPosition + 0.5f);
    int thumbLeft = switchInnerLeft - mTempRect.left + thumbPos;
    int thumbRight = switchInnerLeft + thumbPos + mThumbWidth + mTempRect.right;

    mThumbDrawable.setBounds(thumbLeft, switchTop, thumbRight, switchBottom);
    mThumbDrawable.draw(canvas);

    // mTextColors should not be null, but just in case
    if (mTextColors != null) {
        mTextPaint.setColor(mTextColors.getColorForState(getDrawableState(),
                mTextColors.getDefaultColor()));
    }
    mTextPaint.drawableState = getDrawableState();

    Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
    if (switchText != null) {
        canvas.translate((thumbLeft + thumbRight) / 2 - switchText.getWidth() / 2,
                (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2);
        switchText.draw(canvas);
    }

    canvas.restore();
}
 
Example 12
Source File: AccentSwitch.java    From holoaccent with Apache License 2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
	super.onDraw(canvas);

	// Draw the switch
	int switchLeft = mSwitchLeft;
	int switchTop = mSwitchTop;
	int switchRight = mSwitchRight;
	int switchBottom = mSwitchBottom;

	mTrackDrawable.setBounds(switchLeft, switchTop, switchRight,
			switchBottom);
	mTrackDrawable.draw(canvas);

	canvas.save();

	mTrackDrawable.getPadding(mTempRect);
	int switchInnerLeft = switchLeft + mTempRect.left;
	int switchInnerTop = switchTop + mTempRect.top;
	int switchInnerRight = switchRight - mTempRect.right;
	int switchInnerBottom = switchBottom - mTempRect.bottom;
	canvas.clipRect(switchInnerLeft, switchTop, switchInnerRight,
			switchBottom);

	mThumbDrawable.getPadding(mTempRect);
	final int thumbPos = (int) (mThumbPosition + 0.5f);
	int thumbLeft = switchInnerLeft - mTempRect.left + thumbPos;
	int thumbRight = switchInnerLeft + thumbPos + mThumbWidth
			+ mTempRect.right;

	mThumbDrawable
			.setBounds(thumbLeft, switchTop, thumbRight, switchBottom);
	mThumbDrawable.draw(canvas);

	// mTextColors should not be null, but just in case
	if (mTextColors != null) {
		mTextPaint.setColor(mTextColors.getColorForState(
				getDrawableState(), mTextColors.getDefaultColor()));
	}
	mTextPaint.drawableState = getDrawableState();

	Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
	if (switchText != null) {
		canvas.translate(
				(thumbLeft + thumbRight) / 2 - switchText.getWidth() / 2,
				(switchInnerTop + switchInnerBottom) / 2
						- switchText.getHeight() / 2);
		switchText.draw(canvas);
	}

	canvas.restore();
}
 
Example 13
Source File: Switch.java    From MCPELauncher with Apache License 2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas)
{
	super.onDraw(canvas);

	// Draw the switch
	int switchLeft = mSwitchLeft;
	int switchTop = mSwitchTop;
	int switchRight = mSwitchRight;
	int switchBottom = mSwitchBottom;

	mTrackDrawable.setBounds(switchLeft, switchTop, switchRight, switchBottom);
	mTrackDrawable.draw(canvas);

	canvas.save();

	mTrackDrawable.getPadding(mTempRect);
	int switchInnerLeft = switchLeft + mTempRect.left;
	int switchInnerTop = switchTop + mTempRect.top;
	int switchInnerRight = switchRight - mTempRect.right;
	int switchInnerBottom = switchBottom - mTempRect.bottom;
	canvas.clipRect(switchInnerLeft, switchTop, switchInnerRight, switchBottom);

	mThumbDrawable.getPadding(mTempRect);
	final int thumbPos = (int) (mThumbPosition + 0.5f);
	int thumbLeft = switchInnerLeft - mTempRect.left + thumbPos;
	int thumbRight = switchInnerLeft + thumbPos + mThumbWidth + mTempRect.right;

	mThumbDrawable.setBounds(thumbLeft, switchTop, thumbRight, switchBottom);
	mThumbDrawable.draw(canvas);

	// mTextColors should not be null, but just in case
	if (mTextColors != null)
	{
		mTextPaint.setColor(mTextColors.getColorForState(getDrawableState(), mTextColors.getDefaultColor()));
	}
	mTextPaint.drawableState = getDrawableState();

	Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
	if (switchText != null)
	{
		canvas.translate((thumbLeft + thumbRight) / 2 - switchText.getWidth() / 2, (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2);
		switchText.draw(canvas);
	}

	canvas.restore();
}