Simple custom View, custom view

Source: Internet
Author: User

Simple custom View, custom view

For a simple custom View, see Li Gang's crazy Andriod

package com.utils.MyDemo;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;public class MyView extends View {public float currentX=40;public float currentY=40;public MyView(Context context) {super(context);}public MyView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}public MyView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);Paint paint=new Paint();paint.setColor(Color.GREEN);canvas.drawCircle(currentX, currentX, 15, paint);}}
The primary requirement is to re-create the parent class method. Do not delete the methods.

The XML reference is as follows:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <com.utils.MyDemo.MyView       android:id="@+id/myView"        android:layout_height="wrap_content"         android:layout_width="wrap_content"     />   </LinearLayout>

Full name: package name + class name

Modify in Activity

public class MainActivity extends Activity {    /** Called when the activity is first created. */private MyView myView=null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        myView=(MyView)this.findViewById(R.id.myView);        myView.setMinimumHeight(500);        myView.setMinimumWidth(300);        myView.setOnTouchListener(new View.OnTouchListener() {public boolean onTouch(View arg0, MotionEvent event) {myView.currentX=event.getX();myView.currentY=event.getY();myView.invalidate();return true;}});    }}

The above custom MyView is mainly completed. When you move the event with the mouse clicking, MyView moves with the circle.

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.