This article comes fromHttp://blog.csdn.net/hellogv/, The reference must indicate the source!
There are a lot of commonly used Widgets. Now I want to talk about the commonly used widgets in mobile phone development. To understand how to draw images for Android, you must first understand the basic graphic interfaces used:
1. bitmap, which can be from resources/files or inProgramActually, the function is equivalent to the storage space of images;
2. Canvas is closely related to bitmap. If bitmap is used as a metaphor, canvas is a platform that provides many methods to operate bitamp;
3. Paint, which is closely related to canvas. It is a paint tool on the "canvas" and is also used to set the style on the View control;
4. drawable: if the first three pictures are invisible in the memory, drawable is the interface that shows the drawing results of the first three. Drawable has multiple child classes, such as bitmapdrawable, shapedrawable, and layerdrawable.
This article mainly explains how to draw images in the imageview and how to draw custom images directly on the button (inheriting the view control.
Directly draw resource Images
Draw pictures and draw words on imageview
Draw a picture directly on the control background
Source code of Main. xml:
View plain
Copy to clipboard
Print
?
-
- <?XML Version="1.0" Encoding="UTF-8"?>
-
- <Linearlayout Xmlns: Android=Http://schemas.android.com/apk/res/android"
-
- Android: Orientation="Vertical"
- Android: layout_width="Fill_parent"
-
- Android: layout_height="Fill_parent"
-
- >
- button Android: ID = "@ + ID/button01" Android: layout_width = "fill_parent" Android: layout_height = "44px" Android: text = "display resource images" > button >
- button Android: ID = "@ + ID/button02" Android: layout_width = "fill_parent" Android: layout_height = "44px" Android: text = "display and draw resource images" > button >
- button Android: ID = "@ + ID/button03" Android: layout_height = "44px" Android: layout_width = "fill_parent" Android: text = "drawing on Control" > button >
- <Imageview Android: ID="@ + ID/imageview01" Android: layout_width="Wrap_content" Android: layout_height="Wrap_content"></Imageview>
-
-
- </Linearlayout>
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <button Android: Id = "@ + ID/button01" Android: layout_width = "fill_parent" Android: layout_height = "44px" Android: text = "show resource image"> </button> <br/> <button Android: id = "@ + ID/button02" Android: layout_width = "fill_parent" Android: layout_height = "44px" Android: TEXT = "show and draw resource images"> </button> <br/> <button Android: Id = "@ + ID/button03" Android: layout_height = "44px" Android: layout_width = "fill_parent" Android: text = ""> </button> <br/> <imageview Android: Id = "@ + ID/imageview01" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </imageview> </P> <p> </linearlayout> <br/>
Program source code:
View plain
Copy to clipboard
Print
?
-
- PackageCom. testdraw;
-
-
- ImportAndroid. App. activity;
-
- ImportAndroid. content. res. Resources;
-
- ImportAndroid. Graphics. Bitmap;
-
- ImportAndroid. Graphics. bitmap. config;
-
- ImportAndroid. Graphics. bitmapfactory;
-
- ImportAndroid. Graphics. Canvas;
- ImportAndroid. Graphics. color;
-
- ImportAndroid. Graphics. paint;
-
- ImportAndroid. Graphics. typeface;
-
- ImportAndroid. Graphics. drawable. bitmapdrawable;
-
- ImportAndroid. Graphics. drawable. drawable;
-
- ImportAndroid. OS. Bundle;
-
- ImportAndroid. View. view;
- ImportAndroid. widget. Button;
-
- ImportAndroid. widget. imageview;
-
-
- Public ClassTestdrawExtendsActivity {
-
-
- Imageview IV;
-
- Button btn1, btn2, btn3, btn4;
-
- Resources R;
- @ Override
-
- Public VoidOncreate (bundle savedinstancestate ){
-
- Super. Oncreate (savedinstancestate );
-
- Setcontentview (R. layout. Main );
-
- IV = (imageview)This. Findviewbyid (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 (NewClickevent ());
-
- Btn2.setonclicklistener (NewClickevent ());
- Btn3.setonclicklistener (NewClickevent ());
-
-
- R =This. Getresources ();
-
-
-
- }
-
- ClassClickeventImplementsView. onclicklistener {
-
- Public VoidOnclick (view v ){
-
- If(V = btn1)// Display resource Images
-
- {// Function equivalent
-
- // IV. setbackgroundresource (R. drawable. Icon); // open the resource Image
- Bitmap BMP = bitmapfactory. decoderesource (R, R. drawable. Icon );// Open the resource Image
-
- Iv. setimagebitmap (BMP );
-
- }
-
- Else If(V = btn2)// Display and draw resource Images
-
- {
- Bitmap BMP = bitmapfactory. decoderesource (R, R. drawable. Icon );// Read-only, cannot be directly painted on BMP
-
- Bitmap newb = bitmap. createbitmap (300,300, Config. argb_8888 );
-
- Canvas canvastemp =NewCanvas (NEWB );
-
- Canvastemp. drawcolor (color. Transparent );
-
-
- Paint P =NewPaint ();
- String familyname ="";
-
- Typeface font = typeface. Create (familyname, typeface. Bold );
-
- P. setcolor (color. Red );
-
- P. settypeface (font );
-
- P. settextsize (22);
- Canvastemp. drawtext ("Writing... ",50,50, P );
-
- Canvastemp. drawbitmap (BMP,50,50, P );// Draw
-
- Iv. setimagebitmap (NEWB );
-
- }
- Else If(V = btn3)// Draw the image directly on the button
-
- {
-
- Bitmap newb = bitmap. createbitmap (btn3.getwidth (), btn3.getheight (), config. argb_8888 );
-
- Canvas canvastemp =NewCanvas (NEWB );
- Canvastemp. drawcolor (color. White );
-
- Paint P =NewPaint ();
-
- String familyname ="";
-
- Typeface font = typeface. Create (familyname, typeface. Bold );
- P. setcolor (color. Red );
-
- P. settypeface (font );
-
- P. settextsize (20);
-
- Canvastemp. drawtext ("Writing... ",30,30, P );
- Drawable =NewBitmapdrawable (NEWB );
-
- Btn3.setbackgrounddrawable (drawable );
-
- }
-
- }
-
-
- }
-
-
- }