android浮層圖片拖動(dòng)并且可點(diǎn)擊效果
最近產(chǎn)品出了個(gè)新需求,頁(yè)面上出現(xiàn)浮層并且可點(diǎn)擊,代碼實(shí)現(xiàn)如下:
Activity中實(shí)現(xiàn)浮層圖片:
@Override
public void onResume() {
super.onResume();
createView();
}
@Override
public void onPause() {
super.onPause();
/ 在程序退出(Activity銷毀)時(shí)銷毀懸浮窗口
if(floatView!=null && windowManager !=null) { windowManager.removeView(floatView); floatView=null; windowManager = null; windowManagerParams = null; }}
private void createView() {
if(floatView!=null) return ;
CmsAPI cmsAPI = RestAdapterUtils.getRestAPI(Config.NEW_CMS_URL, CmsAPI.class, this);
cmsAPI.getFloatingAd(new Callback<AdFloating>() {//請(qǐng)求數(shù)據(jù)
@Override
public void success(AdFloating adFloating, Response response) {
if (adFloating != null && "0".equals(adFloating.getErrorCode())) {
long startTime = adFloating.getStarttime();
long endTime = adFloating.getEndtime();
long currentTime = System.currentTimeMillis();
// LOGD(startTime + " +++++ "+endTime +" "+currentTime +" "+(currentTime > startTime && currentTime < endTime));
if (currentTime > startTime && currentTime < endTime) {//活動(dòng)的有效期
floatView = new FloatView(getApplicationContext());
floatView.setOnClickListener(MainActivity.this);
int height = 240;
int width = 110;
float ratio= 1.35f;
if (!TextUtils.isEmpty(adFloating.getImg2())) {
try {
height = Integer.parseInt(adFloating.getImg2h());
width = Integer.parseInt(adFloating.getImg2w());
ratio = (float) width / height;
} catch (Exception e) {
ratio = 1.35f;
}
}
//
floatView.setAspectRatio(ratio);//圖片的大小
floatView.setImageURI(Uri.parse(adFloating.getImg2()));//設(shè)置圖片的網(wǎng)絡(luò)地址
// floatView.setImageResource(R.drawable.face_icon); // 這里簡(jiǎn)單的用自帶的icon來(lái)做演示
// 獲取WindowManager
windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
// 設(shè)置LayoutParams(全局變量)相關(guān)參數(shù)
windowManagerParams = ((MiGuApplication) getApplication()).getWindowParams();
windowManagerParams.type = WindowManager.LayoutParams.TYPE_PHONE; // 設(shè)置window type
windowManagerParams.format = PixelFormat.RGBA_8888; // 設(shè)置圖片格式,效果為背景透明
// 設(shè)置Window flag
windowManagerParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
/*
* 注意,flag的值可以為:
* LayoutParams.FLAG_NOT_TOUCH_MODAL 不影響后面的事件
* LayoutParams.FLAG_NOT_FOCUSABLE 不可聚焦
* LayoutParams.FLAG_NOT_TOUCHABLE 不可觸摸
*/
// 調(diào)整懸浮窗口至左上角,便于調(diào)整坐標(biāo)
windowManagerParams.gravity = Gravity.LEFT | Gravity.TOP;
// 以屏幕左上角為原點(diǎn),設(shè)置x、y初始值
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeigh = dm.heightPixels;
int x = screenWidth - SystemTools.dip2px(MainActivity.this, 100);
int y= screenHeigh - SystemTools.dip2px(MainActivity.this, 200);
windowManagerParams.x = x;
windowManagerParams.y = y;
// 設(shè)置懸浮窗口長(zhǎng)寬數(shù)據(jù)
windowManagerParams.width = width;//設(shè)置窗口的寬度為圖片大小
windowManagerParams.height =height;//設(shè)置窗口的高度為圖片大小
// windowManagerParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
// windowManagerParams.height =WindowManager.LayoutParams.WRAP_CONTENT;
// 顯示myFloatView圖像
windowManager.addView(floatView, windowManagerParams);
return;
}
}
}
@Override
public void failure(RetrofitError error) {//網(wǎng)絡(luò)請(qǐng)求數(shù)據(jù)失敗
LOGE(error.getMessage());
}
});
}
public void onClick(View v) {//圖片的點(diǎn)擊事件
Intent intent = new Intent(MainActivity.this, ActivitiesDetails.class);
startActivity(intent);
}
圖片控件:
public class FloatView extends SimpleDraweeView {
private float mTouchX;
private float mTouchY;
private float x;
private float y;
private float mStartX;
private float mStartY;
private OnClickListener mClickListener;
private WindowManager windowManager = (WindowManager) getContext()
.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
// 此windowManagerParams變量為獲取的全局變量,用以保存懸浮窗口的屬性
private WindowManager.LayoutParams windowManagerParams = ((MiGuApplication) getContext()
.getApplicationContext()).getWindowParams();
public FloatView(Context context) {
super(context);
}
public FloatView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private long curtime=0;
@Override
public boolean onTouchEvent(MotionEvent event) {
//獲取到狀態(tài)欄的高度
Rect frame = new Rect();
getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
System.out.println("statusBarHeight:"+statusBarHeight);
// 獲取相對(duì)屏幕的坐標(biāo),即以屏幕左上角為原點(diǎn)
x = event.getRawX();
y = event.getRawY() - statusBarHeight; // statusBarHeight是系統(tǒng)狀態(tài)欄的高度
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // 捕獲手指觸摸按下動(dòng)作
// 獲取相對(duì)View的坐標(biāo),即以此View左上角為原點(diǎn)
mTouchX = event.getX();
mTouchY = event.getY();
mStartX = x;
mStartY = y;
break;
case MotionEvent.ACTION_MOVE: // 捕獲手指觸摸移動(dòng)動(dòng)作
updateViewPosition();
curtime=System.currentTimeMillis();
break;
case MotionEvent.ACTION_UP: // 捕獲手指觸摸離開(kāi)動(dòng)作
// if(System.currentTimeMillis()-curtime>100){
// break;
// }
updateViewPosition();
mTouchX = mTouchY = 0;
if (Math.abs(x - mStartX) < 5 && Math.abs(y - mStartY) < 5) {//輕微拖動(dòng)算點(diǎn)擊
if(mClickListener!=null) {
mClickListener.onClick(this);
}
}
break;
}
return true;
}
@Override
public void setOnClickListener(OnClickListener l) {
this.mClickListener = l;
}
private void updateViewPosition() {
// 更新浮動(dòng)窗口位置參數(shù)
windowManagerParams.x = (int) (x - mTouchX);
windowManagerParams.y = (int) (y - mTouchY);
windowManager.updateViewLayout(this, windowManagerParams); // 刷新顯示
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開(kāi)發(fā)之使用ExifInterface獲取拍照后的圖片屬性
這篇文章主要介紹了Android開(kāi)發(fā)之使用ExifInterface獲取拍照后的圖片屬性,較為詳細(xì)的分析了ExifInterface類操作圖片的具體使用技巧,需要的朋友可以參考下2016-01-01
Kotlin之自定義 Live Templates詳解(模板代碼)
這篇文章主要介紹了Kotlin之自定義 Live Templates詳解(模板代碼),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android優(yōu)化方案之Fragment的懶加載實(shí)現(xiàn)代碼
本篇文章主要介紹了Android優(yōu)化方案之Fragment的懶加載實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Android Zxing二維碼掃描圖片拉伸問(wèn)題的解決方法
這篇文章主要為大家詳細(xì)介紹了Android Zxing二維碼掃描圖片拉伸問(wèn)題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
新版Android studio導(dǎo)入微信支付和支付寶官方Demo問(wèn)題解決大全
這篇文章主要為大家詳細(xì)介紹了新版Android studio導(dǎo)入微信支付和支付寶官方Demo問(wèn)題的解決大全,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
Android實(shí)現(xiàn)一個(gè)簡(jiǎn)易的帶邊輸入框
如今市面上APP的輸入框可以說(shuō)是千奇百怪,不搞點(diǎn)花樣出來(lái)貌似代表格局沒(méi)打開(kāi),還在使用系統(tǒng)自帶的輸入框的兄弟可以停下腳步,哥帶你實(shí)現(xiàn)一個(gè)簡(jiǎn)易的帶邊框輸入框,感興趣的同學(xué)可以自己動(dòng)手試一下2023-08-08
解決android 顯示內(nèi)容被底部導(dǎo)航欄遮擋的問(wèn)題
今天小編就為大家分享一篇解決android 顯示內(nèi)容被底部導(dǎo)航欄遮擋的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Android進(jìn)程運(yùn)行中權(quán)限被收回導(dǎo)致關(guān)閉的問(wèn)題解決
在Android開(kāi)發(fā)中我們可能會(huì)遇到這樣的問(wèn)題,進(jìn)程還在運(yùn)行著某些權(quán)限卻被收回了,這就導(dǎo)致進(jìn)程崩潰被迫關(guān)閉,本篇文章將帶你了解這個(gè)問(wèn)題的發(fā)生與解決方法2021-10-10

