diff --git a/sticker/src/main/java/com/xiaopo/flying/sticker/DrawableSticker.java b/sticker/src/main/java/com/xiaopo/flying/sticker/DrawableSticker.java index 6a74b70..aab7eb6 100644 --- a/sticker/src/main/java/com/xiaopo/flying/sticker/DrawableSticker.java +++ b/sticker/src/main/java/com/xiaopo/flying/sticker/DrawableSticker.java @@ -1,58 +1,97 @@ package com.xiaopo.flying.sticker; +/** + * Mofified by M.Refaat on 3/25/2018. + */ + +import android.content.Context; import android.graphics.Canvas; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.support.annotation.IntRange; import android.support.annotation.NonNull; +import android.support.v4.content.ContextCompat; + +import com.netaq.schoolvoice.R; +import com.xiaopo.flying.sticker.Sticker; /** * @author wupanjie */ public class DrawableSticker extends Sticker { - private Drawable drawable; - private Rect realBounds; - - public DrawableSticker(Drawable drawable) { - this.drawable = drawable; - realBounds = new Rect(0, 0, getWidth(), getHeight()); - } - - @NonNull @Override public Drawable getDrawable() { - return drawable; - } - - @Override public DrawableSticker setDrawable(@NonNull Drawable drawable) { - this.drawable = drawable; - return this; - } - - @Override public void draw(@NonNull Canvas canvas) { - canvas.save(); - canvas.concat(getMatrix()); - drawable.setBounds(realBounds); - drawable.draw(canvas); - canvas.restore(); - } - - @NonNull @Override public DrawableSticker setAlpha(@IntRange(from = 0, to = 255) int alpha) { - drawable.setAlpha(alpha); - return this; - } - - @Override public int getWidth() { - return drawable.getIntrinsicWidth(); - } - - @Override public int getHeight() { - return drawable.getIntrinsicHeight(); - } - - @Override public void release() { - super.release(); - if (drawable != null) { - drawable = null; - } - } + private Drawable drawable; + public Rect realBounds; + private Drawable allocationDrawable; + private Rect allocationBounds; + + public DrawableSticker(Drawable drawable, Context context) { + this.allocationDrawable = drawable; + this.drawable = ContextCompat.getDrawable(context, R.drawable.empty_sticker); // an invisble/empty view to be considered as the mainn drawable + realBounds = new Rect(300, 300, getAllocationWidth(), getAllocationHeight()); + int startX = (getAllocationWidth() / 2) - (getWidth() / 2); + int startY = (getAllocationHeight() / 2) - (getHeight() / 2); + allocationBounds = new Rect(startX - 300, startY - 300, + startX + getWidth() + 300, startY + getHeight() + 300); + } + + @NonNull + @Override + public Drawable getDrawable() { + return drawable; + } + + @Override + public DrawableSticker setDrawable(@NonNull Drawable drawable) { + this.drawable = drawable; + return this; + } + + @Override + public void draw(@NonNull Canvas canvas) { + canvas.save(); + canvas.concat(getMatrix()); + drawable.setBounds(allocationBounds); + drawable.draw(canvas); + canvas.restore(); + + canvas.save(); + canvas.concat(getMatrix()); + allocationDrawable.setBounds(realBounds); + allocationDrawable.draw(canvas); + canvas.restore(); + } + + @NonNull + @Override + public DrawableSticker setAlpha(@IntRange(from = 0, to = 255) int alpha) { + drawable.setAlpha(alpha); + return this; + } + + @Override + public int getWidth() { + return drawable.getIntrinsicWidth(); + } + + @Override + public int getHeight() { + return drawable.getIntrinsicHeight(); + } + + public int getAllocationWidth() { + return allocationDrawable.getIntrinsicWidth(); + } + + public int getAllocationHeight() { + return allocationDrawable.getIntrinsicHeight(); + } + + @Override + public void release() { + super.release(); + if (drawable != null) { + drawable = null; + } + } } diff --git a/sticker/src/main/res/drawable/empty_sticker.png b/sticker/src/main/res/drawable/empty_sticker.png new file mode 100644 index 0000000..f082072 Binary files /dev/null and b/sticker/src/main/res/drawable/empty_sticker.png differ