android 图片增加浮层图片,Android实现悬浮图片

本文实例为大家分享了android实现悬浮图片的具体代码,供大家参考,具体内容如下

@suppresslint("appcompatcustomview")

public class moveimageview extends imageview {

//按下那一刻的坐标和 控件上下左右距离

private float lastx;

private float lasty;

private int left;

private int top;

private int right;

private int bottom;

//如果是拖动事件就不用响应点击事件

boolean ismove = false;

boolean isanimatoring = false;

//屏幕宽高

private int screenwidthpx;

private int screenheightpx;

public moveimageview(context context) {

this(context, null);

}

public moveimageview(context context, @nullable attributeset attrs) {

this(context, attrs, 0);

}

public moveimageview(context context, @nullable attributeset attrs, int defstyleattr) {

super(context, attrs, defstyleattr);

screenwidthpx = getscreenwidthpx(getcontext());

screenheightpx = getscreenheightpx(getcontext());

}

@override

public boolean ontouchevent(motionevent event) {

switch (event.getaction()) {

case motionevent.action_down:

//动画执行过程中,不响应一切操作,

// 这里相当于不让其走后面的motionevent.action_move:与motionevent.action_up:

if (isanimatoring) {

return false;

}

lastx = event.getrawx();

lasty = event.getrawy();

left = getleft();

top = gettop();

right = getright();

bottom = getbottom();

break;

case motionevent.action_move:

ismove = true;

float x = event.getrawx();

float y = event.getrawy();

int l = (int) (left + (x - lastx));

int t = (int) (top + (y - lasty));

int r = (int) (right + (x - lastx));

int b = (int) (bottom + (y - lasty));

layout(l, t, r, b);

break;

case motionevent.action_up:

if (ismove) {

//如果顶部拖出屏幕外面,回正

if (gettop() < 0) {

layout(getleft(), 0, getright(), getheight());

}

//getbottom() 获取到的是 控件底部到父容器顶部的距离,所以需要减去状态栏的高度

int bottomheight = screenheightpx - getstatusbarheight(getcontext());

//如果底部拖出屏幕外面,回正

if (getbottom() > bottomheight) {

layout(getleft(), bottomheight-getheight(), getright(), bottomheight);

}

ismove = false;

startanimation();

return true;

}

return super.ontouchevent(event);

}

return super.ontouchevent(event);

}

private void startanimation() {

isanimatoring = true;

//右边距

int marinright = displayutils.dptopx(20);

int endvalue = screenwidthpx - marinright;

valueanimator animator = valueanimator.ofint(getright(), endvalue);

animator.setduration(math.abs(endvalue - getright()) > 1000 ? 1000 : math.abs(endvalue - getright()));

animator.addupdatelistener(new valueanimator.animatorupdatelistener() {

@override

public void onanimationupdate(valueanimator animation) {

int curvalue = (int) animation.getanimatedvalue();

layout(curvalue - getwidth(), gettop(), curvalue, getheight() + gettop());

}

});

animator.addlistener(new animatorlisteneradapter() {

@override

public void onanimationstart(animator animation) {

}

@override

public void onanimationend(animator animation) {

isanimatoring = false;

animator.removeallupdatelisteners();

animator.removealllisteners();

}

});

animator.start();

}

/**

* 获取状态栏高度

*/

public static int getstatusbarheight(context context) {

int result = 24;

int resid = context.getresources().getidentifier("status_bar_height", "dimen", "android");

if (resid > 0) {

result = context.getresources().getdimensionpixelsize(resid);

} else {

result = (int) typedvalue.applydimension(typedvalue.complex_unit_dip,

result, resources.getsystem().getdisplaymetrics());

}

return result;

}

public static int getscreenwidthpx(context context) {

windowmanager windowmanager = (windowmanager) context.getsystemservice(context.window_service);

displaymetrics dm = new displaymetrics();

if (windowmanager != null) {

// windowmanager.getdefaultdisplay().getmetrics(dm);

windowmanager.getdefaultdisplay().getrealmetrics(dm);

return dm.widthpixels;

}

return 0;

}

public static int getscreenheightpx(context context) {

windowmanager windowmanager = (windowmanager) context.getsystemservice(context.window_service);

displaymetrics dm = new displaymetrics();

if (windowmanager != null) {

// windowmanager.getdefaultdisplay().getmetrics(dm);

windowmanager.getdefaultdisplay().getrealmetrics(dm);

return dm.heightpixels;

}

return 0;

}

}

应用:

布局文件:

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".testactivity">

android:layout_width="50dp"

android:id="@+id/moveimageview"

android:layout_height="50dp"

android:layout_alignparentright="true"

android:layout_alignparentbottom="true"

android:layout_marginbottom="100dp"

android:layout_marginright="20dp"

android:src="#ff0000" />

activity:

public class testactivity extends appcompatactivity {

private moveimageview moveimageview;

@override

protected void oncreate(bundle savedinstancestate) {

super.oncreate(savedinstancestate);

setcontentview(r.layout.activity_test2);

moveimageview = ((moveimageview) findviewbyid(r.id.moveimageview));

moveimageview.setonclicklistener(new view.onclicklistener() {

@override

public void onclick(view v) {

toast.maketext(testactivity.this, "点击事件", toast.length_short).show();

}

});

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值