Getting started with Android 2016 (16) -- Drawing
Drawing is designed to the image format. If you have time, you can see the various formats of image resources. Understanding A Piece Of format is useful for learning. I also like to ask this question when interviewing others. Haha.
First look at a graph.
Let's look at the code. The comments are very detailed.
Activity_hello_world.xml
HelloWorldActivity. java
Package com. fable. helloworld; import android. app. activity; import android. content. res. resources; import android. graphics. bitmap; import android. graphics. bitmap. config; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. typeface; import android. graphics. drawable. bitmapDrawable; import android. graphics. d Rawable. drawable; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. imageView; public class HelloWorldActivity extends Activity {ImageView iv; Button btn1, btn2, btn3; Resources r; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_hello_world); iv = (ImageView) this. findViewB YId (R. id. imageView01); btn1 = (Button) this. findViewById (R. id. button01); btn2 = (Button) this. findViewById (R. id. button02); btn3 = (Button) this. findViewById (R. id. button03); btn1.setOnClickListener (new ClickEvent (); btn2.setOnClickListener (new ClickEvent (); btn3.setOnClickListener (new ClickEvent (); r = this. getResources ();} class ClickEvent implements View. onClickListener {public void onClick (View v ){ If (v = btn1) // display the resource image {// function equivalent // iv. setBackgroundResource (R. drawable. fable5); // open the resource image Bitmap bmp = BitmapFactory. decodeResource (r, R. drawable. fable5); // open the resource image iv. setImageBitmap (bmp); // display image} else if (v = btn2) // display and draw resource image {Bitmap bmp = BitmapFactory. decodeResource (r, R. drawable. fable5); // read-only, do not directly draw Bitmap newb = Bitmap on bmp. createBitmap (300,300, Config. ARGB_8888); // create a new bitmap Canvas canvasTemp = new Canv As (newb); // canvas, canvas. Use newb content to draw canvasTemp on the canvas. drawColor (Color. TRANSPARENT); // set the background color of the canvas to TRANSPARENT Paint p = new Paint (); // Paint brush, haha, the canvas and paint String familyName = ""; // you can use a non-system font to use. Many fonts in the game are special, need to be placed in assets/fonts/Typeface font = Typeface. create (familyName, Typeface. BOLD); // BOLD p. setColor (Color. RED); // the paint brush color is RED p. setTypeface (font); // set the font p. setTextSize (24); // font size canvasTemp. drawText ("fataobao legend road ",, P); // draw a few words on the canvas: canvasTemp. drawBitmap (bmp, 50, 50, p); // draw the fable5 you just read on the canvas. // all the above are painted on the newb graph on the canvas. Iv. setImageBitmap (newb); // display this new image} else if (v = btn3) // draw on the Button directly {Bitmap newb = Bitmap. createBitmap (btn3.getWidth (), btn3.getHeight (), Config. ARGB_8888); // create an image Canvas canvasTemp = new Canvas (newb); // place the image on the Canvas canvasTemp. drawColor (Color. WHITE); // the background color is WHITE Paint p = new Paint (); // brush String familyName = ""; // use typetypeface font = Typeface. create (familyName, Typeface. BOLD); // set the font p. setColor (Color. BLUE); // BLUE p. setTypeface (font); // set the font of the paint brush. setTextSize (24); // font size canvasTemp. drawText ("getting started with Android 2016", 14, 24, p); // write a few words. Do not fill in too many xy words, drawable drawable = new BitmapDrawable (getApplicationContext () cannot be found when the button size is exceeded (). getResources (), newb); // In fact, this is not very clear about btn3.setBackgroundDrawable (drawable); // use this figure as the background of the button }}}}
The code will be uploaded later.