package com.xhg.test.image;

import android.graphics.Bitmap;
import android.os.AsyncTask;

/**
 * Created by xionghg on 17-7-4.
 */

public class ColorHolder {

    private int mHeight;
    private int mWidth;
    private ColorStrategy mStrategy;
    private int[] mColorArray;
    private int mAlpha = 0xff << 24;

    public ColorHolder() {
        this(1024, 1024);
    }

    public ColorHolder(int width, int height) {
        if (width <= 0 || height <= 0) {
            throw new IllegalArgumentException("params not right");
        }
        this.mWidth = width;
        this.mHeight = height;
        mColorArray = new int[width * height];
    }

    public Bitmap createBitmap() {
        return Bitmap.createBitmap(mColorArray, mWidth, mHeight, Bitmap.Config.ARGB_8888);
    }

    public void createColors() {
        //单线程4.12s
        for (int j = 0; j < mHeight; j++) {
            for (int i = 0; i < mWidth; i++) {
                int r = mStrategy.getRed(i, j) % 256 << 16;
                int g = mStrategy.getGreen(i, j) % 256 << 8;
                int b = mStrategy.getBlue(i, j) % 256;
                mColorArray[j * mWidth + i] = mAlpha | r | g | b;
//                    if (i+j < mWidth) {
//                        mColorArray[i * mWidth + j] = 0xffff0000;
//                    } else {
//                        mColorArray[i * mWidth + j] = 0xff00ff00;
//                    }
            }
        }
    }

//    public abstract void onColorCreated();

    private class MyAsyncTask extends AsyncTask<Integer, Integer, Void> {

        @Override
        protected Void doInBackground(Integer... params) {
            if (params.length < 2) {
                return null;
            }
            int total = params[0];
            int current = params[1];
            for (int i = 0; i < mHeight; i++) {
                for (int j = 0; j < mWidth; j++) {
                    int r = mStrategy.getRed(i, j) % 256;
                    int g = mStrategy.getGreen(i, j) % 256;
                    int b = mStrategy.getBlue(i, j) % 256;
                    mColorArray[i * mWidth + j] = mAlpha | r | g | b;
                }
            }

            return null;
        }
    }

    public interface ColorStrategy {
        int getRed(int i, int j);

        int getGreen(int i, int j);

        int getBlue(int i, int j);
    }

    public int getHeight() {
        return mHeight;
    }

    public void setHeight(int height) {
        mHeight = height;
    }

    public int getWidth() {
        return mWidth;
    }

    public void setWidth(int width) {
        mWidth = width;
    }

    public ColorStrategy getStrategy() {
        return mStrategy;
    }

    /**
     * Set ColorStrategy and create color array
     *
     * @param strategy
     */
    public void setStrategy(ColorStrategy strategy) {
        mStrategy = strategy;
        createColors();
    }

    /**
     * Cause mColorArray is generated by ColorStrategy, so there is no
     * setColorArray() method, use setStrategy() instead.
     *
     * @return mColorArray
     */
    public int[] getColorArray() {
        return mColorArray;
    }

    public int getAlpha() {
        return mAlpha;
    }

    public void setAlpha(int alpha) {
        mAlpha = alpha;
    }
}

@@ -1799,32 +1799,32 @@
    for (int 
-i
+j
  = 0; 
-i
+j
  %3C mHeight; 
@@ -1823,17 +1823,17 @@
 Height; 
-i
+j
 ++) %7B%0A  
@@ -1851,32 +1851,32 @@
    for (int 
-j
+i
  = 0; 
-j
+i
  %3C mWidth; j
@@ -1874,17 +1874,17 @@
 mWidth; 
-j
+i
 ++) %7B%0A  
@@ -2082,33 +2082,33 @@
     mColorArray%5B
-i
+j
  * mWidth + j%5D =
@@ -2099,25 +2099,25 @@
  * mWidth + 
-j
+i
 %5D = mAlpha %7C

change i j