search
HomeJavajavaTutorialIntroduction to the method of using Path to draw basic graphics such as circles, rectangles, ellipses, triangles, etc. in Android development Canvas

This article mainly introduces the method of using Path to draw basic graphics in Canvas in Android programming development, involving basic graphics drawing skills of Android, and analyzes the implementation of drawing basic graphics such as circles, rectangles, ellipses, triangles, etc. with examples. For the method, friends who need it can refer to

Drawing basic collection graphics in Android. This program is to customize a View component. The program rewrites the onDraw(Canvase) method of the View component, and then draws it on the Canvas. Draw a large number of basic set shapes.

Go directly to the code:

1. Customized View component code:

package com.infy.configuration;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View{
    public MyView(Context context, AttributeSet attrs) {
      super(context, attrs);
    }
    @Override
    protected void onDraw(Canvas canvas) {
      // TODO Auto-generated method stub
      super.onDraw(canvas);
      //把整张画布绘制成白色
      canvas.drawColor(Color.WHITE);
      Paint paint = new Paint();
      //去锯齿
      paint.setAntiAlias(true);
      paint.setColor(Color.BLUE);
      paint.setStyle(Paint.Style.STROKE);
      paint.setStrokeWidth(3);
      //绘制圆形
      canvas.drawCircle(40, 40, 30, paint);
      //绘制正方形
      canvas.drawRect(10, 80, 70, 140, paint);
      //绘制矩形
      canvas.drawRect(10, 150, 70, 190, paint);
      RectF rel = new RectF(10,240,70,270);
      //绘制椭圆
      canvas.drawOval(rel, paint);
      //定义一个Path对象,封闭一个三角形
      Path path1 = new Path();
      path1.moveTo(10, 340);
      path1.lineTo(70, 340);
      path1.lineTo(40, 290);
      path1.close();
      //根据Path进行绘制,绘制三角形
      canvas.drawPath(path1, paint);
      //定义一个Path对象,封闭一个五角星
      Path path2 = new Path();
      path2.moveTo(27, 360);
      path2.lineTo(54, 360);
      path2.lineTo(70, 392);
      path2.lineTo(40, 420);
      path2.lineTo(10, 392);
      path2.close();
      //根据Path进行绘制,绘制五角星
      canvas.drawPath(path2, paint);
      //设置填丛风格后进行绘制
      paint.setStyle(Paint.Style.FILL);
      paint.setColor(Color.RED);
      canvas.drawCircle(120, 40, 30, paint);
      //绘制正方形
      canvas.drawRect(90, 80, 150, 140, paint);
      //绘制矩形
      canvas.drawRect(90, 150, 150, 190, paint);
      //绘制圆角矩形
      RectF re2 = new RectF(90,200,150,230);
      canvas.drawRoundRect(re2, 15, 15, paint);
      //绘制椭圆
      RectF re21 = new RectF(90, 240, 150, 270);
      canvas.drawOval(re21, paint);
      Path path3 = new Path();
      path3.moveTo(90, 340);
      path3.lineTo(150, 340);
      path3.lineTo(120, 290);
      path3.close();
      //绘制三角形
      canvas.drawPath(path3,paint);
      //绘制五角形
      Path path4 = new Path();
      path4.moveTo(106, 360);
      path4.lineTo(134, 360);
      path4.lineTo(150, 392);
      path4.lineTo(120, 420);
      path4.lineTo(90, 392);
      path4.close();
      canvas.drawPath(path4, paint);
      //设置渐变器后绘制
      //为Paint设置渐变器
      Shader mShasder = new LinearGradient(0, 0, 40, 60, new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW}, null, Shader.TileMode.REPEAT);
      paint.setShader(mShasder);
      //设置阴影
      paint.setShadowLayer(45, 10, 10, Color.GRAY);
      //绘制圆形
      canvas.drawCircle(200, 40, 30, paint);
      //绘制正方形
      canvas.drawRect(170, 80, 230, 140, paint);
      //绘制矩形
      canvas.drawRect(170, 150, 230, 190, paint);
      //绘制圆角的矩形
      RectF re31 = new RectF();
      canvas.drawRoundRect(re31, 15, 15, paint);
      //绘制椭圆
      RectF re32 =new RectF();
      canvas.drawOval(re32, paint);
      //根据Path,绘制三角形
      Path path5 = new Path();
      path5.moveTo(170, 340);
      path5.lineTo(230, 340);
      path5.lineTo(200, 290);
      path5.close();
      canvas.drawPath(path5, paint);
      //根据PAth,进行绘制五角形
      Path path6 = new Path();
      path6.moveTo(186, 360);
      path6.lineTo(214, 360);
      path6.lineTo(230, 392);
      path6.lineTo(200, 420);
      path6.lineTo(170, 392);
      path6.close();
      canvas.drawPath(path6, paint);
    }
}

2. Use a basic Activity to implement the customized MyView component,

Define an Activity of ZiDingYiViewTes:

package com.infy.configuration;
import android.app.Activity;
import android.os.Bundle;
public class ZiDingYiViewTes extends Activity{
    private MyView myView =null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      myView = new MyView(this, null);
      setContentView(myView);
    }
}

Android's Canvas can draw simple collection graphics or draw a Bitmap directly to the canvas.
Finally attached is the rendering (with an extra ellipse):

The above is the detailed content of Introduction to the method of using Path to draw basic graphics such as circles, rectangles, ellipses, triangles, etc. in Android development Canvas. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to get Java entity class attribute names elegantly to avoid hard-coded in MyBatis queries?How to get Java entity class attribute names elegantly to avoid hard-coded in MyBatis queries?Apr 19, 2025 pm 08:27 PM

When using MyBatis-Plus or tk.mybatis...

How to efficiently query personnel data in MySql and ElasticSearch through natural language processing?How to efficiently query personnel data in MySql and ElasticSearch through natural language processing?Apr 19, 2025 pm 08:24 PM

How to query personnel data through natural language processing? In modern data processing, how to efficiently query personnel data is a common and important requirement. ...

How to parse next-auth generated JWT token in Java and get information in it?How to parse next-auth generated JWT token in Java and get information in it?Apr 19, 2025 pm 08:21 PM

In processing next-auth generated JWT...

Why can't JavaScript directly obtain hardware information on the user's computer?Why can't JavaScript directly obtain hardware information on the user's computer?Apr 19, 2025 pm 08:15 PM

Discussion on the reasons why JavaScript cannot obtain user computer hardware information In daily programming, many developers will be curious about why JavaScript cannot be directly obtained...

Circular dependencies appear in the RuoYi framework. How to troubleshoot and solve the problem of dynamicDataSource Bean?Circular dependencies appear in the RuoYi framework. How to troubleshoot and solve the problem of dynamicDataSource Bean?Apr 19, 2025 pm 08:12 PM

RuoYi framework circular dependency problem troubleshooting and solving the problem of circular dependency when using RuoYi framework for development, we often encounter circular dependency problems, which often leads to the program...

When building a microservice architecture using Spring Cloud Alibaba, do you have to manage each module in a parent-child engineering structure?When building a microservice architecture using Spring Cloud Alibaba, do you have to manage each module in a parent-child engineering structure?Apr 19, 2025 pm 08:09 PM

About SpringCloudAlibaba microservices modular development using SpringCloud...

Treatment of x² in curve integral: Why can the standard answer be ignored (1/3) x³?Treatment of x² in curve integral: Why can the standard answer be ignored (1/3) x³?Apr 19, 2025 pm 08:06 PM

Questions about a curve integral This article will answer a curve integral question. The questioner had a question about the standard answer to a sample question...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)