Skip to content

Commit

Permalink
Add new radio button drawable
Browse files Browse the repository at this point in the history
  • Loading branch information
BloCamLimb committed Jan 2, 2025
1 parent 29976fc commit 83dc3bf
Show file tree
Hide file tree
Showing 5 changed files with 428 additions and 122 deletions.
30 changes: 17 additions & 13 deletions core/src/main/java/icyllis/modernui/TestFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import icyllis.modernui.graphics.text.FontFamily;
import icyllis.modernui.graphics.text.LineBreakConfig;
import icyllis.modernui.material.MaterialCheckBox;
import icyllis.modernui.material.MaterialRadioButton;
import icyllis.modernui.resources.SystemTheme;
import icyllis.modernui.text.*;
import icyllis.modernui.text.style.*;
Expand Down Expand Up @@ -134,14 +133,14 @@ public void draw(@NonNull Canvas canvas) {
}*//*
}
});*/
base.setBackground(new ColorDrawable(SystemTheme.currentTheme().colorSurface));
{
var params = new FrameLayout.LayoutParams(base.dp(960), base.dp(540));
params.gravity = Gravity.CENTER;
base.setLayoutParams(params);
}
//base.setRotation(30);
container.setClipChildren(true);
container.setBackground(new ColorDrawable(SystemTheme.currentTheme().colorSurface));
return base;
}

Expand Down Expand Up @@ -464,7 +463,7 @@ public TestLinearLayout(Context context) {
mGoodAnim = anim;
}

try (Bitmap bitmap = BitmapFactory.decodePath(Path.of("E:/flux_core.png"))) {
/*try (Bitmap bitmap = BitmapFactory.decodePath(Path.of("E:/flux_core.png"))) {
Image image = Image.createTextureFromBitmap(bitmap);
if (image != null) {
Matrix scalingMatrix = new Matrix();
Expand All @@ -475,7 +474,7 @@ public TestLinearLayout(Context context) {
LOGGER.warn("Failed to create image");
}
} catch (IOException ignored) {
}
}*/

mTestLinearGrad = new LinearGradient(
0, 0, 128, 0,
Expand Down Expand Up @@ -616,15 +615,19 @@ public TestLinearLayout(Context context) {
RadioGroup group = new RadioGroup(getContext());
v = group;
for (int j = 0; j < 3; j++) {
RadioButton button = new MaterialRadioButton(getContext());
button.setText("Item " + j);
button.setTextSize(16);
RadioButton button = new RadioButton(getContext());
button.setText(switch (j) {
case 0 -> "English";
case 1 -> "Chinese";
default -> "Spanish";
});
button.setId(9 + j);
SystemTheme.currentTheme().applyRadioButtonStyle(button);
group.addView(button);
}
group.setOnCheckedChangeListener((__, checkedId) ->
/*group.setOnCheckedChangeListener((__, checkedId) ->
Toast.makeText(context, "You checked " + checkedId, Toast.LENGTH_SHORT)
.show());
.show());*/
p = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
} else if (i == 6) {
Expand Down Expand Up @@ -656,6 +659,7 @@ public TestLinearLayout(Context context) {
button.setText("Text button " + i);
v = button;
p = new LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
p.setMargins(0, dp(4),0, dp(4));
}
if (i == 8) {
v.setOnCreateContextMenuListener((menu, v1, menuInfo) -> {
Expand Down Expand Up @@ -771,10 +775,10 @@ public Float get(@Nonnull TestLinearLayout object) {
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mTestImageShader != null) {
/*if (mTestImageShader != null) {
mTestImageShader.release();
mTestImageShader = null;
}
}*/
}

@Override
Expand Down Expand Up @@ -825,7 +829,7 @@ protected void onDraw(@Nonnull Canvas canvas) {
paint.setStrokeCap(Paint.CAP_SQUARE);
canvas.drawRect(200, 450, 500, 550, paint);

canvas.save();
/*canvas.save();
canvas.translate(400, 100);
paint.setShader(mTestImageShader);
paint.setStyle(Paint.FILL);
Expand All @@ -840,7 +844,7 @@ protected void onDraw(@Nonnull Canvas canvas) {
paint.setDither(false);
paint.setStyle(Paint.STROKE);
paint.setShader(null);
canvas.restore();
canvas.restore();*/

paint.setStrokeWidth(40.0f);
//paint.setSmoothWidth(40.0f);
Expand Down
108 changes: 4 additions & 104 deletions core/src/main/java/icyllis/modernui/material/MaterialRadioButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,118 +18,18 @@

package icyllis.modernui.material;

import icyllis.modernui.R;
import icyllis.modernui.core.Context;
import icyllis.modernui.graphics.*;
import icyllis.modernui.graphics.drawable.ShapeDrawable;
import icyllis.modernui.graphics.drawable.StateListDrawable;
import icyllis.modernui.util.ColorStateList;
import icyllis.modernui.util.StateSet;
import icyllis.modernui.view.View;
import icyllis.modernui.resources.SystemTheme;
import icyllis.modernui.widget.RadioButton;

import javax.annotation.Nonnull;

/**
* A fake material design.
* @deprecated to be moved to another package, use base class instead
*/
@Deprecated
public class MaterialRadioButton extends RadioButton {

private static final int[][] ENABLED_CHECKED_STATES = {
new int[]{R.attr.state_enabled, R.attr.state_checked}, // [0]
new int[]{R.attr.state_enabled, -R.attr.state_checked}, // [1]
StateSet.WILD_CARD // [2]
};

private static final int[] COLORS = {
0xFFAADCF0,
0xFF8A8A8A,
0xFF616161
};

public MaterialRadioButton(Context context) {
super(context);
StateListDrawable drawable = new StateListDrawable();
drawable.addState(CHECKED_STATE_SET, new CheckedDrawable(this));
drawable.addState(StateSet.WILD_CARD, new UncheckedDrawable(this));
drawable.setEnterFadeDuration(300);
drawable.setExitFadeDuration(300);
setButtonDrawable(drawable);
setButtonTintList(new ColorStateList(ENABLED_CHECKED_STATES, COLORS));
}

private static class CheckedDrawable extends MaterialDrawable {

private final float mRadius;

CheckedDrawable(View view) {
mRadius = view.dp(4);
}

@Override
public void draw(@Nonnull Canvas canvas) {
final Rect r = getBounds();
float cx = r.exactCenterX();
float cy = r.exactCenterY();
Paint paint = Paint.obtain();
paint.setColor(mColor);
paint.setAlpha(ShapeDrawable.modulateAlpha(paint.getAlpha(), mAlpha));
if (paint.getAlpha() != 0) {
canvas.drawCircle(cx, cy, mRadius, paint);
paint.setStyle(Paint.STROKE);
paint.setStrokeWidth(mRadius * 0.5f);
canvas.drawCircle(cx, cy, mRadius * 1.6f, paint);
}
paint.recycle();
}

@Override
public int getIntrinsicWidth() {
// 24dp
return (int) (mRadius * 6);
}

@Override
public int getIntrinsicHeight() {
// 24dp
return (int) (mRadius * 6);
}
}

private static class UncheckedDrawable extends MaterialDrawable {

private final float mRadius;

UncheckedDrawable(View view) {
mRadius = view.dp(4);
}

@Override
public void draw(@Nonnull Canvas canvas) {
final Rect r = getBounds();
float cx = r.exactCenterX();
float cy = r.exactCenterY();
Paint paint = Paint.obtain();
paint.setColor(mColor);
paint.setAlpha(ShapeDrawable.modulateAlpha(paint.getAlpha(), mAlpha));
if (paint.getAlpha() != 0) {
paint.setStyle(Paint.STROKE);
paint.setStrokeWidth(mRadius * 0.5f);
canvas.drawCircle(cx, cy, mRadius * 1.6f, paint);
}
paint.recycle();
}

@Override
public int getIntrinsicWidth() {
// 24dp
return (int) (mRadius * 6);
}

@Override
public int getIntrinsicHeight() {
// 24dp
return (int) (mRadius * 6);
}
SystemTheme.currentTheme().applyRadioButtonStyle(this);
}
}
Loading

0 comments on commit 83dc3bf

Please sign in to comment.