Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6925897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:50:55+00:00 2026-05-27T10:50:55+00:00

i have prepared one custom view using onDraw method, my view class is, public

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:50:56+00:00Added an answer on May 27, 2026 at 10:50 am

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have prepared paint application,my application contains one custom view for paint.when we draw
I have prepared one custom view, in custom view I have displayed alphabets. It
I have a class test with 2 functions, one to create the prepared statement
I have a problem with my database class. I have a method that takes
I have a PHP script with one select query and several prepared update statements.
I am using Spring 3.0.2. I have two relatively simple bean definitions. One has
I have an Android app that I have just prepared for localization, with one
I have a problem with compiling custom widgetsets for my vaadin application using Maven.
I have something that looks like this: my $report = new ReportGenerator; #custom object
We have a Prepared Statement in Java and we hear that it is different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.