Skip to content

Commit

Permalink
New prepare
Browse files Browse the repository at this point in the history
  • Loading branch information
milindrc committed Dec 28, 2019
1 parent 7e0c885 commit d5a5377
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 14 deletions.
10 changes: 8 additions & 2 deletions app/src/main/java/com/app/trymosaic/customView1/DataModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
import com.bumptech.glide.request.transition.Transition;
import com.matrixdev.mosaic.BitmapContainer;
import com.matrixdev.mosaic.MosaicView;
import com.matrixdev.mosaic.UrlContainer;

public class DataModel implements BitmapContainer {
public class DataModel implements BitmapContainer, UrlContainer {
String name;
String url;
Bitmap bitmap;
Expand All @@ -21,7 +22,7 @@ public DataModel(String name, String url) {
this.name = name;
this.url = url;

bitmap = textAsBitmap(name, 48*3,Color.BLACK);
bitmap = textAsBitmap(name, 40*3,Color.BLACK);
}

public void load(Activity activity, final Runnable onComplete){
Expand Down Expand Up @@ -81,4 +82,9 @@ public void setBitmap(Bitmap bitmap) {
public Bitmap toBitmap() {
return bitmap;
}

@Override
public String toUrl() {
return url;
}
}
27 changes: 17 additions & 10 deletions app/src/main/java/com/app/trymosaic/customView1/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,24 @@ private void initUI() {
add(new DataModel("HANDS ON TRADES PRIVATE LIMTED","https://s3-ap-southeast-1.amazonaws.com/maxwholesale/company_brands/5bbca40a5a3f1b6954b89096/companyLogo.jpg"));
}};

onImageLoad = ()->{
binding.mosaic.prepare(models, model -> {
// onImageLoad = ()->{
// binding.mosaic.prepare(models, model -> {
// Toast.makeText(MainActivity.this, model.getName(), Toast.LENGTH_SHORT).show();
// });
//
// };
//
// for(DataModel model : models){
// model.load(this,onImageLoad);
// }


binding.mosaic.prepareFromUrl(models, new ItemChooseInterface<DataModel>() {
@Override
public void itemChoose(DataModel model) {
Toast.makeText(MainActivity.this, model.getName(), Toast.LENGTH_SHORT).show();
});

};

for(DataModel model : models){
model.load(this,onImageLoad);
}

}
});

}

Expand Down
2 changes: 2 additions & 0 deletions mosaic/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.github.bumptech.glide:glide:4.9.0'

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface ItemChooseInterface <T>{

public void itemChoose(T genericObject);
public void itemChoose(T model);
}
47 changes: 46 additions & 1 deletion mosaic/src/main/java/com/matrixdev/mosaic/MosaicView.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import android.widget.Toast;


import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import java.util.ArrayList;
import java.util.Random;

Expand All @@ -54,6 +58,7 @@ public class MosaicView extends View {
private int topDistance;
private int bottomDistance;
private int currentPosY;
private Bitmap placeholder;

ArrayList<BitmapObjectClass> bitmapObjectClasses = new ArrayList<>();
int totalCompanies = -1;
Expand All @@ -72,6 +77,8 @@ public MosaicView(Context context, AttributeSet attrs) {
super(context, attrs);

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

TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.MosaicView);
// draw = context.getResources().obtainTypedArray(R.array.random_imgs);
//
Expand All @@ -94,6 +101,7 @@ public MosaicView(Context context, AttributeSet attrs) {

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

}

Expand Down Expand Up @@ -329,6 +337,7 @@ public void onFinish() {
}
});


// touchHandler.setOnTouchListener(new OnTouchListener() {
// @Override
// public boolean onTouch(View view, MotionEvent motionEvent) {
Expand Down Expand Up @@ -387,6 +396,14 @@ public void onFinish() {

}

public static Bitmap createPlaceholder(int width, int height, int color) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(color);
canvas.drawRect(0F, 0F, (float) width, (float) height, paint);
return bitmap;
}

private Bitmap addRoundCorners(Bitmap bmp, int padding_y) {
Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight() + padding_y, bmp.getConfig());
Expand Down Expand Up @@ -542,7 +559,6 @@ public <T extends BitmapContainer>void prepare(ArrayList<T> objects,ItemChooseIn
if(i>=random.size()){
random.add(new Random().nextInt(70));
}
Log.d("-----RANDOM", "" + random);
Bitmap bitmap = addRoundCorners(objects.get(i).toBitmap(), random.get(i));
BitmapObjectClass bitmapObjectClass = new BitmapObjectClass();

Expand All @@ -553,4 +569,33 @@ public <T extends BitmapContainer>void prepare(ArrayList<T> objects,ItemChooseIn
totalCompanies = objects.size();
invalidate();
}

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()){
random.add(new Random().nextInt(70));
}
final BitmapObjectClass bitmapObjectClass = new BitmapObjectClass();
final int finalI = i;
bitmapObjectClass.setBitmap(addRoundCorners(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);
invalidate();
}
});
bitmapObjectClass.setGenericObject(objects.get(i));
bitmapObjectClasses.add(bitmapObjectClass);
}
totalCompanies = objects.size();
invalidate();
}
}
7 changes: 7 additions & 0 deletions mosaic/src/main/java/com/matrixdev/mosaic/UrlContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.matrixdev.mosaic;

import android.graphics.Bitmap;

public interface UrlContainer {
public String toUrl();
}

0 comments on commit d5a5377

Please sign in to comment.