/
Libra
/
my-application
Обзор
Документация
Войти
/
Libra
/
my-application
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
app/src/main/java/com/libraprogramming/myapplication/control/MyControl.java
101 строка
4 KB
Толмачёв Владимир Викторович
Next commit
20 ноя 2025, 19:49
20 ноя 2025, 19:49
0db7810
Код
Авторство
О чём код?
package com.libraprogramming.myapplication.control; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.text.Layout; import android.text.StaticLayout; import android.text.TextPaint; import android.util.AttributeSet; import android.view.View; import androidx.annotation.NonNull; public class MyControl extends View { private TextPaint paint; private StaticLayout layout; public MyControl(Context context) { super(context); initView(); } public MyControl(Context context, AttributeSet attrs) { super(context, attrs); initView(); } private void initView() { paint = new TextPaint(); paint.setAntiAlias(true); paint.setTextAlign(Paint.Align.CENTER); paint.setTextSize(14.0f * getResources().getDisplayMetrics().density); paint.setColor(Color.BLACK); final var width = paint.measureText(""); layout = StaticLayout.Builder.obtain("", 0, "".length(), paint, (int) width) .setAlignment(Layout.Alignment.ALIGN_NORMAL) .setLineSpacing(1.0f, 0) .setIncludePad(false) .build(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // Tell the parent layout how big this view would like to be // but still respect any requirements (measure specs) that are passed down. // determine the width int width; int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthRequirement = MeasureSpec.getSize(widthMeasureSpec); if (widthMode == MeasureSpec.EXACTLY) { width = widthRequirement; } else { width = layout.getWidth() + getPaddingLeft() + getPaddingRight(); if (widthMode == MeasureSpec.AT_MOST) { if (width > widthRequirement) { width = widthRequirement; // too long for a single line so relayout as multiline layout = StaticLayout.Builder.obtain("", 0, "".length(), paint, width) .setAlignment(Layout.Alignment.ALIGN_NORMAL) .setLineSpacing(1.0f, 0) .setIncludePad(false) .build(); // layout = new StaticLayout(mText, mTextPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false); } } } // determine the height int height; int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightRequirement = MeasureSpec.getSize(heightMeasureSpec); if (heightMode == MeasureSpec.EXACTLY) { height = heightRequirement; } else { height = layout.getHeight() + getPaddingTop() + getPaddingBottom(); if (heightMode == MeasureSpec.AT_MOST) { height = Math.min(height, heightRequirement); } } // Required call: set width and height setMeasuredDimension(width, height); } @Override protected void onDraw(@NonNull Canvas canvas) { super.onDraw(canvas); // do as little as possible inside onDraw to improve performance // draw the text on the canvas after adjusting for padding canvas.save(); canvas.translate(getPaddingLeft(), getPaddingTop()); // canvas.drawPaint(paint); layout.draw(canvas); canvas.restore(); } }