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
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:
Here you have the complete implementation, hope code walk through will help you: