Android 悬浮按钮的简单实现
16lz
2021-01-23
Android 悬浮按钮的简单实现
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;}});}}
更多相关文章
- Android中Radio单选按钮操作
- Android 自定义控件 按钮滚动选择
- android 登陆右上角 的关闭按钮的使用方法
- Android防止按钮连续点击
- android按钮点击——implements View.OnClickListener
- 通过xml布局文件实现按钮改变焦点设置背景图片
- android 按钮水波纹效果【背景色】
- android 单选按钮组的使用
- android:动态创建多个按钮 并给每个按键添加监听事件