Android Custom view requires rewriting OnDraw () and other methods

Source: Internet
Author: User

Android Custom view need to rewrite OnDraw () and other methods, this blog to everyone to say that the custom view, we need to inherit the view, and then rewrite some

method, a lot of methods, see what you need


Start by writing a custom view inheritance 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);p aint.setcolor (color.red); Canvas.drawcircle (e_x, e_y, N, 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 activities:

Package com.example.engineerjspview;/** * Custom Engineerjspview * @author engineer-jsp * @date 2014.10.28  * */import and Roid.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:



Customizing the view depends on your needs, not just drawing graphics, I'm just explaining the way to customize the view to rewrite

Android Custom view requires rewriting OnDraw () and other methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.