Painting an Android robot with Canvas and Paint in Android

scene

In Android, the brush uses the Paint class, and the Canvas uses the Canvas class to represent it.

Basic steps of drawing

First, write a custom view class inherited from view, then rewrite its onDraw method, and finally add the custom view to actvity.

Effect

 

 

Note:

Blog:
https://blog.csdn.net/badao_liumang_qizhi
Pay attention to the public address
Domineering procedural ape
Get programming related ebooks, tutorials and free downloads.

Realization

First, change to FrameLayout in the layout file of the Activity to be displayed and add an id.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RobitActivity">

</FrameLayout>

Then create a new custom View class in acitivity. Here, MyView makes it inherit the View class and writes a construction method with one parameter and rewrites the onDraw method.

In the redraw onDraw method, create a new brush and set some properties, then use the brush to draw the drawing at the specified position on the canvas.

package com.badao.alarmmanager;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

public class RobitActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_robit);
        FrameLayout frameLayout= (FrameLayout) findViewById(R.id.frameLayout);  //Get frame layout manager
        frameLayout.addView(new MyView(this));  //Add inner classes of custom views to layout manager
    }

    private class MyView extends View {
        public MyView(Context context) {
            super(context);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint paint=new Paint(); //Create a brush by default
            paint.setAntiAlias(true); //Use anti aliasing
            paint.setColor(0xFFA4C739); //Set the color of the brush to green
            //Drawing the robot's head
            RectF rectf_head=new RectF(10, 10, 100, 100);
            rectf_head.offset(90, 20);
            canvas.drawArc(rectf_head, -10, -160, false, paint); //Arc drawing
            //Draw eyes
            paint.setColor(Color.WHITE); //Set the color of the brush to white
            canvas.drawCircle(125, 53, 4, paint); //Draw circle
            canvas.drawCircle(165, 53, 4, paint); //Draw circle
            paint.setColor(0xFFA4C739); //Set the color of the brush to green
            //Drawing antenna
            paint.setStrokeWidth(2); //Set the width of the stroke
            canvas.drawLine(110, 15, 125, 35, paint); //Drawing line
            canvas.drawLine(180, 15, 165, 35, paint); //Drawing line
            //Drawing body
            canvas.drawRect(100, 75, 190, 150, paint); //draw rectangle
            RectF rectf_body=new RectF(100,140,190,160);
            canvas.drawRoundRect(rectf_body, 10, 10, paint); //Draw a rounded rectangle
            //Draw arms
            RectF rectf_arm=new RectF(75,75,95,140);
            canvas.drawRoundRect(rectf_arm, 10, 10, paint); //Draw the left arm
            rectf_arm.offset(120, 0);       //Set in X Offset 120 pixels on axis
            canvas.drawRoundRect(rectf_arm, 10, 10, paint); //Draw the right arm
            //Draw leg
            RectF rectf_leg=new RectF(115,150,135,200);
            canvas.drawRoundRect(rectf_leg, 10, 10, paint); //Draw the left leg
            rectf_leg.offset(40, 0);       //Set in X Offset 40 pixels on axis
            canvas.drawRoundRect(rectf_leg, 10, 10, paint); //Draw the right leg
        }
    }
}

Tags: Android Programming xml encoding

Posted on Fri, 10 Jan 2020 12:38:58 -0500 by smc