Skip to content

Commit

Permalink
use custom width and height of mask, align in center
Browse files Browse the repository at this point in the history
  • Loading branch information
marek.kovac committed Jan 24, 2018
1 parent 38ff791 commit ae2eaa9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public class MaskableFrameLayout extends FrameLayout {
private Paint mPaint = null;
private PorterDuffXfermode mPorterDuffXferMode = null;

private int maskWidth;
private int maskHeight;

public MaskableFrameLayout(Context context) {
super(context);
}
Expand Down Expand Up @@ -103,12 +106,15 @@ private void construct(Context context, AttributeSet attrs) {
//This can take a performance hit.
mPaint = createPaint(true);
}
maskWidth = a.getDimensionPixelSize(R.styleable.MaskableLayout_maskWidth, -1);
maskHeight = a.getDimensionPixelSize(R.styleable.MaskableLayout_maskHeight, -1);
} finally {
if (a != null) {
a.recycle();
}
}
} else {
}
else {
log("Couldn't load theme, mask in xml won't be loaded.");
}
registerMeasure();
Expand Down Expand Up @@ -151,7 +157,10 @@ private Bitmap makeBitmapMask(@Nullable Drawable drawable) {
Bitmap mask = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mask);
drawable.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
if (maskWidth == -1 || maskHeight == -1)
drawable.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
else
setCustomBoundsToDrawable(drawable);
drawable.draw(canvas);
return mask;
} else {
Expand All @@ -164,6 +173,11 @@ private Bitmap makeBitmapMask(@Nullable Drawable drawable) {
return null;
}

private void setCustomBoundsToDrawable(Drawable d) {
d.setBounds((getMeasuredWidth()/2)-(maskWidth/2),(getMeasuredHeight()/2)-(maskHeight/2),(getMeasuredWidth()/2)-(maskWidth/2)+maskWidth,(getMeasuredHeight()/2)-
(maskHeight/2)+maskHeight);
}

public void setMask(int drawableRes) {
Resources res = getResources();
if (res != null) {
Expand Down
2 changes: 2 additions & 0 deletions library/src/main/res/values/attrs_maskable_framelayout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@
<enum name="XOR" value="17"/>
</attr>
<attr name="anti_aliasing" format="boolean"/>
<attr name="maskWidth" format="dimension"/>
<attr name="maskHeight" format="dimension"/>

This comment has been minimized.

Copy link
@novalsanti20

novalsanti20 Jun 26, 2018

Fotmat

</declare-styleable>
</resources>

0 comments on commit ae2eaa9

Please sign in to comment.