Skip to content
This repository has been archived by the owner on Mar 27, 2022. It is now read-only.

Commit

Permalink
Add a sample code that dynamically sets drawable to SegmentedButton.
Browse files Browse the repository at this point in the history
  • Loading branch information
sys1yagi committed Sep 26, 2017
1 parent dad7194 commit 718d2c2
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 2 deletions.
@@ -1,12 +1,15 @@
package co.ceryle.segmentedbutton.sample;

import android.graphics.Color;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import co.ceryle.segmentedbutton.SegmentedButton;
import co.ceryle.segmentedbutton.SegmentedButtonGroup;
import co.ceryle.segmentedbutton.sample.drawable.BadgeDrawable;

public class MainActivity extends AppCompatActivity {

Expand Down Expand Up @@ -51,9 +54,31 @@ public void run() {
}
};
handler.postDelayed(runnable, 5000);

setupDynamicDrawables();
}

private void setupDynamicDrawables() {
final BadgeDrawable drawable = new BadgeDrawable(Color.RED, 80, 50, 3, 3);
final SegmentedButton leftButton = (SegmentedButton) findViewById(R.id.left_button);
leftButton.setDrawable(drawable);

SegmentedButtonGroup group = (SegmentedButtonGroup)findViewById(R.id.dynamic_drawable_group);
group.setOnClickedButtonListener(new SegmentedButtonGroup.OnClickedButtonListener() {
@Override
public void onClickedButton(int position) {
if(position == 0){
drawable.setCount(drawable.getCount() + 1);
leftButton.requestLayout();
}
}
});

final SegmentedButton rightButton = (SegmentedButton) findViewById(R.id.right_button);
rightButton.setDrawable(R.drawable.ic_b1);
}

private void updateButton(int position) {
button.setText("Position: " + position);
}
}
}
@@ -0,0 +1,101 @@
package co.ceryle.segmentedbutton.sample.drawable;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;

public class BadgeDrawable extends Drawable {

private Paint paint;
private int color;
private int width;
private int height;
private int borderWidth;
private int borderRadius;

private RectF rect;
private Path path;
private int count = 10;

public BadgeDrawable(int color, int width, int height, int borderWidth, int borderRadius) {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(32);

path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);

rect = new RectF();

this.color = color;
this.width = width;
this.height = height;
this.borderWidth = borderWidth;
this.borderRadius = borderRadius;
}

@Override
public int getIntrinsicWidth() {
return width;
}

@Override
public int getIntrinsicHeight() {
return height;
}

@Override
protected void onBoundsChange(Rect bounds) {
path.reset();

path.addRect(bounds.left, bounds.top, bounds.right, bounds.bottom, Path.Direction.CW);
rect.set(bounds.left + borderWidth, bounds.top + borderWidth,
bounds.right - borderWidth, bounds.bottom - borderWidth);
path.addRoundRect(rect, borderRadius, borderRadius, Path.Direction.CW);
}

@Override
public void draw(@NonNull Canvas canvas) {
paint.setColor(color);
canvas.drawPath(path, paint);

Rect textBounds = new Rect();
String countString = String.valueOf(count);
paint.getTextBounds(countString, 0, countString.length(), textBounds);
canvas.drawText(
countString,
rect.right - (rect.right - rect.left) / 2 - textBounds.width() / 2,
rect.top + textBounds.height() / 2 + (rect.bottom - rect.top) / 2,
paint
);
}

@Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}

@Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
}

@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}
}
46 changes: 45 additions & 1 deletion sample/src/main/res/layout/activity_main.xml
Expand Up @@ -393,5 +393,49 @@

</co.ceryle.segmentedbutton.SegmentedButtonGroup>

<TextView
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textSize="12sp"
android:text="Using dynamic drawable:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>
<co.ceryle.segmentedbutton.SegmentedButtonGroup
android:id="@+id/dynamic_drawable_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:elevation="2dp"
app:sbg_animateSelector="fastOutSlowIn"
app:sbg_animateSelectorDuration="1000"
app:sbg_backgroundColor="@color/black"
app:sbg_position="0"
app:sbg_radius="2dp"
app:sbg_rippleColor="@color/white"
app:sbg_selectorColor="@color/white">

<co.ceryle.segmentedbutton.SegmentedButton
android:id="@+id/left_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:sb_drawablePadding="8dp"
app:sb_drawableGravity="right"
app:sb_text="Left"
app:sb_drawableTint="@color/white"
app:sb_drawableTint_onSelection="@color/black"/>

<co.ceryle.segmentedbutton.SegmentedButton
android:id="@+id/right_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:sb_drawableGravity="right"
app:sb_text="Right"
app:sb_drawableTint="@color/white"
app:sb_drawableTint_onSelection="@color/black"/>

</co.ceryle.segmentedbutton.SegmentedButtonGroup>
</LinearLayout>

0 comments on commit 718d2c2

Please sign in to comment.