Skip to main content

Play With Bitmap :)

Play With Bitmap :)
How to replace color from bitmap android 

public class Main extends Activity {
    ImageView imViewAndroid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imViewAndroid = (ImageView) findViewById(R.id.imViewAndroid);
        Bitmap b = replaceColor(
                BitmapFactory.decodeResource(getResources(), R.drawable.basic),
                Color.YELLOW, Color.MAGENTA);
        imViewAndroid.setImageBitmap(b);

    }

    public Bitmap replaceColor(Bitmap src, int fromColor, int targetColor) {
        if (src == null) {
            return null;
        }
        // Source image size
        int width = src.getWidth();
        int height = src.getHeight();
        int[] pixels = new int[width * height];
        // get pixels
        src.getPixels(pixels, 0, width, 0, 0, width, height);

        for (int x = 0; x < pixels.length; ++x) {
            pixels[x] = (pixels[x] == fromColor) ? targetColor : pixels[x];
        }
        // create result bitmap output
        Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());
        // set pixels
        result.setPixels(pixels, 0, width, 0, 0, width, height);

        return result;
    }

Comments