This repository has been archived by the owner on Mar 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from sys1yagi/support_set_drawable_object
Support set drawable object
- Loading branch information
Showing
4 changed files
with
217 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
sample/src/main/java/co/ceryle/segmentedbutton/sample/drawable/BadgeDrawable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters