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

Commit

Permalink
Merge pull request #25 from sys1yagi/support_set_drawable_object
Browse files Browse the repository at this point in the history
Support set drawable object
  • Loading branch information
ceryle authored Oct 21, 2017
2 parents 143c264 + e0e4a77 commit 2c327fc
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,22 @@
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class SegmentedButton extends View {
Expand All @@ -56,8 +61,6 @@ public SegmentedButton(Context context, AttributeSet attrs, int defStyleAttr) {
private float mClipAmount;
private boolean clipLeftToRight;

private Paint mBitmapPaint;

private TextPaint mTextPaint;
private StaticLayout mStaticLayout, mStaticLayoutOverlay;
private Rect mTextBounds = new Rect();
Expand Down Expand Up @@ -123,42 +126,23 @@ else if (null != textTypeface) {
}

private void initBitmap() {
if (!hasDrawable)
return;

mBitmap = BitmapFactory.decodeResource(context.getResources(), drawable);
if (hasDrawableWidth || hasDrawableHeight)
mBitmap = getResizedBitmap(mBitmap, drawableWidth, drawableHeight);

mBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
if (hasDrawable) {
mDrawable = ContextCompat.getDrawable(context, drawable);
}

if (hasDrawableTint) {
mBitmapNormalColor = new PorterDuffColorFilter(drawableTint, PorterDuff.Mode.SRC_IN);
mBitmapPaint.setColorFilter(mBitmapNormalColor);
}

if (hasDrawableTintOnSelection)
mBitmapClipColor = new PorterDuffColorFilter(drawableTintOnSelection, PorterDuff.Mode.SRC_IN);
}

public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();

float scaleWidth = hasDrawableWidth ? ((float) newWidth) / width : 1;
float scaleHeight = hasDrawableHeight ? ((float) newHeight) / height : 1;

Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);

return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
}


private void measureTextWidth(int width) {
if (!hasText)
return;

int bitmapWidth = hasDrawable && drawableGravity.isHorizontal() ? mBitmap.getWidth() : 0;
int bitmapWidth = hasDrawable && drawableGravity.isHorizontal() ? mDrawable.getIntrinsicWidth() : 0;

int textWidth = width - (bitmapWidth + getPaddingLeft() + getPaddingRight());

Expand All @@ -177,11 +161,11 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightRequirement = MeasureSpec.getSize(heightMeasureSpec);

int width = 0;
int bitmapWidth = hasDrawable ? mBitmap.getWidth() : 0;
int bitmapWidth = hasDrawable ? mDrawable.getIntrinsicWidth() : 0;
int textWidth = hasText ? mStaticLayout.getWidth() : 0;

int height = getPaddingTop() + getPaddingBottom();
int bitmapHeight = hasDrawable ? mBitmap.getHeight() : 0;
int bitmapHeight = hasDrawable ? mDrawable.getIntrinsicHeight() : 0;
int textHeight = hasText ? mStaticLayout.getHeight() : 0;

switch (widthMode) {
Expand Down Expand Up @@ -265,8 +249,8 @@ private void calculate(int width, int height) {

float bitmapHeight = 0, bitmapWidth = 0;
if (hasDrawable) {
bitmapHeight = mBitmap.getHeight();
bitmapWidth = mBitmap.getWidth();
bitmapHeight = mDrawable.getIntrinsicHeight();
bitmapWidth = mDrawable.getIntrinsicWidth();
}


Expand Down Expand Up @@ -336,7 +320,7 @@ private void calculate(int width, int height) {

private PorterDuffColorFilter mBitmapNormalColor, mBitmapClipColor;

private Bitmap mBitmap;
private Drawable mDrawable;

@Override
protected void onDraw(Canvas canvas) {
Expand Down Expand Up @@ -372,8 +356,7 @@ protected void onDraw(Canvas canvas) {

// Bitmap normal
if (hasDrawable) {
mBitmapPaint.setColorFilter(mBitmapNormalColor);
canvas.drawBitmap(mBitmap, bitmap_X, bitmap_Y, mBitmapPaint);
drawDrawableWithColorFilter(canvas, mBitmapNormalColor);
}
// NORMAL -end

Expand All @@ -399,15 +382,29 @@ protected void onDraw(Canvas canvas) {

// Bitmap clip
if (hasDrawable) {
if (hasDrawableTintOnSelection)
mBitmapPaint.setColorFilter(mBitmapClipColor);
canvas.drawBitmap(mBitmap, bitmap_X, bitmap_Y, mBitmapPaint);
drawDrawableWithColorFilter(canvas, mBitmapClipColor);
}
// CLIP -end

canvas.restore();
}

private void drawDrawableWithColorFilter(Canvas canvas, ColorFilter colorFilter){
int drawableX = (int)bitmap_X;
int drawableY = (int)bitmap_Y;
int drawableWidth = mDrawable.getIntrinsicWidth();
if (hasDrawableWidth) {
drawableWidth = this.drawableWidth;
}
int drawableHeight = mDrawable.getIntrinsicHeight();
if (hasDrawableHeight) {
drawableHeight = this.drawableHeight;
}
mDrawable.setColorFilter(colorFilter);
mDrawable.setBounds(drawableX, drawableY, drawableX + drawableWidth, drawableY + drawableHeight);
mDrawable.draw(canvas);
}

public void clipToLeft(float clip) {
clipLeftToRight = false;
mClipAmount = 1.0f - clip;
Expand Down Expand Up @@ -557,8 +554,18 @@ public boolean isHorizontal() {
* @param resId is your drawable's resource id
*/
public void setDrawable(int resId) {
drawable = resId;
mBitmap = BitmapFactory.decodeResource(context.getResources(), resId);
setDrawable(ContextCompat.getDrawable(context, resId));
}

/**
* Sets button's drawable by given drawable object and its position
*
* @param drawable is your drawable object
*/
public void setDrawable(Drawable drawable){
mDrawable = drawable;
hasDrawable = true;
requestLayout();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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 2c327fc

Please sign in to comment.