栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

android浮层图片拖动并且可点击效果

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android浮层图片拖动并且可点击效果

最近产品出了个新需求,页面上出现浮层并且可点击,代码实现如下:

Activity中实现浮层图片:

@Override
  public void onResume() {
    super.onResume();
    createView();   

  }
@Override
public void onPause() {
  super.onPause();  
/ 在程序退出(Activity销毁)时销毁悬浮窗口
  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() {//请求数据

   @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) {//活动的有效期
  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()));//设置图片的网络地址
//  floatView.setImageResource(R.drawable.face_icon); // 这里简单的用自带的icon来做演示

  // 获取WindowManager
  windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
  // 设置LayoutParams(全局变量)相关参数
  windowManagerParams = ((MiGuApplication) getApplication()).getWindowParams();
  windowManagerParams.type = WindowManager.LayoutParams.TYPE_PHONE; // 设置window type
  windowManagerParams.format = PixelFormat.RGBA_8888; // 设置图片格式,效果为背景透明
  // 设置Window flag
  windowManagerParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
      | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
 
  // 调整悬浮窗口至左上角,便于调整坐标
  windowManagerParams.gravity = Gravity.LEFT | Gravity.TOP;
  // 以屏幕左上角为原点,设置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;
  // 设置悬浮窗口长宽数据
  windowManagerParams.width = width;//设置窗口的宽度为图片大小
  windowManagerParams.height =height;//设置窗口的高度为图片大小
//  windowManagerParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
//  windowManagerParams.height =WindowManager.LayoutParams.WRAP_CONTENT;
  // 显示myFloatView图像
  windowManager.addView(floatView, windowManagerParams);
  return;
}

     }
   }

   @Override
   public void failure(RetrofitError error) {//网络请求数据失败
    LOGE(error.getMessage());
   }
 });

  }
  public void onClick(View v) {//图片的点击事件
    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) {
//获取到状态栏的高度
    Rect frame = new Rect();
    getWindowVisibleDisplayframe(frame);
    int statusBarHeight = frame.top;
    System.out.println("statusBarHeight:"+statusBarHeight);
// 获取相对屏幕的坐标,即以屏幕左上角为原点
    x = event.getRawX();
    y = event.getRawY() - statusBarHeight; // statusBarHeight是系统状态栏的高度
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN: // 捕获手指触摸按下动作
// 获取相对View的坐标,即以此View左上角为原点
 mTouchX = event.getX();
 mTouchY = event.getY();
 mStartX = x;
 mStartY = y;
 break;
      case MotionEvent.ACTION_MOVE: // 捕获手指触摸移动动作
 updateViewPosition();
 curtime=System.currentTimeMillis();
 break;
      case MotionEvent.ACTION_UP: // 捕获手指触摸离开动作
// if(System.currentTimeMillis()-curtime>100){
//   break;
// }
 updateViewPosition();
 mTouchX = mTouchY = 0;
 if (Math.abs(x - mStartX) < 5 && Math.abs(y - mStartY) < 5) {//轻微拖动算点击
   if(mClickListener!=null) {
     mClickListener.onClick(this);
   }
 }
 break;
    }
    return true;
  }
  @Override
  public void setonClickListener(onClickListener l) {
    this.mClickListener = l;
  }
  private void updateViewPosition() {
// 更新浮动窗口位置参数
    windowManagerParams.x = (int) (x - mTouchX);
    windowManagerParams.y = (int) (y - mTouchY);
    windowManager.updateViewLayout(this, windowManagerParams); // 刷新显示
  }
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/156836.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号