Custom view (small balls move with your finger)

Source: Internet
Author: User

Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import android.provider.ContactsContract.CommonDataKinds.Event;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.view.View;

Public class MyView extends View {

Paint paint=new paint ();
private int width;
Private int heigh;
Private float currx=0;
Private float curry=0;

Public MyView (context context) {
Super (context);

}

Public MyView (context context, AttributeSet Attrs) {
Super (context, attrs);

}
Public MyView (context context, AttributeSet attrs, int defstyle) {
Super (context, attrs, defstyle);

}

@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
Width=measurespec.getsize (WIDTHMEASURESPEC);
Heigh=measurespec.getsize (HEIGHTMEASURESPEC);
Currx=width/2;
Curry=heigh/2;
}

@Override
protected void OnDraw (canvas canvas) {
Super.ondraw (canvas);
Paint.setcolor (color.red);
Paint.setantialias (TRUE);
Canvas.drawcircle (currx,curry,60, paint);
}
@Override
public boolean ontouchevent (Motionevent event) {

Switch (event.getaction ()) {
Case Motionevent.action_down:
Currx=event.getx ();
Curry=event.gety ();
Postinvalidate ();
Break
Case Motionevent.action_move:
Currx=event.getx ();
Curry=event.gety ();
Postinvalidate ();
Break
Case MOTIONEVENT.ACTION_UP:

Break
Case Motionevent.action_cancel:

Break

}

return true;

}
}

Custom view (small balls move with your finger)

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.