Android custom components automatically circle with fingers, android custom components

Source: Internet
Author: User

Android custom components automatically circle with fingers, android custom components

First, define a View subclass:

Package com. example. androidtest0.myView; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. util. attributeSet; import android. view. motionEvent; import android. view. view; public class DrawView extends View {public float currentX = 40; public float currentY = 50; // define and create Paint p = new Paint (); public DrawView (Context context) {super (context);} public DrawView (Context context, AttributeSet attrs) {super (context, attrs);} @ Overrideprotected void onDraw (Canvas canvas Canvas) {super. onDraw (canvas); // sets the paint brush color p. setColor (Color. RED); // draw a small ball canvas. drawCircle (currentX, currentY, 15, p);}/*** rewrite event processing method for touch events of the component */@ Overridepublic boolean onTouchEvent (MotionEvent event) {// modify the currentX and currentY attributes currentX = event. getX (); currentY = event. getY (); // notify the current component to redraw its own invalidate (); return true ;}}

Main Interface XML:

Custom_layout.xml

<pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/root"    android:orientation="vertical" ></LinearLayout>


 

Main activity:

Package com. example. androidtest0; import com. example. androidtest0.myView. drawView; import android. app. activity; import android. OS. bundle; import android. widget. linearLayout; public class CustomView extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. custom_layout); // obtain the LinearLayout container LinearLayout root = (LinearLayout) findViewById (R. id. root); // create the DrawView component final DrawView drawView = new DrawView (this); // set the minimum width and height of the custom component drawView. setMinimumWidth (10); drawView. setMinimumHeight (10); root. addView (drawView );}}
Effect:



In addition:

You can also use XML to create a subclass of the View, which is the same as the previous one. Then the main interface XML is as follows:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/root"    android:orientation="vertical" >    <com.example.androidtest0.myView.DrawView         android:layout_width="match_parent" android:layout_height="match_parent"        /></LinearLayout>

The main activity file is as follows:

package com.example.androidtest0;import com.example.androidtest0.myView.DrawView;import android.app.Activity;import android.os.Bundle;import android.widget.LinearLayout;public class CustomView extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.custom_layout);}}



In android, a custom MyView (inherited from the View class) is added as a component in the xml layout file (No error prompt), but an error is reported after running.

The custom <com... MyView.../> is not the root node.
In MainActivity, modify serContentView (new MyView (this ));

Re-painting of the android custom view component

You didn't keep the thread running, so it only runs once and won't keep updating

Related Article

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.