Write a program welcome interface (draw a circle using the drawing method )., Drawing ball
1 package com. lixu. drawable; 2 3 import android. app. activity; 4 import android. OS. bundle; 5 import android. view. window; 6 import android. view. windowManager; 7 import untils. untils; 8 9 public class MainActivity extends Activity {10 private Activity activity; 11 12 @ Override13 protected void onCreate (Bundle savedInstanceState) {14 super. onCreate (savedInstanceState); 15 activity = this; 16 // set full screen 17 requestWindowFeature (Window. FEATURE_NO_TITLE); 18 Untils. toggleFullscreen (activity, true); 19 setContentView (R. layout. main); 20 21} 22 23}
1 package com. lixu. drawable; 2 3 import android. app. activity; 4 import android. content. intent; 5 import android. OS. bundle; 6 import android. support. v4.app. fragment; 7 import android. support. v4.app. fragmentActivity; 8 import android. support. v4.app. fragmentManager; 9 import android. support. v4.app. fragmentPagerAdapter; 10 import android. support. v4.view. viewPager; 11 import android. support. v4.view. viewPager. onPageChangeListener; 12 import android. view. layoutInflater; 13 import android. view. view; 14 import android. view. view. onClickListener; 15 import android. view. viewGroup; 16 import android. view. window; 17 import android. widget. button; 18 import android. widget. imageView; 19 import untils. untils; 20 21 public class WelcomActivity extends FragmentActivity {22 private Mydrawable mMydrawable; 23 private Activity; 24 25 private int [] images = {R. drawable. p1, R. drawable. p2, R. drawable. p3, R. drawable. p4, R. drawable. p5, 26 R. drawable. p6,}; 27 28 @ SuppressWarnings ("deprecation") 29 @ Override 30 protected void onCreate (Bundle savedInstanceState) {31 activity = this; 32 super. onCreate (savedInstanceState); 33 // set the program to be in full screen 34 requestWindowFeature (Window. FEATURE_NO_TITLE); 35 Untils. toggleFullscreen (activity, true); 36 37 setContentView (R. layout. activity_main); 38 39 mMydrawable = (Mydrawable) findViewById (R. id. mydrawable); 40 // set the number of circles to 41 mMydrawable. setCount (images. length); 42 43 ViewPager vp = (ViewPager) findViewById (R. id. vp); 44 45 vp. setAdapter (new MyAdapter (this. getsuppfrfragmentmanager (); 46 47 vp. setOnPageChangeListener (new OnPageChangeListener () {48 // set the position of the red dot when the page slides. 49 @ Override 50 public void onPageSelected (int pos) {51 52 mMydrawable. choose (pos); 53 54} 55 56 @ Override 57 public void onPageScrolled (int arg0, float arg1, int arg2) {58 59} 60 61 @ Override 62 public void onPageScrollStateChanged (int arg0) {63 64} 65 }); 66 67} 68 69 private class MyAdapter extends FragmentPagerAdapter {70 71 public MyAdapter (FragmentManager fm) {72 super (fm ); 73} 74 75 @ Override 76 public Fragment getItem (final int pos) {77 78 Fragment f = new Fragment () {79 @ Override 80 public View onCreateView (LayoutInflater inflater, ViewGroup container, bundle savedInstanceState) {81 82 if (pos = (getCount ()-1) {83 84 View view = inflater. inflate (R. layout. fragmentmain, null); 85 Button btn = (Button) view. findViewById (R. id. btn); 86 btn. setOnClickListener (new OnClickListener () {87 88 @ Override 89 public void onClick (View v) {90 Intent intent = new Intent (activity, MainActivity. class); 91 startActivity (intent); 92 // enter the main program to close the initial animation interface 93 activity. finish (); 94} 95}); 96 ImageView iv = (ImageView) view. findViewById (R. id. iv2); 97 iv. setImageResource (images [pos]); 98 return view; 99 100} else {101 View view = inflater. inflate (R. layout. fragment, null); 102 103 ImageView iv = (ImageView) view. findViewById (R. id. iv1); 104 iv. setImageResource (images [pos]); 105 106 107 return view; 108 109} 110} 111}; 112 113 114 return f; 115} @ Override116 public int getCount () {117 return images. length; 118} 119 120} 121 122}
1 package com. lixu. drawable; 2 3 import android. content. context; 4 import android. graphics. canvas; 5 import android. graphics. color; 6 import android. graphics. paint; 7 import android. util. attributeSet; 8 import android. util. log; 9 import android. view. view; 10 11 public class Mydrawable extends View {12 private int posfirst = 0; 13 // the spacing between the sphere 14 private int juli = 50; 15 // Number of Sphere 16 private int count = 0; 17 18 // set the sphere radius 19 private float radius = 10; 20 21 // use this constructor, other errors will be reported: 22 public Mydrawable (Context context, AttributeSet attrs) {23 super (context, attrs ); 24} 25 26 // set the number of circles 27 public void setCount (int a) {28 count = a; 29} 30 31 public void choose (int pos) {32 posfirst = pos; 33 // execute this method to redraw 34 this. invalidate (); 35} 36 37 @ Override38 protected void onDraw (Canvas canvas) {39 super. onDraw (canvas); 40 41 Paint p = new Paint (); 42 // remove the image's sawtooth to make the sphere smoother 43 p. setAntiAlias (true); 44 45 // obtain the screen x y coordinate 46 float w = getWidth (); 47 float h = getHeight (); 48 49 for (int I = 0; I <6; I ++) {50 if (I = posfirst) {51 52 p. setColor (Color. RED); 53 // draw a RED circle 54 canvas. drawCircle (w-(count-1) * juli)/2 + juli * I, (float) (h * 0.9), radius + 5, p ); 55} else {56 57 p. setColor (Color. GRAY); 58 // draw a GRAY circle 59 canvas. drawCircle (w-(count-1) * juli)/2 + juli * I, (float) (h * 0.9), radius, p ); 60 61} 62 63} 64 65} 66 67}
Tool class:
1 package untils; 2 3 import android. app. activity; 4 import android. view. windowManager; 5 6 public class Untils {7 8 9 public static void toggleFullscreen (Activity activity, boolean fullScreen) {10 // full screen 11 12 WindowManager when fullScreen is true. layoutParams attrs = activity. getWindow (). getAttributes (); 13 14 if (fullScreen) {15 attrs. flags | = WindowManager. layoutParams. FLAG_FULLSCREEN; 16} else {17 attrs. Flags & = ~ WindowManager. LayoutParams. FLAG_FULLSCREEN; 18} 19 20 activity. getWindow (). setAttributes (attrs); 21} 22}
Xml file:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="com.lixu.drawable.WelcomActivity" > 6 7 <com.lixu.drawable.Mydrawable 8 android:id="@+id/mydrawable" 9 android:layout_width="match_parent"10 android:layout_height="match_parent" />11 12 <android.support.v4.view.ViewPager13 android:id="@+id/vp"14 android:layout_width="match_parent"15 android:layout_height="match_parent" >16 </android.support.v4.view.ViewPager>17 18 </RelativeLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <ImageView 8 android:id="@+id/iv1" 9 android:layout_width="match_parent"10 android:layout_height="match_parent" />11 12 </LinearLayout>
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: id = "@ + id/LinearLayout1" 4 android: layout_width = "match_parent" 5 android: layout_height = "match_parent" 6 android: orientation = "vertical"> 7 8 <ImageView 9 android: layout_weight = "4" 10 android: id = "@ + id/iv2" 11 android: layout_width = "match_parent" 12 android: layout_height = "match_parent"/> 13 14 <Button15 android: layout_weight = "1" 16 android: id = "@ + id/btn" 17 android: layout_width = "wrap_content" 18 android: layout_height = "wrap_content" 19 android: layout_gravity = "center" 20 android: text = "go to QQ music"/> 21 22 </LinearLayout>
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical"> 6 7 <TextView 8 android: layout_width = "match_parent" 9 android: layout_height = "match_parent" 10 android: text = "I am the main program interface" 11 android: textSize = "35sp" 12 android: gravity = "center"/> 13 </LinearLayout>
Run: