Android custom widget _ custom widget and android custom widget
Controls: Self-painted controls, composite controls, and inherited controls
How does one customize the View in each mode?
1. Self-painted controls
Inherit the view, override the onDraw method, and reference it in the layout file.
Example: WaterRipplesActivity
1 public class WaterRipplesActivity extends Activity {2 @Override3 protected void onCreate(Bundle savedInstanceState) {4 super.onCreate(savedInstanceState);5 setContentView(R.layout.main_ring_layout);6 }
WaterRipplesActivity loads the LayoutMain_ring_layout.xml
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=".MainActivity" > 6 7 <com.example.userview.MyRingView 8 android:layout_width="match_parent" 9 android:layout_height="match_parent"10 />11 </RelativeLayout>
When loadedWhen using the com. example. userview. MyRingView control, it means to load the custom control and then execute the class of MyRingView to draw the view. All the painting operations are completed in the MyRingView class.
1 public class MyRingView extends View {2 3 private static final int FLUSH = 0; 4 5 public MyRingView (Context context, AttributeSet attrs) {6 super (context, attrs ); 7} 8 9/** 10 * radius of the Garden Ring 11 */12 private float radius; 13/** 14 * Paint brush for the Garden Ring 15 */16 private paint; 17 18 private void initView () {19 radius = 0; 20 paint = new Paint (); 21 // turn on the anti-moment tooth effect 22 paint. setAntiAlias (true); 23 // set the painting style to draw a line, 24 paint. setStyle (Style. STROKE); 25 // set the width of the line to 26 paint. setStrokeWidth (0); 27 // set the paint transparency 28 paint. setAlpha (255); 29 // set the color to 30 paint. setColor (Color. RED); 31 32 invalidate (); 33} 34 35 private Handler handler = new Handler () {36 public void handleMessage (android. OS. message msg) {37 // modify the Garden Ring status, 38 radius + = 5; 39 40 paint. setStrokeWidth (radius/3); 41 // regardless of the alpha value, the paint will convert it to the number 42 int alpha = paint between 0--255. getAlpha (); 43 44 alpha-= 10; 45 46 if (alpha <10) {47 alpha = 0; 48} 49 paint. setAlpha (alpha); 50 // refresh view 51 invalidate (); 52 53 }}; 54 55 @ Override56/** 57 * draw view content 58 */59 protected void onDraw (Canvas canvas) {60 if (paint = null) {61 return; 62} 63 canvas. drawCircle (cx, cy, radius, paint); 64 65 if (paint. getAlpha ()> 0) {66 // handler. sendEmptyMessage (FLUSH); 67 handler. sendEmptyMessageDelayed (FLUSH, 100); 68} 69} 70 71/** 72 * X coordinate 73 */74 private int cx; 75/** 76 * Y coordinate 77 */78 private int cy; 79 80 @ Override81 public boolean onTouchEvent (MotionEvent event) {82 if (event. getAction () = MotionEvent. ACTION_DOWN) {83 cx = (int) event. getX (); 84 cy = (int) event. getY (); 85 initView (); 86} 87 88 return super. onTouchEvent (event); 89} 90 91 92 @ Override93 protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {94 // test 95 super according to the system's default rules. onMeasure (widthMeasureSpec, heightMeasureSpec); 96}
Effect: the Red Circle ranges from small to large, and the color ranges from deep to light (from left to right)
Second: Composite controls
About composite controls: see (I think this is probably the Model)
Summary:
The overall feeling is that the user-defined definition is similar. The specific implementation is reflected in the three words "user-defined ".
1. The "Custom" of the custom control is to dynamically draw the view in the code and then fill in
2. "Custom" of the combined control: combines the native control.
3. inherit the "Custom" of the control: Inherit the native control and make some modifications.