Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize drawing in Android

Shulou Source: shulou.com Published: 2022-05-31 21:46:28 09月19日 Update

This article mainly introduces the relevant knowledge of "how to draw in Android". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to draw in Android" can help you solve the problem.

In the development of Android operating system, it is a very common basic operation for the operation of graphical interface. We can first learn about the operation skills of the graphical interface through the related operations of Android drawing introduced in this article, so as to improve our understanding of this system.

Import java.io.InputStream

Import android.app.Activity

Import android.content.Context

Import android.graphics.Bitmap

Import android.graphics.BitmapFactory

Import android.graphics.Canvas

Import android.graphics.Color

Import android.graphics.LinearGradient

Import android.graphics.Paint

Import android.graphics.PorterDuff

Import android.graphics.PorterDuffXfermode

Import android.graphics.Shader

Import android.graphics.Typeface

Import android.os.Bundle

Import android.view.View

Public class AlphaBitmap extends Activity {

@ Override

Protected void onCreate (Bundle savedInstanceState) {

Super.onCreate (savedInstanceState)

SetContentView (new SampleView (this))

}

Private static class SampleView extends View {

Private Bitmap mBitmap

Private Bitmap mBitmap2

Private Bitmap mBitmap3

Private Shader mShader

Private static void drawIntoBitmap (Bitmap bm) {

Float x = bm.getWidth ()

Float y = bm.getHeight ()

/ / build a canvas with defined objects

Canvas c = new Canvas (bm)

Paint p = new Paint ()

/ / it is mainly used to remove edges. From the effect, it seems that false has rough edges.

P.setAntiAlias (true)

P.setAlpha (0x555555); / / Gray

C.drawCircle (XAGAR 2, YAGUAGAN 2, XAGUAGUBER 2, p)

P.setAlpha (0x0000ff); / / Blue

P.setXfermode (new PorterDuffXfermode (PorterDuff.Mode.SRC_OVER))

/ / the following are the settings for fonts

P.setTextSize (20)

String familyName = "Song style"

Typeface font = Typeface.create (familyName,Typeface.ITALIC)

P.setTypeface (font)

P.setTextAlign (Paint.Align.CENTER)

Paint.FontMetrics fm = p.getFontMetrics ()

C.drawText ("I love android", Xamp 2, (y-fm.ascent) / 2, p)

}

Public SampleView (Context context) {

Super (context)

SetFocusable (true)

/ / several methods to get an image

InputStream is = context.getResources ().

OpenRawResource (R.drawable.app_sample_code)

MBitmap = BitmapFactory.decodeStream (is)

/ / extract the color value from an image into a new image, usually the color of the brush is the same as below

P.setColor (Color.BLUE)

MBitmapmBitmap2 = mBitmap.extractAlpha ()

MBitmap3 = Bitmap.createBitmap (200,200, Bitmap.Config.ALPHA_8)

DrawIntoBitmap (mBitmap3)

/ / A gradient effect

MShader = new LinearGradient (0,0,150,70, new int [] {

Color.RED, Color.GREEN, Color.BLUE}

Null, Shader.TileMode.MIRROR)

}

@ Override protected void onDraw (Canvas canvas) {

Canvas.drawColor (Color.LTGRAY)

Paint p = new Paint ()

Float y = 10

P.setColor (Color.BLUE)

Canvas.drawBitmap (mBitmap, 10, y, p)

Y + = mBitmap.getHeight () + 10

Canvas.drawBitmap (mBitmap2, 10, y, p)

Y + = mBitmap2.getHeight () + 10

P.setShader (mShader)

Canvas.drawBitmap (mBitmap3, 10, y, p)

}

}

}

This is the end of the introduction of "how to draw in Android". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Images knowledge graphics effects methods interfaces systems industries different practical operating systems content fundamentals fonts Arial practicality reality objects common I love Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info vpn Docker macOS OPPO Reno