Создание кругового вида фиксированного размера в андроид студии - PullRequest
0 голосов
/ 04 декабря 2018

Так что у меня есть немного разочарования для меня.То, чего я хотел бы добиться, - это кнопка с двумя текстовыми изображениями в центре.Один с именем и один с числовым значением ... Как счетчик ...

Я бы хотел, чтобы это выглядело так:

Counter Idea

Вокруг него тонкая белая рамка (учитывая, что ее цвет не совсем виден ... извините).

Мне бы хотелось, чтобы значение было увеличено науменьшение в зависимости от того, как нажата кнопка.

Теперь у меня есть следующее:

attrs.xml

<resources>
<declare-styleable name="ButtonCounter">
    <attr name="backgroundColor" format="color"/>
    <attr name="borderColor" format="color"/>
    <attr name="borderSize" format="integer"/>
    <attr name="labelNameColor" format="color"/>
    <attr name="labelValueColor" format="color"/>
    <attr name="labelName" format="string"></attr>
    <attr name="labelValue" format="string"></attr>
</declare-styleable>

ButtonCounter.java

public class ButtonCounter extends View {
private int                 backgroundColor,
                            borderColor,
                            borderSize,
                            labelNameColor,
                            labelValueColor;
private String              labelName,
                            labelValue;
private Paint               paintCircle,
                            paintStroke;

public ButtonCounter(Context context, AttributeSet attrs) {
    super(context);

    paintCircle = new Paint();
    paintStroke = new Paint();

    TypedArray a = context.getTheme().obtainStyledAttributes(
            attrs,
            R.styleable.ButtonCounter
            ,
            0,
            0
    );

    try {
        backgroundColor = a.getInteger(R.styleable.ButtonCounter_backgroundColor, 0);
        borderColor = a.getInteger(R.styleable.ButtonCounter_borderColor, 0);
        borderSize = a.getInteger(R.styleable.ButtonCounter_borderSize, 0);
        labelNameColor = a.getInteger(R.styleable.ButtonCounter_labelNameColor,0);
        labelValueColor = a.getInteger(R.styleable.ButtonCounter_labelValueColor, 0 );
        labelName = a.getString(R.styleable.ButtonCounter_labelName);
        labelValue = a.getString(R.styleable.ButtonCounter_labelValue);
    } finally {
        a.recycle();
    }
}

@Override
protected void onDraw(Canvas canvas) {
    paintCircle.setColor(backgroundColor);
    paintCircle.setAntiAlias(true);
    paintStroke.setColor(borderColor);
    paintStroke.setAntiAlias(true);

    int width = this.getMeasuredWidth();
    int height = this.getMeasuredHeight();

    int diameter = ((height > width) ? height : width);
    int radius = diameter / 2;


    canvas.drawCircle(diameter / 2, diameter / 2, radius - borderSize, paintCircle);
    canvas.drawCircle(diameter / 2, diameter / 2, radius, paintStroke);
}

}

Это уже не то, чего я хочу, потому что оно становится слишком большим.

Есть какие-нибудь идеи о том, как я мог бы начать пытаться это делать?

Спасибо!

1 Ответ

0 голосов
/ 04 декабря 2018

Привет. Если вы хотите добиться того же самого без использования Java Class, то посмотрите следующее решение:

  1. Создать чертеж с любым именем позволяет circle_bg_xml
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
        <solid android:color="@color/black" />
    </shape>
Теперь в вашем макете используйте два TextView (или любое другое представление) внутри LinearLayout и установите его для рисования в качестве фона Layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="100dp"
    android:layout_height="100dp"
    android:gravity="center"
    android:background="@drawable/circle_bg">
    <TextView
        android:layout_width="wrap_content"
        android:textColor="@color/white"
        android:textSize="15sp"
        android:layout_height="wrap_content"
        android:text="Global"/>
    <TextView
        android:layout_width="wrap_content"
        android:textColor="@color/white"
        android:textSize="22sp"
        android:layout_height="wrap_content"
        android:text="0"/>
</LinearLayout>

Надеюсь, это поможет вам.Спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...