9

By having the following codes, I have some questions.

public class MainActivity extends Activity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView( new View(this) {
         Paint mPaint = new Paint();

         @Override
         protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);


            int width = this.getWidth();
            int height = this.getHeight();
            int radius = width > height ? height/2 : width/2;
            int center_x = width/2;
            int center_y = height/2;

            // prepare a paint
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(5);
            mPaint.setAntiAlias(true);

            // draw a rectangle
            mPaint.setColor(Color.BLUE);
                mPaint.setStyle(Paint.Style.FILL); //fill the background with blue color
            canvas.drawRect(center_x - radius, center_y - radius, center_x + radius, center_y + radius, mPaint);
            // draw some text and rotation
            mPaint.setTextSize(50);
            mPaint.setTextAlign(Paint.Align.CENTER);
            mPaint.setColor(Color.BLACK);
            canvas.drawText( "Hello World" , center_x , center_y, mPaint);
         }
      });
    }
}

enter image description here

Q1: How can I fill blue colour in the frame? (The words still appear)

Q2: How many views and surfaces in this app? How can I count these in the app?

Q3: How many windows in this app?

Q4: In the code, I dont see any bitmap object in it. However, I thought that bitmap is the object that I can really draw things on it. Is my understanding incorrect? One possibility is that Canvas constructor initializes bitmap when it is newed.

Q5: I knew that these graphic thing will finally go to surface and then pass to surfaceflinger for final composition. Where does it locate in my code?

Thanks for any reply.

Sam
  • 4,521
  • 13
  • 46
  • 81

3 Answers3

8

Five questions. Let's see where I can help.

Q1: Tell the Paint to fill the rectangle: paint.setStyle(Paint.Style.FILL);

Q2: I see only the one view you create programmatically. Why would you like to count the views?

Q3: Again: One

Q4: You draw an mutable bitmaps by wrapping them with a Canvas. The method to actually draw are part of Canvas

Q5: The code you show is part of an Activity. The Activity is called by Android. It's your entry point into your App.

jboi
  • 11,324
  • 4
  • 36
  • 43
  • Q1: After adding paint.setStyle(Paint.Style.FILL); right after the paint to draw the rectangle, the effect remains the same. Should I have to do something else to make it work (codes have been changed) Q2: I thought that (1)white background is a view and the blue canvas is another view. I count this because I want to be more clear with some terms in Android. (surface, paint, bitmap, view, window....) Q3: Will that be possible to create multiple window in an app? If so, can you show me a simple example? Does one activity mean I can only create one window in it? – Sam Sep 22 '13 at 21:22
  • Q4: Thanks. this is more clean now. Q5: So Android does these things to my created activity. – Sam Sep 22 '13 at 21:25
  • Q1: I have just figured out how to implement this. Thanks. – Sam Sep 23 '13 at 01:27
7

Thanks for the answer. I did the job of making the code for marked answer, and it works.

    Bitmap bg = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bg);
    // paint background with the trick
    Paint rect_paint = new Paint();
    rect_paint.setStyle(Paint.Style.FILL);
    rect_paint.setColor(Color.rgb(0, 0, 0));
    rect_paint.setAlpha(0x80); // optional
    canvas.drawRect(0, 0, width, height, rect_paint); // that's painting the whole canvas in the chosen color.
Puffy
  • 401
  • 6
  • 13
  • Puffy, I used the code that passed above and solved my problem of changing the color of my canvas, however if I already have a drawing and I change the color of the canvas, it ends up overlapping my drawing, how do I change the color without overlapping the drawing already done? – Carlos Diego Jul 14 '20 at 16:44
0

Q2:Hierarchy Viewer is very useful when you want to count how many views in your app. Optimizing Your UI

Peter Zhao
  • 7,456
  • 3
  • 21
  • 22
  • Thanks. Do you know how to count how many windows in an app? Will that be possible to create multiple windows in an app? – Sam Sep 23 '13 at 16:37
  • Why do you want to create multiple windows? Are you making an app for tablet? – Peter Zhao Sep 24 '13 at 10:35
  • William, my intention is to understand what role that window play in the view system. Say that we have bitmap, paint, surface, canvas, view and window for the graphic application layer. I am clear with others only window and view confuse me. – Sam Sep 25 '13 at 13:08
  • Window means the screen of the device, so there is only one window in an app. I have seen a multiple windows tablet based on android system, but actually it's not multiple windows but multiple views. – Peter Zhao Sep 26 '13 at 02:24
  • I see. So in each app, there is only one window in an app. Could you explain a bit about the app I created in this question list, I seem to misunderstand that this app has three views in it graphic system (let us ignore status bar or some other views come with default). 1. The white background 2. The blue canvas and 3. the "HelloWorld" displayed on the canvas. Will this be separated to three views? I've checked this with Hierarchy viewer but it seems only one view in an app as Jobi mentioned in the answer. Further, in Hierarchy viewer, I dont see when the white background gets displayed. – Sam Sep 26 '13 at 06:04
  • 1
    This is only one view. The white background is activity's default style. The view you created is transparent by default, and fills white background. The canvas fills the view, not only the blue range. You have drawn a blue square in the canvas so you think the blue range is the canvas, aren't you? :) Finally you drew a text on the canvas. There is only one view, and you have drawn everything on it's canvas. – Peter Zhao Sep 26 '13 at 13:38
  • Brilliant. Let me clarify something here. 1. The system created a view with white background by default. 2. I added a canvas on this white view and drew a blue square on the canvas. This means, the canvas I added is also white color by default and I only drew a blue square on the white canvas I created. It does not overlap. Is my understanding correct? Thanks William. – Sam Sep 28 '13 at 04:11
  • Yes, the view and canvas are both transparent color, but seem white. You are welcome:) – Peter Zhao Sep 28 '13 at 06:19
  • Would you mind for having your email address? It's none in your profile. – Sam Sep 28 '13 at 07:50
  • Sure. qq157755587@gmail.com – Peter Zhao Sep 28 '13 at 14:39
  • Thanks William. I have sent you an email. Please check your email box. – Sam Sep 29 '13 at 00:49