package com.jeroenmols.legofy;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {

    private static final int GRANULARITY = 64;
    private ImageView image;
    private SeekBar seekbar;
    private SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            Bitmap legoFiedBitmap = createLegoFiedBitmap(progress, true);
            image.setImageBitmap(legoFiedBitmap);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            //NOP
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            //NOP
        }
    };
    ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        image = (ImageView) findViewById(R.id.imageview_main);
        seekbar = (SeekBar) findViewById(R.id.seekbar_main_bricksize);
        seekbar.setProgress(GRANULARITY);
        seekbar.setOnSeekBarChangeListener(onSeekBarChangeListener);
        Button button = (Button) findViewById(R.id.button_main_highres);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap legoFiedBitmap = createLegoFiedBitmap(seekbar.getProgress(), false);
                image.setImageBitmap(legoFiedBitmap);
            }
        });

        Bitmap legofied = createLegoFiedBitmap(seekbar.getProgress(), true);
        image.setImageBitmap(legofied);
    }

    @NonNull
    private Bitmap createLegoFiedBitmap(int granularity, boolean fastmode) {
        if (granularity <= 0) {
            granularity = 1;
        }

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.profile_picture);
        Bitmap brick = BitmapFactory.decodeResource(getResources(), R.drawable.brick);
        Bitmap scaledBrick = Bitmap.createScaledBitmap(brick, granularity, granularity, false);

        int imageWidth = bitmap.getWidth() / granularity;
        int imageHeight = bitmap.getHeight() / granularity;
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, imageWidth, imageHeight, true);

        Bitmap legofied = Bitmap.createBitmap(imageWidth * granularity, imageHeight * granularity, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(legofied);
        Paint paint = new Paint();

        for (int i = 0; i < scaledBitmap.getWidth(); i++) {
            for (int j = 0; j < scaledBitmap.getHeight(); j++) {
                if (fastmode) {
                    paint.setColorFilter(new PorterDuffColorFilter(scaledBitmap.getPixel(i, j), PorterDuff.Mode.OVERLAY));
                } else {
                    paint.setColorFilter(new PorterDuffColorFilter(getColor(bitmap, i, j, granularity), PorterDuff.Mode.OVERLAY));
                }
                canvas.drawBitmap(scaledBrick, i * granularity, j * granularity, paint);
            }
        }
        return legofied;
    }

    public int getColor(Bitmap source, int x, int y, int granularity) {
        int red = 0;
        int green = 0;
        int blue = 0;
        for (int i = 0; i < granularity; i++) {
            for (int j = 0; j < granularity; j++) {
                int color = source.getPixel(granularity * x + i, granularity * y + j);
                red += Color.red(color);
                green += Color.green(color);
                blue += Color.blue(color);
            }
        }
        return Color.rgb(red / (granularity * granularity), green / (granularity * granularity), blue / (granularity * granularity));
    }
}


@@ -115,237 +115,17 @@
 oid.
-graphics.Canvas;%0Aimport android.graphics.Color;%0Aimport android.graphics.Paint;%0Aimport android.graphics.PorterDuff;%0Aimport android.graphics.PorterDuffColorFilter;%0Aimport android.os.Bundle;%0Aimport android.support.annotation.NonNull
+os.Bundle
 ;%0Aim
@@ -692,110 +692,322 @@
 map 
-legoFiedBitmap = createLegoFiedBitmap(progress, true);%0A            image.setImageBitmap(legoFiedBitmap
+bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.profile_picture);%0A            LegofyDrawable legofyDrawable = new LegofyDrawable(getResources(), bitmap);%0A            image.setImageDrawable(legofyDrawable);%0A            legofyDrawable.createLegoFiedBitmap(getResources(), seekbar.getProgress(), true
 );%0A 
@@ -1878,128 +1878,88 @@
 map 
-legoFiedBitmap = createLegoFiedBitmap(seekbar.getProgress(), false);%0A                image.setImageBitmap(legoFiedBitmap
+bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.profile_picture
 );%0A 
@@ -1973,100 +1973,96 @@
     
-%7D%0A
     
-    %7D);%0A%0A        Bitmap legofied = createLegoFiedBitmap(seekbar.getProgr
+LegofyDrawable legofyDrawable = new LegofyDrawable(getResourc
 es
-s
 (), 
-true);%0A
+bitmap);%0A        
     
@@ -2083,183 +2083,146 @@
 mage
-Bitmap(legofied
+Drawable(legofyDrawable
 );%0A    
-%7D%0A%0A
     
-@NonNull%0A    private Bitmap createLegoFiedBitmap(int granularity, boolean fastmode) %7B%0A        if (granularity %3C= 0) %7B%0A            granularity = 1;
+        legofyDrawable.createLegoFiedBitmap(getResources(), seekbar.getProgress(), false);%0A            %7D
 %0A   
@@ -2227,26 +2227,30 @@
        %7D
-%0A%0A
+);%0A%0A//
         
 Bitmap b
@@ -2241,32 +2241,37 @@
         
-Bitmap b
+image.setImageB
 itmap
- = 
+(
 BitmapFa
@@ -2326,32 +2326,33 @@
 profile_picture)
+)
 ;%0A        Bitmap
@@ -2349,23 +2349,59 @@
     
-Bitmap brick = 
+EffectDrawable customDrawable = new EffectDrawable(
 Bitm
@@ -2456,112 +2456,26 @@
 ble.
-brick);%0A        Bitmap scaledBrick = Bitmap.createScaledBitmap(brick, granularity, granularity, false);%0A
+profile_picture));
 %0A   
@@ -2483,1629 +2483,123 @@
     
-int 
 image
-Width = bitmap.getWidth() / granularity;%0A        int imageHeight = bitmap.getHeight() / granularity;%0A        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, imageWidth, imageHeight, true);%0A%0A        Bitmap legofied = Bitmap.createBitmap(imageWidth * granularity, imageHeight * granularity, Bitmap.Config.ARGB_8888);%0A        Canvas canvas = new Canvas(legofied);%0A        Paint paint = new Paint();%0A%0A        for (int i = 0; i %3C scaledBitmap.getWidth(); i++) %7B%0A            for (int j = 0; j %3C scaledBitmap.getHeight(); j++) %7B%0A                if (fastmode) %7B%0A                    paint.setColorFilter(new PorterDuffColorFilter(scaledBitmap.getPixel(i, j), PorterDuff.Mode.OVERLAY));%0A                %7D else %7B%0A                    paint.setColorFilter(new PorterDuffColorFilter(getColor(bitmap, i, j, granularity), PorterDuff.Mode.OVERLAY));%0A                %7D%0A                canvas.drawBitmap(scaledBrick, i * granularity, j * granularity, paint);%0A            %7D%0A        %7D%0A        return legofied;%0A    %7D%0A%0A    public int getColor(Bitmap source, int x, int y, int granularity) %7B%0A        int red = 0;%0A        int green = 0;%0A        int blue = 0;%0A        for (int i = 0; i %3C granularity; i++) %7B%0A            for (int j = 0; j %3C granularity; j++) %7B%0A                int color = source.getPixel(granularity * x + i, granularity * y + j);%0A                red += Color.red(color);%0A                green += Color.green(color);%0A                blue += Color.blue(color);%0A            %7D%0A        %7D%0A        return Color.rgb(red / (granularity * granularity), green / (granularity * granularity), blue / (granularity * granularity
+.setImageDrawable(customDrawable);%0A        customDrawable.applyEffect(new LegofyEffect(getResources(), 64, 300
 ));%0A

Enable effect in Activity