From d5a5377a34585d7d5a4cfe3897764a3b5d8be019 Mon Sep 17 00:00:00 2001 From: milind Date: Sat, 28 Dec 2019 11:28:15 +0530 Subject: [PATCH] New prepare --- .../app/trymosaic/customView1/DataModel.java | 10 +++- .../trymosaic/customView1/MainActivity.java | 27 +++++++---- mosaic/build.gradle | 2 + .../matrixdev/mosaic/ItemChooseInterface.java | 2 +- .../java/com/matrixdev/mosaic/MosaicView.java | 47 ++++++++++++++++++- .../com/matrixdev/mosaic/UrlContainer.java | 7 +++ 6 files changed, 81 insertions(+), 14 deletions(-) create mode 100644 mosaic/src/main/java/com/matrixdev/mosaic/UrlContainer.java diff --git a/app/src/main/java/com/app/trymosaic/customView1/DataModel.java b/app/src/main/java/com/app/trymosaic/customView1/DataModel.java index e9cee80..7795c5d 100644 --- a/app/src/main/java/com/app/trymosaic/customView1/DataModel.java +++ b/app/src/main/java/com/app/trymosaic/customView1/DataModel.java @@ -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; @@ -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){ @@ -81,4 +82,9 @@ public void setBitmap(Bitmap bitmap) { public Bitmap toBitmap() { return bitmap; } + + @Override + public String toUrl() { + return url; + } } diff --git a/app/src/main/java/com/app/trymosaic/customView1/MainActivity.java b/app/src/main/java/com/app/trymosaic/customView1/MainActivity.java index d9e787b..30ecd1f 100644 --- a/app/src/main/java/com/app/trymosaic/customView1/MainActivity.java +++ b/app/src/main/java/com/app/trymosaic/customView1/MainActivity.java @@ -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() { + @Override + public void itemChoose(DataModel model) { Toast.makeText(MainActivity.this, model.getName(), Toast.LENGTH_SHORT).show(); - }); - - }; - - for(DataModel model : models){ - model.load(this,onImageLoad); - } - + } + }); } diff --git a/mosaic/build.gradle b/mosaic/build.gradle index 08abf0d..0d3c153 100644 --- a/mosaic/build.gradle +++ b/mosaic/build.gradle @@ -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' + } diff --git a/mosaic/src/main/java/com/matrixdev/mosaic/ItemChooseInterface.java b/mosaic/src/main/java/com/matrixdev/mosaic/ItemChooseInterface.java index 87fd0b3..b64d2f3 100644 --- a/mosaic/src/main/java/com/matrixdev/mosaic/ItemChooseInterface.java +++ b/mosaic/src/main/java/com/matrixdev/mosaic/ItemChooseInterface.java @@ -2,5 +2,5 @@ public interface ItemChooseInterface { - public void itemChoose(T genericObject); + public void itemChoose(T model); } diff --git a/mosaic/src/main/java/com/matrixdev/mosaic/MosaicView.java b/mosaic/src/main/java/com/matrixdev/mosaic/MosaicView.java index c4d5246..7aa8398 100644 --- a/mosaic/src/main/java/com/matrixdev/mosaic/MosaicView.java +++ b/mosaic/src/main/java/com/matrixdev/mosaic/MosaicView.java @@ -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; @@ -54,6 +58,7 @@ public class MosaicView extends View { private int topDistance; private int bottomDistance; private int currentPosY; + private Bitmap placeholder; ArrayList bitmapObjectClasses = new ArrayList<>(); int totalCompanies = -1; @@ -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); // @@ -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); } @@ -329,6 +337,7 @@ public void onFinish() { } }); + // touchHandler.setOnTouchListener(new OnTouchListener() { // @Override // public boolean onTouch(View view, MotionEvent motionEvent) { @@ -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()); @@ -542,7 +559,6 @@ public void prepare(ArrayList 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(); @@ -553,4 +569,33 @@ public void prepare(ArrayList objects,ItemChooseIn totalCompanies = objects.size(); invalidate(); } + + public void prepareFromUrl(ArrayList objects,ItemChooseInterface 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() { + @Override + public void onResourceReady(Bitmap resource, Transition 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(); + } } \ No newline at end of file diff --git a/mosaic/src/main/java/com/matrixdev/mosaic/UrlContainer.java b/mosaic/src/main/java/com/matrixdev/mosaic/UrlContainer.java new file mode 100644 index 0000000..edcd297 --- /dev/null +++ b/mosaic/src/main/java/com/matrixdev/mosaic/UrlContainer.java @@ -0,0 +1,7 @@ +package com.matrixdev.mosaic; + +import android.graphics.Bitmap; + +public interface UrlContainer { + public String toUrl(); +}