SuReSh PaTi
SuReSh PaTi

Reputation: 1371

how to find pixel color in custom view?

i have prepared one custom view using onDraw method,

my view class is,

public class MyAlphabetDraw extends View {

    Paint mPaint = new Paint();
    public static Bitmap myBitmap;

    public MyAlphabetDraw(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);

        mPaint.setDither(true);
        mPaint.setColor(0xFFFFFFFF);
        mPaint.setTextSize(50);

    }

    public void onDraw(Canvas canvas) {
        // canvas.drawColor(Color.BLUE);
        myBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(),
                Bitmap.Config.ARGB_8888);
                System.out.println("bitmaps ---"+myBitmap);
                canvas.drawText("Android", 50, 280,mAlphaInner);

        }

}

Here i converted view to bitmap using,

myBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(),
                    Bitmap.Config.ARGB_8888);

after getting bitmaps i converted into pixels using,

private int intArray[];

intArray = new int[myBitmap.getWidth() * myBitmap.getHeight()];

        // copy pixel data from the Bitmap into the 'intArray' array
        myBitmap.getPixels(intArray, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(),myBitmap.getHeight());

        // replace the red pixels with yellow ones
        for (int i = 0; i < intArray.length; i++) {
                    System.out.println("color is--"+i+" "+intArray[i]);
            if (intArray[i] == 0xFFFFFFFF) {

                intArray[i] = 0xFFFF0000;
            }
        }

here control not enter into if condition.sop printed color value always "0". Some where i done mistake...please me

Upvotes: 1

Views: 2501

Answers (1)

havexz
havexz

Reputation: 9590

The ans to your question is that you will get 0 only as in your code you never updated your bitmap with any drawing. All actions were just related to creating the bitmap.

You have to call apis like myBitmap.setPixel(0,0,color).

Now if you want to use canvas to write to Bitmap. You have to create a new canvas. Here is the sudo code:

Canvas bmpCanvas = new Canvas(myBitmap);
bmpCanvas.drawText("Android", 50, 280,mAlphaInner);

// change pixel values in the bitmap like you are doing above 
// or you should use bmpCanvas to change the values
// and after that

canvas.drawBitmap(myBitmap, 0, 0, null);

Here you have the complete implementation, hope code walk through will help you:

package com.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MyAlphabetDraw extends View {
    Paint mPaint = new Paint();
    Paint mAlphaInner = new Paint();
    public static Bitmap myBitmap;
    public static Canvas bmpCanvas;
    private int intArray[];

    public MyAlphabetDraw(Context context) {
        super(context);
        init();
    }

    public MyAlphabetDraw(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public MyAlphabetDraw(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    void init() {
        mPaint.setDither(true);
        mPaint.setColor(0xFFFFFFFF);
        mPaint.setTextSize(100);
        mPaint.setStrokeWidth(10);
        mAlphaInner.setDither(true);
        mAlphaInner.setColor(0xFF0000FF);
        mAlphaInner.setTextSize(98);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (myBitmap == null) {
            myBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(),
                    Bitmap.Config.ARGB_8888);
            bmpCanvas = new Canvas(myBitmap);
            intArray = new int[myBitmap.getWidth() * myBitmap.getHeight()];
        }

        if (bmpCanvas != null) {
            bmpCanvas.drawCircle(100, 100, 100, mPaint);
            bmpCanvas.drawCircle(100, 100, 90, mAlphaInner);

            // Code to get copy pixels into int array
            myBitmap.getPixels(intArray, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(),
                    myBitmap.getHeight());

            // Sample code if you put false the image will have white border
            // if true image will have red color for white pixels
            if (true) {
                for (int i = 0; i < intArray.length; i++) {
                    if (intArray[i] == 0xFFFFFFFF) {
                        intArray[i] = 0xFFFF0000;
                    }
                }
                myBitmap.setPixels(intArray, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(),
                        myBitmap.getHeight());
            }
        }

        if (myBitmap != null)
            canvas.drawBitmap(myBitmap, 0, 0, null);
    }
}

Upvotes: 1

Related Questions