Skip to content

Commit

Permalink
click sound, attr booleans added
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitjain240595 committed Dec 28, 2019
1 parent 95faea1 commit 9782788
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 70 deletions.
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
android:background="@color/colorAccent"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:scrollbars="none"
android:id="@+id/vScroll">

<com.matrixdev.mosaic.HScroll
android:id="@+id/hScroll"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent">

Expand All @@ -24,10 +26,8 @@

<com.matrixdev.mosaic.MosaicView
android:id="@+id/mosaic"
app:circle_background="@android:color/black"
app:radius_dimen="5dp"
android:background="@color/colorblue"
app:rectangle_background="@android:color/black"
android:layout_width="2000dp"
android:layout_height="2000dp"/>

Expand Down
115 changes: 71 additions & 44 deletions mosaic/src/main/java/com/matrixdev/mosaic/MosaicView.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public class MosaicView extends View {
private float mx;
private float my;

private boolean isRoundedEdges;
private boolean isFadeEnabled;
private boolean isShrinkEnabled;


public MosaicView(Context context, AttributeSet attrs) {
super(context, attrs);

Expand All @@ -81,6 +86,10 @@ public MosaicView(Context context, AttributeSet attrs) {

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);

attributes.recycle();
}

Expand All @@ -107,23 +116,31 @@ public void drawRectCenter(Canvas canvas, BitmapObjectClass bitmapObjectClass, i
} else {
percX = (dist * 100) / leftDistance;
}
int h = (height * percX) / 100;
height = height - ((h * percX) / 100);
int w = (width * percX) / 100;
width = width - ((w * percX) / 100);
alphaPaint.setAlpha(alphaPaint.getAlpha() - Math.abs((150 * percX) / 100));
if(isShrinkEnabled){
int h = (height * percX) / 100;
height = height - ((h * percX) / 100);
int w = (width * percX) / 100;
width = width - ((w * percX) / 100);
}
if(isFadeEnabled){
alphaPaint.setAlpha(alphaPaint.getAlpha() - Math.abs((150 * percX) / 100));
}
} else { //right
int dist = midX - pivotX;
if (dist > rightDistance) {
percX = 100;
} else {
percX = (dist * 100) / rightDistance;
}
int h = (height * percX) / 100;
height = height - ((h * percX) / 100);
int w = (width * percX) / 100;
width = width - ((w * percX) / 100);
alphaPaint.setAlpha(alphaPaint.getAlpha() - Math.abs((150 * percX) / 100));
if(isShrinkEnabled){
int h = (height * percX) / 100;
height = height - ((h * percX) / 100);
int w = (width * percX) / 100;
width = width - ((w * percX) / 100);
}
if(isFadeEnabled){
alphaPaint.setAlpha(alphaPaint.getAlpha() - Math.abs((150 * percX) / 100));
}
}

int pivotY = currentPosY + (centerY - y);
Expand All @@ -135,23 +152,31 @@ public void drawRectCenter(Canvas canvas, BitmapObjectClass bitmapObjectClass, i
} else {
percY = (dist * 100) / topDistance;
}
int h = (height * percY) / 100;
height = height - ((h * percY) / 100);
int w = (width * percY) / 100;
width = width - ((w * percY) / 100);
alphaPaint.setAlpha(alphaPaint.getAlpha() - Math.abs((150 * percY) / 100));
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;
if (dist > bottomDistance) {
percY = 100;
} else {
percY = (dist * 100) / bottomDistance;
}
int h = (height * percY) / 100;
height = height - ((h * percY) / 100);
int w = (width * percY) / 100;
width = width - ((w * percY) / 100);
alphaPaint.setAlpha(alphaPaint.getAlpha() - Math.abs((150 * percY) / 100));
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));
}
}

RectF rect = new RectF();
Expand Down Expand Up @@ -180,33 +205,14 @@ private void setBitmapClassCoordinates(BitmapObjectClass bitmapObjectClass, int
bitmapObjectClass.setBottom(bottom);
}

@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
Log.d("-----Down", "down");
touchX = motionEvent.getX();
touchY = motionEvent.getY();

timer = new Handler();
timer.postDelayed(new Runnable() {
@Override
public void run() {
//todo use touchx and touchy

}
}, 300);
}
return super.onTouchEvent(motionEvent);

}

private void handleClick() {
for (int i = 0; i < bitmapObjectClasses.size(); i++) {
if (bitmapObjectClasses.get(i).getLeft() <= touchX &&
bitmapObjectClasses.get(i).getRight() >= touchX &&
bitmapObjectClasses.get(i).getTop() <= touchY &&
bitmapObjectClasses.get(i).getBottom() >= touchY ) {

playSoundEffect(android.view.SoundEffectConstants.CLICK);
BitmapObjectClass data = bitmapObjectClasses.get(i);
if(onItemChooseListener!=null)
onItemChooseListener.itemChoose(data.getGenericObject());
Expand Down Expand Up @@ -356,6 +362,14 @@ private Bitmap addRoundCorners(Bitmap bmp, int padding_y) {
return bmpWithBorder;
}

public Bitmap pad(Bitmap Src, int padding_y) {
Bitmap outputimage = Bitmap.createBitmap(Src.getWidth(),Src.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
Canvas can = new Canvas(outputimage);
// can.drawColor(context.getResources().getColor(R.color.colorAccent)); //This represents White color
can.drawBitmap(Src, 0, padding_y, null);
return outputimage;
}

private int dpToPx(float dp) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
float fpixels = metrics.density * dp;
Expand Down Expand Up @@ -487,7 +501,12 @@ public <T extends BitmapContainer>void prepare(ArrayList<T> objects,ItemChooseIn
if(i>=random.size()){
random.add(new Random().nextInt(70));
}
Bitmap bitmap = addRoundCorners(objects.get(i).toBitmap(), random.get(i));
Bitmap bitmap;
if(isRoundedEdges){
bitmap = addRoundCorners(objects.get(i).toBitmap(), random.get(i));
}else{
bitmap=pad(objects.get(i).toBitmap(),random.get(i));
}
BitmapObjectClass bitmapObjectClass = new BitmapObjectClass();

bitmapObjectClass.setBitmap(bitmap);
Expand All @@ -507,16 +526,24 @@ public <T extends UrlContainer>void prepareFromUrl(ArrayList<T> objects,ItemChoo
}
final BitmapObjectClass bitmapObjectClass = new BitmapObjectClass();
final int finalI = i;
bitmapObjectClass.setBitmap(addRoundCorners(placeholder, random.get(i)));
if(isRoundedEdges){
bitmapObjectClass.setBitmap(addRoundCorners(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);
Bitmap rounded = addRoundCorners(resource, random.get(finalI));
bitmapObjectClass.setBitmap(rounded);
if(isRoundedEdges){
Bitmap rounded = addRoundCorners(resource, random.get(finalI));
bitmapObjectClass.setBitmap(rounded);
}else{
bitmapObjectClass.setBitmap(pad(resource,random.get(finalI)));
}
invalidate();
}
});
Expand Down
28 changes: 4 additions & 24 deletions mosaic/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MosaicView">
<attr name="rectangle_background" format="color" />
<attr name="circle_background" format="color" />
<attr name="radius_dimen" format="dimension" />
<attr name="isRoundedEdges" format="boolean"/>
<attr name="isFadeEnabled" format="boolean"/>
<attr name="isShrinkEnabled" format="boolean"/>

</declare-styleable>

<integer-array name="random_imgs">
<item>@drawable/company_logo</item>
<item>@drawable/company_logo1</item>
<item>@drawable/company_logo2</item>
<item>@drawable/company_logo3</item>
<item>@drawable/company_logo4</item>
<item>@drawable/company_logo6</item>
<item>@drawable/company_logo7</item>
<item>@drawable/company_logo8</item>
<item>@drawable/company_logo9</item>
<item>@drawable/company_logo10</item>
<item>@drawable/company_logo11</item>
<item>@drawable/company_logo12</item>
<item>@drawable/company_logo13</item>
<item>@drawable/company_logo14</item>
<item>@drawable/company_logo15</item>
<item>@drawable/company_logo16</item>
<item>@drawable/company_logo17</item>
<item>@drawable/company_logo18</item>
<item>@drawable/company_logo19</item>
<item>@drawable/company_logo20</item>
</integer-array>
</resources>

0 comments on commit 9782788

Please sign in to comment.