Skip to content

Commit

Permalink
negative perc Y fix on alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
milindrc committed Dec 30, 2019
1 parent 06ccdb0 commit 7de75e6
Showing 1 changed file with 49 additions and 45 deletions.
94 changes: 49 additions & 45 deletions mosaic/src/main/java/com/matrixdev/mosaic/MosaicView.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,21 @@ public MosaicView(Context context, AttributeSet attrs) {
super(context, attrs);

this.context = context;
placeholder = createPlaceholder(dpToPx(100),dpToPx(100),Color.WHITE);
placeholder = createPlaceholder(dpToPx(100), dpToPx(100), Color.WHITE);

TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.MosaicView);

isRoundedEdges = attributes.getBoolean(R.styleable.MosaicView_isRoundedEdges, true);
isFadeEnabled = attributes.getBoolean(R.styleable.MosaicView_isFadeEnabled, true);
isShrinkEnabled = attributes.getBoolean(R.styleable.MosaicView_isShrinkEnabled, true);
defaultColor= attributes.getColor(R.styleable.MosaicView_imageBackground,Color.WHITE);
defaultColor = attributes.getColor(R.styleable.MosaicView_imageBackground, Color.WHITE);

attributes.recycle();
}

public MosaicView(Context context, Drawable mCustomImage) {
super(context);
placeholder = createPlaceholder(dpToPx(100),dpToPx(100),Color.WHITE);
placeholder = createPlaceholder(dpToPx(100), dpToPx(100), Color.WHITE);

}

Expand All @@ -118,13 +118,13 @@ public void drawRectCenter(Canvas canvas, BitmapObjectClass bitmapObjectClass, i
} else {
percX = (dist * 100) / leftDistance;
}
if(isShrinkEnabled){
if (isShrinkEnabled) {
int h = (height * percX) / 100;
height = height - ((h * percX) / 100);
int w = (width * percX) / 100;
width = width - ((w * percX) / 100);
}
if(isFadeEnabled){
if (isFadeEnabled) {
alphaPaint.setAlpha(alphaPaint.getAlpha() - Math.abs((150 * percX) / 100));
}
} else { //right
Expand All @@ -134,13 +134,13 @@ public void drawRectCenter(Canvas canvas, BitmapObjectClass bitmapObjectClass, i
} else {
percX = (dist * 100) / rightDistance;
}
if(isShrinkEnabled){
if (isShrinkEnabled) {
int h = (height * percX) / 100;
height = height - ((h * percX) / 100);
int w = (width * percX) / 100;
width = width - ((w * percX) / 100);
}
if(isFadeEnabled){
if (isFadeEnabled) {
alphaPaint.setAlpha(alphaPaint.getAlpha() - Math.abs((150 * percX) / 100));
}
}
Expand All @@ -154,14 +154,16 @@ public void drawRectCenter(Canvas canvas, BitmapObjectClass bitmapObjectClass, i
} else {
percY = (dist * 100) / topDistance;
}
if(isShrinkEnabled && percY!=0){
int h = (height * percY) / 100;
height = height - ((h * percY) / 100);
int w = (width * percY) / 100;
width = width - ((w * percY) / 100);
}
if(isFadeEnabled){
alphaPaint.setAlpha(alphaPaint.getAlpha() - Math.abs((150 * percY) / 100));
if (percY > 0) {
if (isShrinkEnabled) {
int h = (height * percY) / 100;
height = height - ((h * percY) / 100);
int w = (width * percY) / 100;
width = width - ((w * percY) / 100);
}
if (isFadeEnabled) {
alphaPaint.setAlpha(alphaPaint.getAlpha() - Math.abs((150 * percY) / 100));
}
}
} else { //right
int dist = midY - pivotY;
Expand All @@ -170,14 +172,16 @@ public void drawRectCenter(Canvas canvas, BitmapObjectClass bitmapObjectClass, i
} else {
percY = (dist * 100) / bottomDistance;
}
if(isShrinkEnabled && percY!=0){
int h = (height * percY) / 100;
height = height - ((h * percY) / 100);
int w = (width * percY) / 100;
width = width - ((w * percY) / 100);
}
if(isFadeEnabled){
alphaPaint.setAlpha(alphaPaint.getAlpha() - Math.abs((150 * percY) / 100));
if (percY > 0) {
if (isShrinkEnabled) {
int h = (height * percY) / 100;
height = height - ((h * percY) / 100);
int w = (width * percY) / 100;
width = width - ((w * percY) / 100);
}
if (isFadeEnabled) {
alphaPaint.setAlpha(alphaPaint.getAlpha() - Math.abs((150 * percY) / 100));
}
}
}
// if(bitmapObjectClass.getGenericObject().toString().equals("Cadbury"))
Expand Down Expand Up @@ -214,11 +218,11 @@ private void handleClick() {
if (bitmapObjectClasses.get(i).getLeft() <= touchX &&
bitmapObjectClasses.get(i).getRight() >= touchX &&
bitmapObjectClasses.get(i).getTop() <= touchY &&
bitmapObjectClasses.get(i).getBottom() >= touchY ) {
bitmapObjectClasses.get(i).getBottom() >= touchY) {

playSoundEffect(android.view.SoundEffectConstants.CLICK);
BitmapObjectClass data = bitmapObjectClasses.get(i);
if(onItemChooseListener!=null)
if (onItemChooseListener != null)
onItemChooseListener.itemChoose(data.getGenericObject());
}
}
Expand All @@ -230,7 +234,7 @@ private void init() {
hscroll = ((HScroll) ((LinearLayout) getParent()).getParent());
vScroll = (VScroll) ((HScroll) ((LinearLayout) getParent()).getParent()).getParent();

rootview=(ViewGroup)vScroll.getParent();
rootview = (ViewGroup) vScroll.getParent();
rootview.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
Expand All @@ -248,13 +252,13 @@ public boolean onTouch(View view, MotionEvent event) {
case MotionEvent.ACTION_MOVE:
curX = event.getX();
curY = event.getY();
Log.d("-----Touch",""+touchX+","+touchY+"|"+(curX+hscroll.getScrollX()));
Log.d("-----Touch", "" + touchX + "," + touchY + "|" + (curX + hscroll.getScrollX()));

if(curX+ hscroll.getScrollX()< touchX-10 || curX+ hscroll.getScrollX()>touchX+10){
if (curX + hscroll.getScrollX() < touchX - 10 || curX + hscroll.getScrollX() > touchX + 10) {
touchX = 0;
touchY = 0;
}
if(curY+ vScroll.getScrollY()< touchY-10 || curY+ vScroll.getScrollY()>touchY+10){
if (curY + vScroll.getScrollY() < touchY - 10 || curY + vScroll.getScrollY() > touchY + 10) {
touchX = 0;
touchY = 0;
}
Expand All @@ -264,7 +268,7 @@ public boolean onTouch(View view, MotionEvent event) {
my = curY;
break;
case MotionEvent.ACTION_UP:
if(touchX!=0){
if (touchX != 0) {

handleClick();
touchX = 0;
Expand Down Expand Up @@ -365,7 +369,7 @@ private Bitmap addRoundCorners(Bitmap bmp, int padding_y) {
}

public Bitmap pad(Bitmap Src, int padding_y) {
Bitmap outputimage = Bitmap.createBitmap(Src.getWidth(),Src.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
Bitmap outputimage = Bitmap.createBitmap(Src.getWidth(), Src.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
Expand Down Expand Up @@ -499,18 +503,18 @@ public boolean onSingleTapConfirmed(MotionEvent e) {
}
}

public <T extends BitmapContainer>void prepare(ArrayList<T> objects,ItemChooseInterface<T> onItemChooseListener){
public <T extends BitmapContainer> void prepare(ArrayList<T> objects, ItemChooseInterface<T> onItemChooseListener) {
this.onItemChooseListener = onItemChooseListener;
bitmapObjectClasses.clear();
for (int i = 0; i < objects.size(); i++) {
if(i>=random.size()){
if (i >= random.size()) {
random.add(new Random().nextInt(70));
}
Bitmap bitmap;
if(isRoundedEdges){
if (isRoundedEdges) {
bitmap = addRoundCorners(objects.get(i).toBitmap(), random.get(i));
}else{
bitmap=pad(objects.get(i).toBitmap(),random.get(i));
} else {
bitmap = pad(objects.get(i).toBitmap(), random.get(i));
}
BitmapObjectClass bitmapObjectClass = new BitmapObjectClass();

Expand All @@ -522,32 +526,32 @@ public <T extends BitmapContainer>void prepare(ArrayList<T> objects,ItemChooseIn
invalidate();
}

public <T extends UrlContainer>void prepareFromUrl(ArrayList<T> objects,ItemChooseInterface<T> onItemChooseListener){
public <T extends UrlContainer> void prepareFromUrl(ArrayList<T> objects, ItemChooseInterface<T> onItemChooseListener) {
this.onItemChooseListener = onItemChooseListener;
bitmapObjectClasses.clear();
for (int i = 0; i < objects.size(); i++) {
if(i>=random.size()){
if (i >= random.size()) {
random.add(new Random().nextInt(70));
}
final BitmapObjectClass bitmapObjectClass = new BitmapObjectClass();
final int finalI = i;
if(isRoundedEdges){
if (isRoundedEdges) {
bitmapObjectClass.setBitmap(addRoundCorners(placeholder, random.get(i)));
}else{
bitmapObjectClass.setBitmap(pad(placeholder,random.get(i)));
} else {
bitmapObjectClass.setBitmap(pad(placeholder, random.get(i)));
}
Glide.with(context)
.asBitmap()
.load(objects.get(i).toUrl())
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
Log.d("----","loaded"+finalI);
if(isRoundedEdges){
Log.d("----", "loaded" + finalI);
if (isRoundedEdges) {
Bitmap rounded = addRoundCorners(resource, random.get(finalI));
bitmapObjectClass.setBitmap(rounded);
}else{
bitmapObjectClass.setBitmap(pad(resource,random.get(finalI)));
} else {
bitmapObjectClass.setBitmap(pad(resource, random.get(finalI)));
}
invalidate();
}
Expand Down

0 comments on commit 7de75e6

Please sign in to comment.