The Android custom control implements the small ball _android that moves with the finger

Source: Internet
Author: User

A small demo about a custom control that moves with the fingers of the ball.
First look at the effect of the picture:

The implementation code is as follows:

1. Custom Control class

Package Com.dc.customview.view;
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;

Import Android.view.WindowManager;
  public class Drawcircle extends View {//The initial position of the circle private int x = 100;
  private int y = 100;
  
  Context context; /** * has a style resource file called * @param context * @param attrs * @param defstyle/Public drawcircle (context Contex
    T, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
  This.context = context;  /** * XML Creation Call * @param context * @param attrs/Public drawcircle (context, AttributeSet attrs)
    {Super (context, attrs);
  This.context = context;
    /** * Java code creation Call * @param context/Public drawcircle (context) {super (context);
  This.context = context; } @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
    Brush Paint Paint = new Paint ();

    Paint.setcolor (color.red);
  Draw the Circle//CX: The x coordinate of the center: the y-coordinate of the center//cy//radius: The radius of the circle//paint: The Brush canvas.drawcircle (x, y, paint); @Override public boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.
          
          Action_down:case MotionEvent.ACTION_MOVE:case motionevent.action_up://Get the x,y coordinates of the current touch point
          x = (int) event.getx ();
        

        y = (int) event.gety ();
    Break
    //Get screen width high windowmanager manager = (WindowManager) context.getsystemservice (Context.window_service);
    int width = Manager.getdefaultdisplay (). GetWidth ();
    
    int heigh = Manager.getdefaultdisplay (). GetHeight (); Redraw the circle, control the ball will not be moved out of the screen if (x>=20 && y>=20 && x<=width-20 && y<=heigh-90) {inval
    Idate ();
    //Handle the touch event yourself return true;

 }
  
}

2. Referencing custom controls

First: References in XML

<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:id= "@+id/rl" >
  
  <!--the full class name of the custom control-->
  <com.dc.customview.view.drawcircle
    Android : id= "@+id/circle"
    android:layout_width= "wrap_content"
    android:layout_height= "wrap_content" >
  </com.dc.customview.view.DrawCircle>

</RelativeLayout>

Second: References in code

Package Com.dc.customview;

Import com.dc.customview.view.DrawCircle;

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.view.View;
Import Android.widget.RelativeLayout;

public class Mainactivity extends activity {

  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_main);
    
    Get the container
    relativelayout container = (relativelayout) Findviewbyid (R.ID.RL);
    
    Create a custom control
    drawcircle circle = new Drawcircle (this);
    
    Add to Container
    Container.addview (circle);
  }




Above, the demo will run to the simulator or mobile phone, you can achieve a red circle, with the finger touch move and move the effect.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.