【移動應用開發(fā)技術】Android如何實現(xiàn)自定義可拖拽的懸浮按鈕DragFloatingActionButton_第1頁
【移動應用開發(fā)技術】Android如何實現(xiàn)自定義可拖拽的懸浮按鈕DragFloatingActionButton_第2頁
【移動應用開發(fā)技術】Android如何實現(xiàn)自定義可拖拽的懸浮按鈕DragFloatingActionButton_第3頁
【移動應用開發(fā)技術】Android如何實現(xiàn)自定義可拖拽的懸浮按鈕DragFloatingActionButton_第4頁
【移動應用開發(fā)技術】Android如何實現(xiàn)自定義可拖拽的懸浮按鈕DragFloatingActionButton_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

【移動應用開發(fā)技術】Android如何實現(xiàn)自定義可拖拽的懸浮按鈕DragFloatingActionButton

compile

'com.android.support:design:25.3.1'<android.support.design.widget.FloatingActionButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="right|bottom"

android:src="@drawable/ic_launcher"

/>

/upload/information/20200623/125/129384.pngimport

android.animation.ObjectAnimator;

import

android.content.Context;

import

android.support.design.widget.FloatingActionButton;

import

android.util.AttributeSet;

import

android.util.Log;

import

android.view.MotionEvent;

import

android.view.animation.DecelerateInterpolator;

public

class

DragFloatActionButton

extends

FloatingActionButton

{

private

int

screenWidth;

private

int

screenHeight;

private

int

screenWidthHalf;

private

int

statusHeight;

private

int

virtualHeight;

public

DragFloatActionButton(Context

context)

{

super(context);

init();

}

public

DragFloatActionButton(Context

context,

AttributeSet

attrs)

{

super(context,

attrs);

init();

}

public

DragFloatActionButton(Context

context,

AttributeSet

attrs,

int

defStyleAttr)

{

super(context,

attrs,

defStyleAttr);

init();

}

private

void

init()

{

screenWidth

=

ScreenUtils.getScreenWidth(getContext());

screenWidthHalf

=

screenWidth

/

2;

screenHeight

=

ScreenUtils.getScreenHeight(getContext());

statusHeight

=

ScreenUtils.getStatusHeight(getContext());

virtualHeight=ScreenUtils.getVirtualBarHeigh(getContext());

}

private

int

lastX;

private

int

lastY;

private

boolean

isDrag;

@Override

public

boolean

onTouchEvent(MotionEvent

event)

{

int

rawX

=

(int)

event.getRawX();

int

rawY

=

(int)

event.getRawY();

switch

(event.getAction()

&

MotionEvent.ACTION_MASK)

{

case

MotionEvent.ACTION_DOWN:

isDrag

=

false;

getParent().requestDisallowInterceptTouchEvent(true);

lastX

=

rawX;

lastY

=

rawY;

Log.e("down>",

"getX="

+

getX()

+

";screenWidthHalf="

+

screenWidthHalf);

break;

case

MotionEvent.ACTION_MOVE:

isDrag

=

true;

//計算手指移動了多少

int

dx

=

rawX

-

lastX;

int

dy

=

rawY

-

lastY;

//這里修復一些手機無法觸發(fā)點擊事件的問題

int

distance=

(int)

Math.sqrt(dx*dx+dy*dy);

Log.e("distance>",distance+"");

if(distance<3){//給個容錯范圍,不然有部分手機還是無法點擊

isDrag=false;

break;

}

float

x

=

getX()

+

dx;

float

y

=

getY()

+

dy;

//檢測是否到達邊緣

左上右下

x

=

x

<

0

?

0

:

x

>

screenWidth

-

getWidth()

?

screenWidth

-

getWidth()

:

x;

//

y

=

y

<

statusHeight

?

statusHeight

:

(y

+

getHeight()

>=

screenHeight

?

screenHeight

-

getHeight()

:

y);

if

(y<0){

y=0;

}

if

(y>screenHeight-statusHeight-getHeight()){

y=screenHeight-statusHeight-getHeight();

}

setX(x);

setY(y);

lastX

=

rawX;

lastY

=

rawY;

Log.e("move>",

"getX="

+

getX()

+

";screenWidthHalf="

+

screenWidthHalf

+

"

"

+

isDrag+"

statusHeight="+statusHeight+

"

virtualHeight"+virtualHeight+

"

screenHeight"+

screenHeight+"

getHeight="+getHeight()+"

y"+y);

break;

case

MotionEvent.ACTION_UP:

if

(isDrag)

{

//恢復按壓效果

setPressed(false);

Log.e("ACTION_UP>",

"getX="

+

getX()

+

";screenWidthHalf="

+

screenWidthHalf);

if

(rawX

>=

screenWidthHalf)

{

animate().setInterpolator(new

DecelerateInterpolator())

.setDuration(500)

.xBy(screenWidth

-

getWidth()

-

getX())

.start();

}

else

{

ObjectAnimator

oa

=

ObjectAnimator.ofFloat(this,

"x",

getX(),

0);

oa.setInterpolator(new

DecelerateInterpolator());

oa.setDuration(500);

oa.start();

}

}

Log.e("up>",isDrag+"");

break;

}

//如果是拖拽則消耗事件,否則正常傳遞即可。

return

isDrag

||

super.onTouchEvent(event);

}

}package

com.example.cmos.retrofitdemo;

import

android.app.Activity;

import

android.content.Context;

import

android.graphics.Rect;

import

android.util.DisplayMetrics;

import

android.view.Display;

import

android.view.Window;

import

android.view.WindowManager;

import

java.lang.reflect.Method;

/**

*

Created

by

gongwq

on

2017/6/14

0014.

*/

public

class

ScreenUtils

{

private

ScreenUtils()

{

/*

cannot

be

instantiated

*/

throw

new

UnsupportedOperationException("cannot

be

instantiated");

}

/**

*

獲得屏幕高度

*

*

@param

context

*

@return

*/

public

static

int

getScreenWidth(Context

context)

{

WindowManager

wm

=

(WindowManager)

context

.getSystemService(Context.WINDOW_SERVICE);

DisplayMetrics

outMetrics

=

new

DisplayMetrics();

wm.getDefaultDisplay().getMetrics(outMetrics);

return

outMetrics.widthPixels;

}

/**

*

獲得屏幕寬度

*

*

@param

context

*

@return

*/

public

static

int

getScreenHeight(Context

context)

{

WindowManager

wm

=

(WindowManager)

context

.getSystemService(Context.WINDOW_SERVICE);

DisplayMetrics

outMetrics

=

new

DisplayMetrics();

wm.getDefaultDisplay().getMetrics(outMetrics);

return

outMetrics.heightPixels;

}

/**

*

獲得狀態(tài)欄的高度

*

*

@param

context

*

@return

*/

public

static

int

getStatusHeight(Context

context)

{

int

statusHeight

=

-1;

try

{

Class<?>

clazz

=

Class.forName("ernal.R$dimen");

Object

object

=

clazz.newInstance();

int

height

=

Integer.parseInt(clazz.getField("status_bar_height")

.get(object).toString());

statusHeight

=

context.getResources().getDimensionPixelSize(height);

}

catch

(Exception

e)

{

e.printStackTrace();

}

return

statusHeight;

}

/**

*

獲取虛擬功能鍵高度

*/

public

static

int

getVirtualBarHeigh(Context

context)

{

int

vh

=

0;

WindowManager

windowManager

=

(WindowManager)

context.getSystemService(Context.WINDOW_SERVICE);

Display

display

=

windowManager.getDefaultDisplay();

DisplayMetrics

dm

=

new

DisplayMetrics();

try

{

@SuppressWarnings("rawtypes")

Class

c

=

Class.forName("android.view.Display");

@SuppressWarnings("unchecked")

Method

method

=

c.getMethod("getRealMetrics",

DisplayMetrics.class);

method.invoke(display,

dm);

vh

=

dm.heightPixels

-

windowManager.getDefaultDisplay().getHeight();

}

catch

(Exception

e)

{

e.printStackTrace();

}

return

vh;

}

public

static

int

getVirtualBarHeigh(Activity

activity)

{

int

titleHeight

=

0;

Rect

frame

=

new

Rect();

activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

int

statusHeight

=

frame.top;

titleHeight

=

activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop()

-

statusHeight;

return

titleHeight;

}

}dragFloatActionButton=

(DragFloatActionButton)

findViewById(R.id.floatBtn);

dragFloatActionButton.setOnClickListener(this);

@Override

public

void

onClick(View

view)

{

switch

(view.getId())

{

case

R.id.floatBtn:

PopupMenu

popupMenu=new

PopupMenu(this,view);

getMenuInflater().inflate(R.menu.pop_item,popupMenu.getMenu());

popupMenu.setOnMenuItemClickListener(new

PopupMenu.OnMenuItemClickListener()

{

@Override

public

boolean

onMenuItemClick(MenuItem

menuItem)

{

switch

(menuItem.getItemId()){

case

R.id.action_last:

Toast.makeText(TestActivity.this,""+menuItem.getItemId(),Toast.LENGTH_SHORT).show();

break;

case

R.id.action_next:

Toast.makeText(TestActivity.this,""+menuItem.getItemId(),Toast.LENGTH_SHORT).show();

break;

}

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論