Android canvas fill background color (Canvas appli

2019-03-27 19:14发布

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.

3条回答
做个烂人
2楼-- · 2019-03-27 19:22

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.
查看更多
Summer. ? 凉城
3楼-- · 2019-03-27 19:23

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

查看更多
SAY GOODBYE
4楼-- · 2019-03-27 19:36

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.

查看更多
登录 后发表回答