Android custom View needs to override ondraw () and other methods, androidondraw
Android custom View Requires override of ondraw () and other methods. This blog tells you how to write a custom View. We need to inherit the View and then rewrite some
There are many methods, depending on what method you need
First, write a custom View to inherit the View.
Package com. example. engineerjspview; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. util. attributeSet; import android. view. motionEvent;/*** custom EngineerJspView * @ author Engineer-Jsp * @ date 2014.10.28 **/import android. view. view; public class EngineerJspView extends View {public float E_X = 188; public float E_Y = 188; Paint paint = new Paint (); public EngineerJspView (Context context) {super (context);} public EngineerJspView (Context context, AttributeSet set) {super (context, set) ;}@ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); paint. setColor (Color. RED); canvas. drawCircle (E_X, E_Y, 88, paint);} @ Overridepublic boolean onTouchEvent (MotionEvent event) {E_X = event. getX (); E_X = event. getY (); invalidate (); return false ;}@ Overrideprotected void onAnimationStart () {super. onAnimationStart () ;}@ Overrideprotected void onAnimationEnd () {super. onAnimationEnd ();}}
Layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <com.example.engineerjspview.EngineerJspView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /></RelativeLayout>
Main activity:
Package com. example. engineerjspview;/*** custom EngineerJspView * @ author Engineer-Jsp * @ date 2014.10.28 **/import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main );}}
Effect:
Custom View depends on your needs. It is not just about drawing a picture. Here I just want to explain how to rewrite the custom View ~~
For Android, which of the following methods do I need to rewrite imageview and add display text to it? Urgent answer
You can customize an adapter to set the text you want to display in the getview () method of the adapter... There is an example area for Android bus to look ....
How does one use two external classes to draw two different views and display them in the same Canvas?
There are many methods:
1. Call the draw methods of view1 and view2 directly in MainView to pass the Canvas of MainView;
2. Use MainView to inherit the ViewGroup and draw a child View in MainView;
......
In fact, the principle is to draw things on the MainView Canvas, but there is a difference between where to draw.