In Android operating system development, a basic operation is very common for graphic interface operations. We can first use the operations related to Android plotting described in this article to learn more about the operation skills of a single interface, so as to improve our awareness 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. porterduduxfermode;
- 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 ();
- // Create a canvas with a specific object
- Canvas c = new Canvas (bm );
- Paint p = new Paint ();
- // It is mainly used to remove the edge. In effect, it seems that there is a margin if false.
- P. setAntiAlias (true );
- P. setAlpha (0x555555); // gray
- C. drawCircle (x/2, y/2, x/2, p );
- P. setAlpha (0x0000ff); // blue
- P. setXfermode (new porterduxfermode (PorterDuff. Mode. SRC_OVER ));
- // The following figure shows the font settings.
- P. setTextSize (20 );
- String familyName = "";
- 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", x/2, (y-fm.ascent)/2, p );
- }
- Public SampleView (Context context ){
- Super (context );
- SetFocusable (true );
- // Several methods for obtaining images
- InputStream is = context. getResources ().
OpenRawResource (R. drawable. app_sample_code );
- MBitmap = BitmapFactory. decodeStream (is );
- // Select a color value from an image and change it to a new image. Generally, the color of the paint brush is the same as that of the image below.
P. setColor (Color. BLUE );
- MBitmapmBitmap2 = mBitmap. extractAlpha ();
- MBitmap3 = Bitmap. createBitmap (200,200, Bitmap. Config. ALPHA_8 );
- DrawIntoBitmap (mBitmap3 );
- // 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 );
- }
- }
- }
If you change an image into a mutable image, you can mark it as your logo. This program is mainly used to draw an image by yourself. It can be used to draw an image, just like a blank sheet of white paper. At the same time, there is a function that can be used to extract the shape.