Simple implementation of Android floating button, android floating button

Source: Internet
Author: User

Simple implementation of Android floating button, android floating button

Simple implementation of Android floating button


package com.example.doinbackground;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.Button;public class MainActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final Button bn = (Button) findViewById(R.id.bn);bn.setOnTouchListener(new OnTouchListener() {int[] temp = new int[] { 0, 0 };public boolean onTouch(View v, MotionEvent event) {int eventaction = event.getAction();int x = (int) event.getRawX();int y = (int) event.getRawY();switch (eventaction) {case MotionEvent.ACTION_DOWN: // touch down so check if thetemp[0] = (int) event.getX();temp[1] = y - v.getTop();break;case MotionEvent.ACTION_MOVE: // touch drag with the ballv.layout(x - temp[0], y - temp[1], x + v.getWidth()- temp[0], y - temp[1] + v.getHeight());// v.postInvalidate();break;case MotionEvent.ACTION_UP:break;}return false;}});}}


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.