/
arti4
/
IndividualProject
Обзор
Документация
Войти
/
arti4
/
IndividualProject
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
MyGame/app/src/main/java/com/example/mygame/TriangleButton.java
161 строка
5 KB
arti4
upload files
18 ноя 2025, 12:41
18 ноя 2025, 12:41
f3a2f8a
Код
Авторство
О чём код?
package com.example.mygame; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.util.AttributeSet; import android.view.View; public class TriangleButton extends View { private Paint trianglePaint; private Paint strokePaint; private Paint textPaint; private boolean isPressed = false; private int normalColor; private int pressedColor; private int strokeColor; private int textColor; public TriangleButton(Context context) { super(context); init(context); } public TriangleButton(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public TriangleButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } private void init(Context context) { normalColor = Color.parseColor("#FF32CD32"); pressedColor = Color.parseColor("#FF228B22"); strokeColor = Color.WHITE; textColor = Color.WHITE; trianglePaint = new Paint(); trianglePaint.setStyle(Paint.Style.FILL); trianglePaint.setAntiAlias(true); strokePaint = new Paint(); strokePaint.setStyle(Paint.Style.STROKE); strokePaint.setStrokeWidth(4f); strokePaint.setAntiAlias(true); textPaint = new Paint(); textPaint.setColor(textColor); textPaint.setTextSize(36f); textPaint.setTextAlign(Paint.Align.CENTER); textPaint.setAntiAlias(true); setClickable(true); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = getWidth(); int height = getHeight(); float scale = 0.8f; int scaledWidth = (int)(width * scale); int scaledHeight = (int)(height * scale); int offsetX = (width - scaledWidth) / 2; int offsetY = (height - scaledHeight) / 2; int padding = 5; if (isPressed) { trianglePaint.setColor(pressedColor); strokePaint.setColor(strokeColor); } else { trianglePaint.setColor(normalColor); strokePaint.setColor(strokeColor); } Path trianglePath = createRotatedTrianglePath(scaledWidth, scaledHeight, padding, offsetX, offsetY); canvas.drawPath(trianglePath, trianglePaint); canvas.drawPath(trianglePath, strokePaint); float textY = scaledHeight + offsetY + 50; canvas.drawText("PLAY", width / 2, textY, textPaint); } private Path createRotatedTrianglePath(int width, int height, int padding, int offsetX, int offsetY) { Path path = new Path(); float rightX = width - padding + offsetX; float rightY = height / 2f + offsetY; float topX = padding + offsetX; float topY = padding + offsetY; float bottomX = padding + offsetX; float bottomY = height - padding + offsetY; path.moveTo(rightX, rightY); path.lineTo(topX, topY); path.lineTo(bottomX, bottomY); path.close(); return path; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int desiredWidth = 120; int desiredHeight = 120; int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int width; int height; if (widthMode == MeasureSpec.EXACTLY) { width = widthSize; } else if (widthMode == MeasureSpec.AT_MOST) { width = Math.min(desiredWidth, widthSize); } else { width = desiredWidth; } if (heightMode == MeasureSpec.EXACTLY) { height = heightSize; } else if (heightMode == MeasureSpec.AT_MOST) { height = Math.min(desiredHeight, heightSize); } else { height = desiredHeight; } setMeasuredDimension(width, height); } public void setPressed(boolean pressed) { if (isPressed != pressed) { isPressed = pressed; invalidate(); } } @Override public void setOnClickListener(OnClickListener l) { super.setOnClickListener(l); } public void setNormalColor(int color) { this.normalColor = color; invalidate(); } public void setPressedColor(int color) { this.pressedColor = color; invalidate(); } public void setStrokeColor(int color) { this.strokeColor = color; invalidate(); } public void setTextColor(int color) { this.textColor = color; textPaint.setColor(color); invalidate(); } }