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

 更新时间:2017年12月25日 08:39:49   作者:欲疯狂地狱  
这篇文章主要为大家详细介绍了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<AdFloating>() {//请求数据

                 @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;
                      /*
                      * 注意,flag的值可以为:
                      * LayoutParams.FLAG_NOT_TOUCH_MODAL 不影响后面的事件
                      * LayoutParams.FLAG_NOT_FOCUSABLE 不可聚焦
                      * LayoutParams.FLAG_NOT_TOUCHABLE 不可触摸
                      */
                       // 调整悬浮窗口至左上角,便于调整坐标
                       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); // 刷新显示
  }
}

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

相关文章

  • android读写cookie的方法示例

    android读写cookie的方法示例

    这篇文章主要介绍了android读写cookie的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • Android实现视频的画中画功能

    Android实现视频的画中画功能

    手机观看视频的时候,有些工作需要沟通,或者参与抢购活动,同时为了不错过视频精彩片段,会选择画中画模式,这篇文章主要为大家详细介绍了Android实现视频的画中画功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • Android SwipereFreshLayout下拉刷新

    Android SwipereFreshLayout下拉刷新

    这篇文章主要介绍了Android SwipereFreshLayout下拉刷新的相关资料,需要的朋友可以参考下
    2017-06-06
  • android事件总线EventBus3.0使用方法详解

    android事件总线EventBus3.0使用方法详解

    这篇文章主要为大家详细介绍了android事件总线EventBus3.0使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • Android页面中引导蒙层的使用方法详解

    Android页面中引导蒙层的使用方法详解

    这篇文章主要为大家详细介绍了Android页面中的引导蒙层使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • Android 登录Web 时对cookie 处理

    Android 登录Web 时对cookie 处理

    本文主要介绍 Android登录web时对cookie的处理方法,这里cookie 的读写做了详细介绍,并附有代码进行讲解,希望能帮到有需要的同学
    2016-07-07
  • android 字体颜色选择器(ColorPicker)介绍

    android 字体颜色选择器(ColorPicker)介绍

    本文将详细介绍android 字体颜色选择器(ColorPicker)需要了解更多的朋友可以参考下
    2012-11-11
  • Flutter路由守卫拦截的实现

    Flutter路由守卫拦截的实现

    路由守卫拦截最常见的应用场景就是对用户数据权限的校验,本文主要介绍了Flutter路由守卫拦截的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • Android控件之菜单的创建方式

    Android控件之菜单的创建方式

    本文给大家分享android控件菜单的两种创建方式,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-09-09
  • Android实现倒计时效果

    Android实现倒计时效果

    这篇文章主要为大家详细介绍了Android实现倒计时效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-10-10

最新评论