Java Code Examples for android.text.TextPaint#setTextAlign()

The following examples show how to use android.text.TextPaint#setTextAlign() . 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: SplashView.java    From KA27 with Apache License 2.0 6 votes vote down vote up
private void draw(Canvas canvas, int x, int y, int radius) {
    if (radius > 0) canvas.drawCircle(x / 2, y / 2, radius, mPaintCircle);
    matrix.postRotate(rotate);
    Bitmap iconRotate = Bitmap.createBitmap(icon, 0, 0, icon.getWidth(), icon.getHeight(), matrix, false);
    canvas.drawBitmap(iconRotate, x / 2 - iconRotate.getWidth() / 2, y / 2 - iconRotate.getHeight() / 2, mPaintCircle);

    TextPaint textPaint = new TextPaint();
    textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
    textPaint.setColor(textColor);
    textPaint.setAntiAlias(true);
    textPaint.setTextAlign(Paint.Align.CENTER);
    textPaint.setTextSize(textSize);
    float textHeight = textPaint.descent() - textPaint.ascent();
    float textOffset = (textHeight / 2) - textPaint.descent();

    canvas.drawText(getResources().getString(R.string.root_waiting), x / 2, y - textOffset - y / 4, textPaint);
}
 
Example 2
Source File: TextViewMultilineEllipse.java    From document-viewer with GNU General Public License v3.0 6 votes vote down vote up
private void init() {
    mExpanded = false;
    mDrawEllipsizeMoreString = true;
    mRightAlignEllipsizeMoreString = false;
    mMaxLines = -1;
    mStrEllipsis = "...";
    mStrEllipsisMore = "";
    mColorEllipsizeMore = 0xFF0000FF;

    mBreakerExpanded = new LineBreaker();
    mBreakerCollapsed = new LineBreaker();

    // Default font size and color.
    mTextPaint = new TextPaint();
    mTextPaint.setAntiAlias(true);
    mTextPaint.setTextSize(13);
    mTextPaint.setColor(0xFF000000);
    mTextPaint.setTextAlign(Align.LEFT);
}
 
Example 3
Source File: TitleLineGraphSeries.java    From WiFiAnalyzer with GNU General Public License v3.0 6 votes vote down vote up
/**
 * do the initialization
 * creates internal objects
 */
protected void init() {
    mStyles = new Styles();
    mPaint = new Paint();
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaintBackground = new Paint();

    mSelectionPaint = new Paint();
    mSelectionPaint.setColor(Color.argb(80, 0, 0, 0));
    mSelectionPaint.setStyle(Paint.Style.FILL);

    mPathBackground = new Path();
    mPath = new Path();

    mAnimationInterpolator = new AccelerateInterpolator(2f);

    paintTitle = new TextPaint();
    paintTitle.setTextAlign(Paint.Align.CENTER);
}
 
Example 4
Source File: DraggableFlagView.java    From DraggableFlagView with Apache License 2.0 6 votes vote down vote up
private void init(Context context) {
    this.context = context;

    setBackgroundColor(Color.TRANSPARENT);

    // 设置绘制flag的paint
    paint = new Paint();
    paint.setColor(patientColor);
    paint.setAntiAlias(true);

    // 设置绘制文字的paint
    textPaint = new TextPaint();
    textPaint.setAntiAlias(true);
    textPaint.setColor(Color.WHITE);
    textPaint.setTextSize(ABTextUtil.sp2px(context, 12));
    textPaint.setTextAlign(Paint.Align.CENTER);
    textFontMetrics = textPaint.getFontMetrics();

}
 
Example 5
Source File: IconDrawable.java    From grblcontroller with GNU General Public License v3.0 6 votes vote down vote up
private void init(Context context, Icon icon) {
    this.context = context;
    this.icon = icon;
    paint = new TextPaint();
    IconFontDescriptorWrapper descriptor = Iconify.findTypefaceOf(icon);
    if (descriptor == null) {
        throw new IllegalStateException("Unable to find the module associated " +
                "with icon " + icon.key() + ", have you registered the module " +
                "you are trying to use with Iconify.with(...) in your Application?");
    }
    paint.setTypeface(descriptor.getTypeface(context));
    paint.setStyle(Paint.Style.FILL);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setUnderlineText(false);
    paint.setColor(Color.BLACK);
    paint.setAntiAlias(true);
}
 
Example 6
Source File: IconicsDrawableOld.java    From clear-todolist with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create an IconDrawable.
 * Just give it the icon-identifier
 *
 * @param context Your activity or application context.
 * @param icon    The icon identifier without icon- (font must be registered)
 */
public IconicsDrawableOld(Context context, String icon) {
    this.context = context;

    ITypeface font = Iconics.findFont(icon.substring(0, 3));
    icon = icon.replace("-", "_");
    this.icon = font.getIcon(icon);

    paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    paint.setTypeface(font.getTypeface(context));
    paint.setStyle(Paint.Style.STROKE);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setUnderlineText(false);
    paint.setColor(Color.BLACK);
    paint.setAntiAlias(true);
}
 
Example 7
Source File: Page.java    From Mupdf with Apache License 2.0 5 votes vote down vote up
private TextPaint textPaint() {
    final TextPaint paint = new TextPaint();
    paint.setColor(Color.BLACK);
    paint.setAntiAlias(true);
    paint.setTextSize(32);
    paint.setTextAlign(Paint.Align.CENTER);
    return paint;
}
 
Example 8
Source File: DigitalWatchFace.java    From android-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(SurfaceHolder holder) {
    super.onCreate(holder);

    setWatchFaceStyle(new WatchFaceStyle.Builder(DigitalWatchFace.this)
            .setHideStatusBar(true)
            .setShowSystemUiTime(false) // we set the UI time to false because we will already show the time on the watch by drawing it onto the canvas
            .setAmbientPeekMode(WatchFaceStyle.AMBIENT_PEEK_MODE_HIDDEN)    //when the watch enters in ambient mode, no peek card will be visible
            .setCardPeekMode(WatchFaceStyle.PEEK_MODE_SHORT)    //this will specify that the first card peeked and shown on the watch will have a single line tail (i.e. it will have small height)
            .setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE) //the background of the peek card should only be shown briefly, and only if the peek card represents an interruptive notification
            .build());

    //Initialize every thing here, not in onDraw().
    mDateFormatWithoutSec = new SimpleDateFormat("hh:mm a", Locale.getDefault());
    mDateFormatWithSec = new SimpleDateFormat("hh:mm:ss a", Locale.getDefault());

    //Clock text paint
    mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    mTextPaint.setColor(ContextCompat.getColor(DigitalWatchFace.this, android.R.color.white));
    mTextPaint.setTextSize(getResources().getDimension(R.dimen.clock_text_size));
    mTextPaint.setTextAlign(Paint.Align.CENTER);
    mTextPaint.setTypeface(Typeface.createFromAsset(getAssets(), "Montserrat-Bold.ttf"));

    //Load the selected color from the preference file
    mNormalBgColor = Color.parseColor(getSharedPreferences("settings", Context.MODE_PRIVATE)
            .getString("select_color", "#000000"));

    //set the ticker to tick every second when watch is in interactive mode
    mTimeTick = new Handler(Looper.myLooper());
    mTimeTick.postDelayed(mTimeRunnable, 1000);
}
 
Example 9
Source File: BitmapUtils.java    From hr with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Bitmap getAlphabetImage(Context context, String content) {
    Resources res = context.getResources();
    Bitmap mDefaultBitmap = BitmapFactory.decodeResource(res, android.R.drawable.sym_def_app_icon);
    int width = mDefaultBitmap.getWidth();
    int height = mDefaultBitmap.getHeight();
    TextPaint mPaint = new TextPaint();
    mPaint.setTypeface(OControlHelper.boldFont());
    mPaint.setColor(Color.WHITE);
    mPaint.setTextAlign(Paint.Align.CENTER);
    mPaint.setAntiAlias(true);
    int textSize = res.getDimensionPixelSize(R.dimen.text_size_xxlarge);

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas();
    Rect mBounds = new Rect();
    canvas.setBitmap(bitmap);
    canvas.drawColor(OStringColorUtil.getStringColor(context, content));
    if (content == null || content.trim().length() == 0) {
        content = "?";
    }
    char[] alphabet = {Character.toUpperCase(content.trim().charAt(0))};
    mPaint.setTextSize(textSize);
    mPaint.getTextBounds(alphabet, 0, 1, mBounds);
    canvas.drawText(alphabet, 0, 1, 0 + width / 2,
            0 + height / 2 + (mBounds.bottom - mBounds.top) / 2, mPaint);
    return bitmap;
}
 
Example 10
Source File: StepView.java    From StepView with Apache License 2.0 5 votes vote down vote up
public StepView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextAlign(Paint.Align.CENTER);
    textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    textPaint.setTextAlign(Paint.Align.CENTER);
    applyStyles(context, attrs, defStyleAttr);
    drawEditMode();
}
 
Example 11
Source File: BitmapUtils.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Bitmap getAlphabetImage(Context context, String content) {
    Resources res = context.getResources();
    Bitmap mDefaultBitmap = BitmapFactory.decodeResource(res, android.R.drawable.sym_def_app_icon);
    int width = mDefaultBitmap.getWidth();
    int height = mDefaultBitmap.getHeight();
    TextPaint mPaint = new TextPaint();
    mPaint.setTypeface(OControlHelper.boldFont());
    mPaint.setColor(Color.WHITE);
    mPaint.setTextAlign(Paint.Align.CENTER);
    mPaint.setAntiAlias(true);
    int textSize = res.getDimensionPixelSize(R.dimen.text_size_xxlarge);

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas();
    Rect mBounds = new Rect();
    canvas.setBitmap(bitmap);
    canvas.drawColor(OStringColorUtil.getStringColor(context, content));
    if (content == null || content.trim().length() == 0) {
        content = "?";
    }
    char[] alphabet = {Character.toUpperCase(content.trim().charAt(0))};
    mPaint.setTextSize(textSize);
    mPaint.getTextBounds(alphabet, 0, 1, mBounds);
    canvas.drawText(alphabet, 0, 1, width / 2,
            height / 2 + (mBounds.bottom - mBounds.top) / 2, mPaint);
    return bitmap;
}
 
Example 12
Source File: MyPieChartView.java    From kAndroid with Apache License 2.0 5 votes vote down vote up
private void initPaint() {
    mPieChartPaint = new Paint();
    //是否开启抗锯齿
    mPieChartPaint.setAntiAlias(true);
    //防抖动
    mPieChartPaint.setDither(true);
    //画笔样式 STROKE 只绘制图形轮廓(描边) FILL 只绘制图形内容 FILL_AND_STROKE 既绘制轮廓也绘制内容
    mPieChartPaint.setStyle(Paint.Style.FILL);
    //画笔宽度
    mPieChartPaint.setStrokeWidth(mPieChartWidth);
    /*mPieChartPaint!!.strokeCap =
      Paint.Cap.SQUARE*///笔刷样式 //当画笔样式为STROKE或FILL_OR_STROKE时,
    // 设置笔刷的图形样式,如圆形样式Cap.ROUND,或方形样式Cap.SQUARE
    //mPieChartPaint!!.color = Color.RED

    mDataPaint = new TextPaint();
    mDataPaint.setDither(true);
    mDataPaint.setAntiAlias(true);
    mDataPaint.setTextSize(mDataSize);
    mDataPaint.setColor(mDataColor);
    //从中间向两边绘制,不需要再次计算文字
    mDataPaint.setTextAlign(Paint.Align.CENTER);

    mUnitPaint = new TextPaint();
    mUnitPaint.setDither(true);
    mUnitPaint.setAntiAlias(true);
    mUnitPaint.setTextSize(mUnitSize);
    mUnitPaint.setColor(mUnitColor);
    //从中间向两边绘制,不需要再次计算文字
    mUnitPaint.setTextAlign(Paint.Align.CENTER);

    mPointingPaint = new Paint();
    mPointingPaint.setDither(true);
    mPointingPaint.setAntiAlias(true);
    mPointingPaint.setColor(mPointingColor);
    //从中间向两边绘制,不需要再次计算文字
    mPointingPaint.setStrokeWidth(mPointingWidth);
}
 
Example 13
Source File: ColorPickerView.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
private void init() {
    mRingPaint = new Paint();
    mRingPaint.setAntiAlias(true);
    mRingPaint.setColor(Color.WHITE);
    mRingPaint.setStyle(Paint.Style.STROKE);

    mFocusPaint = new Paint();
    mFocusPaint.setAntiAlias(true);
    mFocusPaint.setStyle(Paint.Style.STROKE);
    mFocusPaint.setStrokeWidth(3f);
    mFocusPaint.setColor(Color.BLACK);

    mBitmapPaint = new Paint();
    mBitmapPaint.setFilterBitmap(false);

    mGridPaint = new Paint();
    //设置线宽。单位为1像素
    mGridPaint.setStrokeWidth(1f);
    mGridPaint.setStyle(Paint.Style.STROKE);
    //画笔颜色
    mGridPaint.setColor(-3355444);
    mGridShadowPaint = new Paint(mGridPaint);
    mGridShadowPaint.setColor(-12303292);

    mTextPaint = new TextPaint();
    mTextPaint.setAntiAlias(true);
    mTextPaint.setTextAlign(Paint.Align.CENTER);
    mTextPaint.setTypeface(Typeface.MONOSPACE);
    mTextPaint.setTextSize(getResources().getDimensionPixelSize(R.dimen.dk_font_size_12));
}
 
Example 14
Source File: BookPieChartView.java    From kAndroid with Apache License 2.0 4 votes vote down vote up
private void initPaint() {
    mPieChartPaint = new Paint();
    //是否开启抗锯齿
    mPieChartPaint.setAntiAlias(true);
    //防抖动
    mPieChartPaint.setDither(true);
    //画笔样式 STROKE 只绘制图形轮廓(描边) FILL 只绘制图形内容 FILL_AND_STROKE 既绘制轮廓也绘制内容
    mPieChartPaint.setStyle(Paint.Style.FILL);
    //画笔宽度
    mPieChartPaint.setStrokeWidth(mPieChartWidth);
    ///笔刷样式
    // 当画笔样式为STROKE或FILL_OR_STROKE时,
    mPieChartPaint.setStrokeCap(Paint.Cap.SQUARE);
    // 设置笔刷的图形样式,如圆形样式Cap.ROUND,或方形样式Cap.SQUARE
    mPieChartPaint.setColor(Color.RED);

    //数字
    mDataPaint = new TextPaint();
    mDataPaint.setDither(true);
    mDataPaint.setAntiAlias(true);
    mDataPaint.setTextSize(mDataSize);
    mDataPaint.setColor(mDataColor);
    //从中间向两边绘制,不需要再次计算文字
    mDataPaint.setTextAlign(Paint.Align.CENTER);

    //单位
    mUnitPaint = new TextPaint();
    mUnitPaint.setDither(true);
    mUnitPaint.setAntiAlias(true);
    mUnitPaint.setTextSize(mUnitSize);
    mUnitPaint.setColor(mUnitColor);
    //从中间向两边绘制,不需要再次计算文字
    mUnitPaint.setTextAlign(Paint.Align.CENTER);

    mPointingPaint = new Paint();
    mPointingPaint.setDither(true);
    mPointingPaint.setAntiAlias(true);
    mPointingPaint.setColor(mPointingColor);
    //从中间向两边绘制,不需要再次计算文字
    mPointingPaint.setStrokeWidth(mPointingWidth);
}
 
Example 15
Source File: Utils.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
public static void drawMultilineText(Canvas c, StaticLayout textLayout,
                                     float x, float y,
                                     TextPaint paint,
                                     MPPointF anchor, float angleDegrees) {

    float drawOffsetX = 0.f;
    float drawOffsetY = 0.f;
    float drawWidth;
    float drawHeight;

    final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer);

    drawWidth = textLayout.getWidth();
    drawHeight = textLayout.getLineCount() * lineHeight;

    // Android sometimes has pre-padding
    drawOffsetX -= mDrawTextRectBuffer.left;

    // Android does not snap the bounds to line boundaries,
    //  and draws from bottom to top.
    // And we want to normalize it.
    drawOffsetY += drawHeight;

    // To have a consistent point of reference, we always draw left-aligned
    Paint.Align originalTextAlign = paint.getTextAlign();
    paint.setTextAlign(Paint.Align.LEFT);

    if (angleDegrees != 0.f) {

        // Move the text drawing rect in a way that it always rotates around its center
        drawOffsetX -= drawWidth * 0.5f;
        drawOffsetY -= drawHeight * 0.5f;

        float translateX = x;
        float translateY = y;

        // Move the "outer" rect relative to the anchor, assuming its centered
        if (anchor.x != 0.5f || anchor.y != 0.5f) {
            final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees(
                    drawWidth,
                    drawHeight,
                    angleDegrees);

            translateX -= rotatedSize.width * (anchor.x - 0.5f);
            translateY -= rotatedSize.height * (anchor.y - 0.5f);
            FSize.recycleInstance(rotatedSize);
        }

        c.save();
        c.translate(translateX, translateY);
        c.rotate(angleDegrees);

        c.translate(drawOffsetX, drawOffsetY);
        textLayout.draw(c);

        c.restore();
    } else {
        if (anchor.x != 0.f || anchor.y != 0.f) {

            drawOffsetX -= drawWidth * anchor.x;
            drawOffsetY -= drawHeight * anchor.y;
        }

        drawOffsetX += x;
        drawOffsetY += y;

        c.save();

        c.translate(drawOffsetX, drawOffsetY);
        textLayout.draw(c);

        c.restore();
    }

    paint.setTextAlign(originalTextAlign);
}
 
Example 16
Source File: Utils.java    From Ticket-Analysis with MIT License 4 votes vote down vote up
public static void drawMultilineText(Canvas c, StaticLayout textLayout,
                                     float x, float y,
                                     TextPaint paint,
                                     MPPointF anchor, float angleDegrees) {

    float drawOffsetX = 0.f;
    float drawOffsetY = 0.f;
    float drawWidth;
    float drawHeight;

    final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer);

    drawWidth = textLayout.getWidth();
    drawHeight = textLayout.getLineCount() * lineHeight;

    // Android sometimes has pre-padding
    drawOffsetX -= mDrawTextRectBuffer.left;

    // Android does not snap the bounds to line boundaries,
    //  and draws from bottom to top.
    // And we want to normalize it.
    drawOffsetY += drawHeight;

    // To have a consistent point of reference, we always draw left-aligned
    Paint.Align originalTextAlign = paint.getTextAlign();
    paint.setTextAlign(Paint.Align.LEFT);

    if (angleDegrees != 0.f) {

        // Move the text drawing rect in a way that it always rotates around its center
        drawOffsetX -= drawWidth * 0.5f;
        drawOffsetY -= drawHeight * 0.5f;

        float translateX = x;
        float translateY = y;

        // Move the "outer" rect relative to the anchor, assuming its centered
        if (anchor.x != 0.5f || anchor.y != 0.5f) {
            final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees(
                    drawWidth,
                    drawHeight,
                    angleDegrees);

            translateX -= rotatedSize.width * (anchor.x - 0.5f);
            translateY -= rotatedSize.height * (anchor.y - 0.5f);
            FSize.recycleInstance(rotatedSize);
        }

        c.save();
        c.translate(translateX, translateY);
        c.rotate(angleDegrees);

        c.translate(drawOffsetX, drawOffsetY);
        textLayout.draw(c);

        c.restore();
    } else {
        if (anchor.x != 0.f || anchor.y != 0.f) {

            drawOffsetX -= drawWidth * anchor.x;
            drawOffsetY -= drawHeight * anchor.y;
        }

        drawOffsetX += x;
        drawOffsetY += y;

        c.save();

        c.translate(drawOffsetX, drawOffsetY);
        textLayout.draw(c);

        c.restore();
    }

    paint.setTextAlign(originalTextAlign);
}
 
Example 17
Source File: PageLoader.java    From HaoReader with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 作用:设置与文字相关的参数
 */
private void setTextParams() {
    // 文字大小
    mTextSize = ScreenUtils.spToPx(mSettingManager.getTextSize());
    // 行间距
    mTextInterval = ScreenUtils.dpToPx(mSettingManager.getLineSpacing());
    // 段落间距
    mTextPara = ScreenUtils.dpToPx(mSettingManager.getParagraphSpacing());

    Typeface typeface;
    try {
        if (mSettingManager.getFontPath() != null) {
            typeface = Typeface.createFromFile(mSettingManager.getFontPath());
        } else {
            typeface = Typeface.SANS_SERIF;
        }
    } catch (Exception e) {
        ToastUtils.toast(mContext, "字体文件未找,到恢复默认字体");
        mSettingManager.setReadBookFont(null);
        typeface = Typeface.SANS_SERIF;
    }

    // 绘制提示的画笔
    mTipPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    mTipPaint.setColor(mTextColor);
    mTipPaint.setTextAlign(Paint.Align.LEFT); // 绘制的起始点
    mTipPaint.setTextSize(ScreenUtils.spToPx(DEFAULT_TIP_SIZE)); // Tip默认的字体大小
    mTipPaint.setTypeface(typeface);
    mTipPaint.setFakeBoldText(mSettingManager.getTextBold());
    mTipPaint.setSubpixelText(true);
    mTipPaint.setDither(true);

    // 绘制标题的画笔
    mTitlePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    mTitlePaint.setColor(mTextColor);
    mTitlePaint.setTextSize(mTextSize * 1.25f);
    mTitlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mTitlePaint.setTypeface(typeface);
    mTitlePaint.setFakeBoldText(true);
    mTitlePaint.setSubpixelText(true);
    mTitlePaint.setDither(true);

    // 绘制页面内容的画笔
    mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    mTextPaint.setColor(mTextColor);
    mTextPaint.setTextSize(mTextSize);
    mTextPaint.setTypeface(typeface);
    mTextPaint.setFakeBoldText(mSettingManager.getTextBold());
    mTextPaint.setSubpixelText(true);
    mTextPaint.setDither(true);
}
 
Example 18
Source File: Utils.java    From android-kline with Apache License 2.0 4 votes vote down vote up
public static void drawMultilineText(Canvas c, StaticLayout textLayout,
                                     float x, float y,
                                     TextPaint paint,
                                     MPPointF anchor, float angleDegrees) {

    float drawOffsetX = 0.f;
    float drawOffsetY = 0.f;
    float drawWidth;
    float drawHeight;

    final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer);

    drawWidth = textLayout.getWidth();
    drawHeight = textLayout.getLineCount() * lineHeight;

    // Android sometimes has pre-padding
    drawOffsetX -= mDrawTextRectBuffer.left;

    // Android does not snap the bounds to line boundaries,
    //  and draws from bottom to top.
    // And we want to normalize it.
    drawOffsetY += drawHeight;

    // To have a consistent point of reference, we always draw left-aligned
    Paint.Align originalTextAlign = paint.getTextAlign();
    paint.setTextAlign(Paint.Align.LEFT);

    if (angleDegrees != 0.f) {

        // Move the text drawing rect in a way that it always rotates around its center
        drawOffsetX -= drawWidth * 0.5f;
        drawOffsetY -= drawHeight * 0.5f;

        float translateX = x;
        float translateY = y;

        // Move the "outer" rect relative to the anchor, assuming its centered
        if (anchor.x != 0.5f || anchor.y != 0.5f) {
            final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees(
                    drawWidth,
                    drawHeight,
                    angleDegrees);

            translateX -= rotatedSize.width * (anchor.x - 0.5f);
            translateY -= rotatedSize.height * (anchor.y - 0.5f);
            FSize.recycleInstance(rotatedSize);
        }

        c.save();
        c.translate(translateX, translateY);
        c.rotate(angleDegrees);

        c.translate(drawOffsetX, drawOffsetY);
        textLayout.draw(c);

        c.restore();
    } else {
        if (anchor.x != 0.f || anchor.y != 0.f) {

            drawOffsetX -= drawWidth * anchor.x;
            drawOffsetY -= drawHeight * anchor.y;
        }

        drawOffsetX += x;
        drawOffsetY += y;

        c.save();

        c.translate(drawOffsetX, drawOffsetY);
        textLayout.draw(c);

        c.restore();
    }

    paint.setTextAlign(originalTextAlign);
}
 
Example 19
Source File: JustifiedTextView.java    From RxAndroidBootstrap with Apache License 2.0 4 votes vote down vote up
private void initTextPaint(){
    textPaint=new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
    textPaint.setTextAlign(Align.RIGHT);
}
 
Example 20
Source File: CircularView.java    From CircularView with Apache License 2.0 4 votes vote down vote up
private void init(AttributeSet attrs, int defStyle) {
    // Load attributes
    final TypedArray a = getContext().obtainStyledAttributes(
            attrs, R.styleable.CircularView, defStyle, 0);
    final int centerBackgroundColor = a.getColor(
            R.styleable.CircularView_centerBackgroundColor,
            CircularViewObject.NO_COLOR);

    // Set up a default TextPaint object
    mTextPaint = new TextPaint();
    mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    mTextPaint.setTextAlign(Paint.Align.LEFT);

    mText = a.getString(R.styleable.CircularView_text);
    mTextPaint.setTextSize(a.getDimension(
            R.styleable.CircularView_textSize,
            24f));
    mTextPaint.setColor(a.getColor(
            R.styleable.CircularView_textColor,
            mTextPaint.getColor()));


    Drawable circleDrawable = null;
    if (a.hasValue(R.styleable.CircularView_centerDrawable)) {
        circleDrawable = a.getDrawable(
                R.styleable.CircularView_centerDrawable);
        circleDrawable.setCallback(this);
    }

    mHighlightedDegreeObjectAnimator = new ObjectAnimator();
    mHighlightedDegreeObjectAnimator.setTarget(CircularView.this);
    mHighlightedDegreeObjectAnimator.setPropertyName("highlightedDegree");
    mHighlightedDegreeObjectAnimator.addListener(mAnimatorListener);

    // Update TextPaint and text measurements from attributes
    invalidateTextPaintAndMeasurements();

    mCirclePaint = new Paint();
    mCirclePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    mCirclePaint.setStyle(Paint.Style.FILL);
    mCirclePaint.setColor(Color.RED);

    mDrawHighlightedMarkerOnTop = a.getBoolean(R.styleable.CircularView_drawHighlightedMarkerOnTop, false);
    mHighlightedMarker = null;
    mHighlightedMarkerPosition = -1;
    mHighlightedDegree = a.getFloat(R.styleable.CircularView_highlightedDegree, HIGHLIGHT_NONE);
    mMarkerStartingPoint = a.getFloat(R.styleable.CircularView_markerStartingPoint, 0f);
    mAnimateMarkersOnStillHighlight = a.getBoolean(R.styleable.CircularView_animateMarkersOnStillHighlight, false);
    mAnimateMarkersOnHighlightAnimation = false;
    mIsAnimating = false;

    mCircle = new CircularViewObject(getContext(), CIRCLE_TO_MARKER_PADDING, centerBackgroundColor);
    mCircle.setSrc(circleDrawable);
    mCircle.setFitToCircle(a.getBoolean(R.styleable.CircularView_fitToCircle, false));

    mDefaultMarkerRadius = getResources().getInteger(R.integer.cv_default_marker_radius);

    mEditModeMarkerCount = a.getInt(R.styleable.CircularView_editMode_markerCount, 0);
    mEditModeMarkerRadius = a.getInt(R.styleable.CircularView_editMode_markerRadius, mDefaultMarkerRadius);

    a.recycle();

    setOnLongClickListener(mOnLongClickListener);

    if (isInEditMode()) {
        mAdapter = new SimpleCircularViewAdapter() {
            @Override
            public int getCount() {
                return mEditModeMarkerCount;
            }

            @Override
            public void setupMarker(int position, Marker marker) {
                marker.setRadius(mEditModeMarkerRadius);
                marker.setCenterBackgroundColor(getResources().getColor(android.R.color.black));
            }
        };
    }
}