Android Simple Graphics Example
The [Link] can be used to draw graphics in android. It provides
methods to draw oval, rectangle, picture, text, line etc.
The [Link] class is used with canvas to draw objects. It holds the
information of color and style.
In this example, we are going to display 2D graphics in android.
activity_main.xml
File: activity_main.xml
1. <RelativeLayout xmlns:androclass="[Link]
2. xmlns:tools="[Link]
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:paddingBottom="@dimen/activity_vertical_margin"
6. android:paddingLeft="@dimen/activity_horizontal_margin"
7. android:paddingRight="@dimen/activity_horizontal_margin"
8. android:paddingTop="@dimen/activity_vertical_margin"
9. tools:context=".MainActivity" >
10.
11. <TextView
12. android:layout_width="wrap_content"
13. android:layout_height="wrap_content"
14. android:text="@string/hello_world" />
15.
16. </RelativeLayout>
<RelativeLayout xmlns:androcla
xmlns:tools="[Link]
android:layout_w idth="match
android:layout_height="match
android:paddingBottom="@di
Activity class
File: [Link]
1. package [Link];
2.
3. import [Link];
4. import [Link];
5. import [Link];
6. import [Link];
7. import [Link];
8. import [Link];
9. import [Link];
10. import [Link];
11.
12. public class MainActivity extends Activity {
13.
14. DemoView demoview;
15. /** Called when the activity is first created. */
16. @Override
17. public void onCreate(Bundle savedInstanceState) {
18. [Link](savedInstanceState);
19. demoview = new DemoView(this);
20. setContentView(demoview);
21. }
22.
23. private class DemoView extends View{
24. public DemoView(Context context){
25. super(context);
26. }
27.
28. @Override protected void onDraw(Canvas canvas) {
29. [Link](canvas);
30.
31. // custom drawing code here
32. Paint paint = new Paint();
33. [Link]([Link]);
34.
35. // make the entire canvas white
36. [Link]([Link]);
37. [Link](paint);
38.
39. // draw blue circle with anti aliasing turned off
40. [Link](false);
41. [Link]([Link]);
42. [Link](20, 20, 15, paint);
43.
44. // draw green circle with anti aliasing turned on
45. [Link](true);
46. [Link]([Link]);
47. [Link](60, 20, 15, paint);
48.
49. // draw red rectangle with anti aliasing turned off
50. [Link](false);
51. [Link]([Link]);
52. [Link](100, 5, 200, 30, paint);
53.
54. // draw the rotated text
55. [Link](-45);
56.
57. [Link]([Link]);
58. [Link]("Graphics Rotation", 40, 180, paint);
59.
60. //undo the rotate
61. [Link]();
62. }
63. }
64. @Override
65. public boolean onCreateOptionsMenu(Menu menu) {
66. // Inflate the menu; this adds items to the action bar if it is present.
67. getMenuInflater().inflate([Link], menu);
68. return true;
69. }
70. }
package [Link]
import [Link];
import [Link];
import [Link] .Menu;
download this android example
Output:
Android Animation Example
Android provides a large number of classes and interface for the animation development.
Most of the classes and interfaces are given in [Link] package.
Android Animation enables you to change the object property and behavior at run time.
There are various ways to do animation in android.
The AnimationDrawable class provides methods to start and end the animation. Even,
you can use time based animation.
Let's have a look at the simple example of android animation.
activity_main.xml
You need to have a view only.
File: activity_main.xml
1. <RelativeLayout xmlns:android="[Link]
2. xmlns:tools="[Link]
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:paddingBottom="@dimen/activity_vertical_margin"
6. android:paddingLeft="@dimen/activity_horizontal_margin"
7. android:paddingRight="@dimen/activity_horizontal_margin"
8. android:paddingTop="@dimen/activity_vertical_margin"
9. tools:context=".MainActivity" >
10.
11. <View
12. />
13.
14. </RelativeLayout>
<RelativeLayout xmlns:android=
xmlns:tools="[Link]
android:layout_w idth="match
android:layout_height="match
android:paddingBottom="@di
File: [Link]
Have a image view only.
1. <?xml version="1.0" encoding="utf-8"?>
2. <ImageView xmlns:android="[Link]
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:id="@+id/anm"
6. >
7.
8. </ImageView>
android:id="@+id/anm"
>
</ImageView >
MainActivity class
File: [Link]
1. package [Link];
2.
3. import [Link];
4. import [Link];
5. import [Link];
6. import [Link];
7.
8. public class MainActivity extends Activity {
9.
10. ImageView anm;
11. public void onCreate(Bundle savedInstanceState) {
12. [Link](savedInstanceState);
13. setContentView([Link]);
14. anm = (ImageView)findViewById([Link]);
15.
16. [Link]([Link]);
17. // the frame-by-frame animation defined as a xml file within the drawable folder
18.
19. /*
20. * NOTE: It's not possible to start the animation during the onCreate.
21. */
22. }
23. public void onWindowFocusChanged (boolean hasFocus) {
24. [Link](hasFocus);
25. AnimationDrawable frameAnimation =
26. (AnimationDrawable) [Link]();
27. if(hasFocus) {
28. [Link]();
29. } else {
30. [Link]();
31. }
32. }
33.
34. }
package [Link]
import [Link];
import [Link];
import [Link] ab
You need to create [Link] file inside res/drawable-hdpi directory.
You need to have many images. Here, we are using 14 images and all the 14 images are
located inside res/drawable-mdpi directory.
File: [Link]
1. <?xml version="1.0" encoding="utf-8"?>
2. <animation-list xmlns:android="[Link]
3. android:oneshot="false">
4.
5. <item android:drawable="@drawable/frame0" android:duration="120" />
6. <item android:drawable="@drawable/frame1" android:duration="120" />
7. <item android:drawable="@drawable/frame2" android:duration="120" />
8. <item android:drawable="@drawable/frame3" android:duration="120" />
9. <item android:drawable="@drawable/frame4" android:duration="120" />
10. <item android:drawable="@drawable/frame5" android:duration="120" />
11. <item android:drawable="@drawable/frame6" android:duration="120" />
12. <item android:drawable="@drawable/frame7" android:duration="120" />
13. <item android:drawable="@drawable/frame8" android:duration="120" />
14. <item android:drawable="@drawable/frame9" android:duration="120" />
15. <item android:drawable="@drawable/frame10" android:duration="120" />
16. <item android:drawable="@drawable/frame11" android:duration="120" />
17. <item android:drawable="@drawable/frame12" android:duration="120" />
18. <item android:drawable="@drawable/frame13" android:duration="120" />
19. <item android:drawable="@drawable/frame14" android:duration="120" />
20. <item android:drawable="@drawable/frame14" android:duration="120" />
21. <item android:drawable="@drawable/frame13" android:duration="120" />
22. <item android:drawable="@drawable/frame12" android:duration="120" />
23. <item android:drawable="@drawable/frame11" android:duration="120" />
24. <item android:drawable="@drawable/frame10" android:duration="120" />
25. <item android:drawable="@drawable/frame9" android:duration="120" />
26. <item android:drawable="@drawable/frame8" android:duration="120" />
27. <item android:drawable="@drawable/frame7" android:duration="120" />
28. <item android:drawable="@drawable/frame6" android:duration="120" />
29. <item android:drawable="@drawable/frame5" android:duration="120" />
30. <item android:drawable="@drawable/frame4" android:duration="120" />
31. <item android:drawable="@drawable/frame3" android:duration="120" />
32. <item android:drawable="@drawable/frame2" android:duration="120" />
33. <item android:drawable="@drawable/frame1" android:duration="120" />
34. <item android:drawable="@drawable/frame0" android:duration="120" />
35.
36. </animation-list>
<?xml version="1.0" encoding="
<animation-list xmlns:android="h
android:oneshot="false">
<item android:draw ab
download this android example
Output:
Use of log class
Android SDK includes a useful logging ulility class which is categories based on msgs
import [Link];
the msgs are ordered from lowest to highest priority
1. Log.v()
2. Log.d()