热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

GuideView的封装实现app功能引导页

这篇文章主要为大家详细介绍了GuideView的封装实现app功能引导页,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了GuideView的封装实现app功能引导页的具体代码,供大家参考,具体内容如下

package oschina.comxianbing100.yindao;
 
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
 
import java.util.List;
 
/**
 * author : Majunbao
 * date : 2019/3/4 16:32
 * description :App第一次打开功能蒙版 引导
 */
public class GuideView extends RelativeLayout implements ViewTreeObserver.OnGlobalLayoutListener {
 private final String TAG = getClass().getSimpleName();
 private Context mContent;
 private List mViews;
 private boolean first = true;
 /**
  * targetView前缀。SHOW_GUIDE_PREFIX + targetView.getId()作为保存在SP文件的key。
  */
 private static final String SHOW_GUIDE_PREFIX = "show_guide_on_view_";
 /**
  * GuideView 偏移量
  */
 private int offsetX, offsetY;
 /**
  * targetView 的外切圆半径
  */
 private int radius;
 /**
  * 需要显示提示信息的View
  */
 private View targetView;
 /**
  * 自定义View
  */
 private View customGuideView;
 /**
  * 透明圆形画笔
  */
 private Paint mCirclePaint;
 /**
  * 背景色画笔
  */
 private Paint mBackgroundPaint;
 /**
  * targetView是否已测量
  */
 private boolean isMeasured;
 /**
  * targetView圆心
  */
 private int[] center;
 /**
  * 绘图层叠模式
  */
 private PorterDuffXfermode porterDuffXfermode;
 /**
  * 绘制前景bitmap
  */
 private Bitmap bitmap;
 /**
  * 背景色和透明度,格式 #aarrggbb
  */
 private int backgroundColor;
 /**
  * Canvas,绘制bitmap
  */
 private Canvas temp;
 /**
  * 相对于targetView的位置.在target的那个方向
  */
 private Direction direction;
 
 /**
  * 形状
  */
 private MyShape myShape;
 /**
  * targetView左上角坐标
  */
 private int[] location;
 private boolean onClickExit;
 private OnClickCallback onclickListener;
 private RelativeLayout guideViewLayout;
 
 public void restoreState() {
  Log.v(TAG, "restoreState");
  offsetX = offsetY = 0;
  radius = 0;
  mCirclePaint = null;
  mBackgroundPaint = null;
  isMeasured = false;
  center = null;
  porterDuffXfermode = null;
  bitmap = null;
  needDraw = true;
  //  backgroundColor = Color.parseColor("#00000000");
  temp = null;
  //  direction = null;
 
 }
 
 public int[] getLocation() {
  return location;
 }
 
 public void setLocation(int[] location) {
  this.location = location;
 }
 
 public GuideView(Context context) {
  super(context);
  this.mCOntent= context;
  init();
 }
 
 public int getRadius() {
  return radius;
 }
 
 public void setRadius(int radius) {
  this.radius = radius;
 }
 
 public void setOffsetX(int offsetX) {
  this.offsetX = offsetX;
 }
 
 public void setOffsetY(int offsetY) {
  this.offsetY = offsetY;
 }
 
 public void setDirection(Direction direction) {
  this.direction = direction;
 }
 
 public void setShape(MyShape shape) {
  this.myShape = shape;
 }
 
 public void setCustomGuideView(View customGuideView) {
  this.customGuideView = customGuideView;
  if (!first) {
   restoreState();
  }
 }
 
 public void setBgColor(int background_color) {
  this.backgroundColor = background_color;
 }
 
 public View getTargetView() {
  return targetView;
 }
 
 public void setTargetView(View targetView) {
  this.targetView = targetView;
  //  restoreState();
  if (!first) {
   //   guideViewLayout.removeAllViews();
  }
 }
 
 private void init() {
 }
 
 public void showOnce() {
  if (targetView != null) {
   mContent.getSharedPreferences(TAG, Context.MODE_PRIVATE).edit().putBoolean(generateUniqId(targetView), true).commit();
  }
 }
 
 private boolean hasShown() {
  if (targetView == null)
   return true;
  return mContent.getSharedPreferences(TAG, Context.MODE_PRIVATE).getBoolean(generateUniqId(targetView), false);
 }
 
 private String generateUniqId(View v) {
  return SHOW_GUIDE_PREFIX + v.getId();
 }
 
 public int[] getCenter() {
  return center;
 }
 
 public void setCenter(int[] center) {
  this.center = center;
 }
 
 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
 public void hide() {
  Log.v(TAG, "hide");
  if (customGuideView != null) {
   targetView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
   this.removeAllViews();
   ((FrameLayout) ((Activity) mContent).getWindow().getDecorView()).removeView(this);
   restoreState();
  }
 }
 
 public void show() {
  Log.v(TAG, "show");
  if (hasShown())
   return;
 
  if (targetView != null) {
   targetView.getViewTreeObserver().addOnGlobalLayoutListener(this);
  }
 
  this.setBackgroundResource(R.color.transparent);
 
  ((FrameLayout) ((Activity) mContent).getWindow().getDecorView()).addView(this);
  first = false;
 }
 
 /**
  * 添加提示文字,位置在targetView的下边
  * 在屏幕窗口,添加蒙层,蒙层绘制总背景和透明圆形,圆形下边绘制说明文字
  */
 private void createGuideView() {
  Log.v(TAG, "createGuideView");
 
  // 添加到蒙层
  //  if (guideViewLayout == null) {
  //   guideViewLayout = new RelativeLayout(mContent);
  //  }
 
  // Tips布局参数
  LayoutParams guideViewParams;
  guideViewParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  guideViewParams.setMargins(0, center[1] + radius + 10, 0, 0);
 
  if (customGuideView != null) {
 
   //   LayoutParams guideViewParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
   if (direction != null) {
    int width = this.getWidth();
    int height = this.getHeight();
 
    int left = center[0] - radius;
    int right = center[0] + radius;
    int top = center[1] - radius;
    int bottom = center[1] + radius;
    switch (direction) {
     case TOP:
      this.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
      guideViewParams.setMargins(offsetX, offsetY - height + top, -offsetX, height - top - offsetY);
      break;
     case LEFT:
      this.setGravity(Gravity.RIGHT);
      guideViewParams.setMargins(offsetX - width + left, top + offsetY, width - left - offsetX, -top - offsetY);
      break;
     case BOTTOM:
      this.setGravity(Gravity.CENTER_HORIZONTAL);
      guideViewParams.setMargins(offsetX, bottom + offsetY, -offsetX, -bottom - offsetY);
      break;
     case RIGHT:
      guideViewParams.setMargins(right + offsetX, top + offsetY, -right - offsetX, -top - offsetY);
      break;
     case LEFT_TOP:
      this.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
      guideViewParams.setMargins(offsetX - width + left, offsetY - height + top, width - left - offsetX, height - top - offsetY);
      break;
     case LEFT_BOTTOM:
      this.setGravity(Gravity.RIGHT);
      guideViewParams.setMargins(offsetX - width + left, bottom + offsetY, width - left - offsetX, -bottom - offsetY);
      break;
     case RIGHT_TOP:
      this.setGravity(Gravity.BOTTOM);
      guideViewParams.setMargins(right + offsetX, offsetY - height + top, -right - offsetX, height - top - offsetY);
      break;
     case RIGHT_BOTTOM:
      guideViewParams.setMargins(right + offsetX, bottom + offsetY, -right - offsetX, -top - offsetY);
      break;
    }
   } else {
    guideViewParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    guideViewParams.setMargins(offsetX, offsetY, -offsetX, -offsetY);
   }
 
   //   guideViewLayout.addView(customGuideView);
 
   this.addView(customGuideView, guideViewParams);
  }
 }
 
 /**
  * 获得targetView 的宽高,如果未测量,返回{-1, -1}
  *
  * @return
  */
 private int[] getTargetViewSize() {
  int[] location = {-1, -1};
  if (isMeasured) {
   location[0] = targetView.getWidth();
   location[1] = targetView.getHeight();
  }
  return location;
 }
 
 /**
  * 获得targetView 的半径
  *
  * @return
  */
 private int getTargetViewRadius() {
  if (isMeasured) {
   int[] size = getTargetViewSize();
   int x = size[0];
   int y = size[1];
 
   return (int) (Math.sqrt(x * x + y * y) / 2);
  }
  return -1;
 }
 
 boolean needDraw = true;
 
 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  Log.v(TAG, "onDraw");
 
  if (!isMeasured)
   return;
 
  if (targetView == null)
   return;
 
  //  if (!needDraw) return;
 
  drawBackground(canvas);
 
 }
 
 private void drawBackground(Canvas canvas) {
  Log.v(TAG, "drawBackground");
  needDraw = false;
  // 先绘制bitmap,再将bitmap绘制到屏幕
  bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
  temp = new Canvas(bitmap);
 
  // 背景画笔
  Paint bgPaint = new Paint();
  if (backgroundColor != 0)
   bgPaint.setColor(backgroundColor);
  else
   bgPaint.setColor(getResources().getColor(R.color.shadow));
 
  // 绘制屏幕背景
  temp.drawRect(0, 0, temp.getWidth(), temp.getHeight(), bgPaint);
 
  // targetView 的透明圆形画笔
  if (mCirclePaint == null)
   mCirclePaint = new Paint();
  porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT);// 或者CLEAR
  mCirclePaint.setXfermode(porterDuffXfermode);
  mCirclePaint.setAntiAlias(true);
 
  if (myShape != null) {
   RectF oval = new RectF();
   switch (myShape) {
    case CIRCULAR://圆形
     temp.drawCircle(center[0], center[1], radius, mCirclePaint);//绘制圆形
     break;
    case ELLIPSE://椭圆
     //RectF对象
     oval.left = center[0] - 150;        //左边
     oval.top = center[1] - 50;         //上边
     oval.right = center[0] + 150;        //右边
     oval.bottom = center[1] + 50;        //下边
     temp.drawOval(oval, mCirclePaint);     //绘制椭圆
     break;
    case RECTANGULAR://圆角矩形
     //RectF对象
     oval.left = center[0] - 150;        //左边
     oval.top = center[1] - 50;         //上边
     oval.right = center[0] + 150;        //右边
     oval.bottom = center[1] + 50;        //下边
     temp.drawRoundRect(oval, radius, radius, mCirclePaint);     //绘制圆角矩形
     break;
   }
  } else {
   temp.drawCircle(center[0], center[1], radius, mCirclePaint);//绘制圆形
  }
 
  // 绘制到屏幕
  canvas.drawBitmap(bitmap, 0, 0, bgPaint);
  bitmap.recycle();
 }
 
 public void setOnClickExit(boolean onClickExit) {
  this.OnClickExit= onClickExit;
 }
 
 public void setOnclickListener(OnClickCallback onclickListener) {
  this.OnclickListener= onclickListener;
 }
 
 private void setClickInfo() {
  final boolean exit = onClickExit;
  setOnClickListener(new OnClickListener() {
   @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
   @Override
   public void onClick(View v) {
    if (onclickListener != null) {
     onclickListener.onClickedGuideView();
    }
    if (exit) {
     hide();
    }
   }
  });
 }
 
 @Override
 public void onGlobalLayout() {
  if (isMeasured)
   return;
  if (targetView.getHeight() > 0 && targetView.getWidth() > 0) {
   isMeasured = true;
  }
 
  // 获取targetView的中心坐标
  if (center == null) {
   // 获取右上角坐标
   location = new int[2];
   targetView.getLocationInWindow(location);
   center = new int[2];
   // 获取中心坐标
   center[0] = location[0] + targetView.getWidth() / 2;
   center[1] = location[1] + targetView.getHeight() / 2;
  }
  // 获取targetView外切圆半径
  if (radius == 0) {
   radius = getTargetViewRadius();
  }
  // 添加GuideView
  createGuideView();
 }
 
 /**
  * 定义GuideView相对于targetView的方位,共八种。不设置则默认在targetView下方
  */
 enum Direction {
  LEFT, TOP, RIGHT, BOTTOM,
  LEFT_TOP, LEFT_BOTTOM,
  RIGHT_TOP, RIGHT_BOTTOM
 }
 
 /**
  * 定义目标控件的形状,共3种。圆形,椭圆,带圆角的矩形(可以设置圆角大小),不设置则默认是圆形
  */
 enum MyShape {
  CIRCULAR, ELLIPSE, RECTANGULAR
 }
 
 /**
  * GuideView点击Callback
  */
 interface OnClickCallback {
  void onClickedGuideView();
 }
 
 public static class Builder {
  static GuideView guiderView;
  static Builder instance = new Builder();
  Context mContext;
 
  private Builder() {
  }
 
  public Builder(Context ctx) {
   mCOntext= ctx;
  }
 
  public static Builder newInstance(Context ctx) {
   guiderView = new GuideView(ctx);
   return instance;
  }
 
  public Builder setTargetView(View target) {
   guiderView.setTargetView(target);
   return instance;
  }
 
  public Builder setBgColor(int color) {
   guiderView.setBgColor(color);
   return instance;
  }
 
  public Builder setDirction(Direction dir) {
   guiderView.setDirection(dir);
   return instance;
  }
 
  public Builder setShape(MyShape shape) {
   guiderView.setShape(shape);
   return instance;
  }
 
  public Builder setOffset(int x, int y) {
   guiderView.setOffsetX(x);
   guiderView.setOffsetY(y);
   return instance;
  }
 
  public Builder setRadius(int radius) {
   guiderView.setRadius(radius);
   return instance;
  }
 
  public Builder setCustomGuideView(View view) {
   guiderView.setCustomGuideView(view);
   return instance;
  }
 
  public Builder setCenter(int X, int Y) {
   guiderView.setCenter(new int[]{X, Y});
   return instance;
  }
 
  public Builder showOnce() {
   guiderView.showOnce();
   return instance;
  }
 
  public GuideView build() {
   guiderView.setClickInfo();
   return guiderView;
  }
 
  public Builder setOnclickExit(boolean onclickExit) {
   guiderView.setOnClickExit(onclickExit);
   return instance;
  }
 
  public Builder setOnclickListener(final OnClickCallback callback) {
   guiderView.setOnclickListener(callback);
   return instance;
  }
 }
}

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


推荐阅读
  • Java学习笔记之使用反射+泛型构建通用DAO
    本文介绍了使用反射和泛型构建通用DAO的方法,通过减少代码冗余度来提高开发效率。通过示例说明了如何使用反射和泛型来实现对不同表的相同操作,从而避免重复编写相似的代码。该方法可以在Java学习中起到较大的帮助作用。 ... [详细]
  • 原理:dismiss再弹出,把dialog设为全局对象。if(dialog!null&&dialog.isShowing()&&!(Activity.)isFinishing()) ... [详细]
  • 本文详细介绍了在Centos7上部署安装zabbix5.0的步骤和注意事项,包括准备工作、获取所需的yum源、关闭防火墙和SELINUX等。提供了一步一步的操作指南,帮助读者顺利完成安装过程。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • iOS Swift中如何实现自动登录?
    本文介绍了在iOS Swift中如何实现自动登录的方法,包括使用故事板、SWRevealViewController等技术,以及解决用户注销后重新登录自动跳转到主页的问题。 ... [详细]
  • SpringBoot整合SpringSecurity+JWT实现单点登录
    SpringBoot整合SpringSecurity+JWT实现单点登录,Go语言社区,Golang程序员人脉社 ... [详细]
  • LVS实现负载均衡的原理LVS负载均衡负载均衡集群是LoadBalance集群。是一种将网络上的访问流量分布于各个节点,以降低服务器压力,更好的向客户端 ... [详细]
  • GSIOpenSSH PAM_USER 安全绕过漏洞
    漏洞名称:GSI-OpenSSHPAM_USER安全绕过漏洞CNNVD编号:CNNVD-201304-097发布时间:2013-04-09 ... [详细]
  • 本文介绍了在RHEL 7中的系统日志管理和网络管理。系统日志管理包括rsyslog和systemd-journal两种日志服务,分别介绍了它们的特点、配置文件和日志查询方式。网络管理主要介绍了使用nmcli命令查看和配置网络接口的方法,包括查看网卡信息、添加、修改和删除配置文件等操作。 ... [详细]
  • Python脚本编写创建输出数据库并添加模型和场数据的方法
    本文介绍了使用Python脚本编写创建输出数据库并添加模型数据和场数据的方法。首先导入相应模块,然后创建输出数据库并添加材料属性、截面、部件实例、分析步和帧、节点和单元等对象。接着向输出数据库中添加场数据和历程数据,本例中只添加了节点位移。最后保存数据库文件并关闭文件。文章还提供了部分代码和Abaqus操作步骤。另外,作者还建立了关于Abaqus的学习交流群,欢迎加入并提问。 ... [详细]
  •     这里使用自己编译的hadoop-2.7.0版本部署在windows上,记得几年前,部署hadoop需要借助于cygwin,还需要开启ssh服务,最近发现,原来不需要借助cy ... [详细]
  • 大坑|左上角_pycharm连接服务器同步写代码(图文详细过程)
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了pycharm连接服务器同步写代码(图文详细过程)相关的知识,希望对你有一定的参考价值。pycharm连接服务 ... [详细]
  • Hadoop2.6.0 + 云centos +伪分布式只谈部署
    3.0.3玩不好,现将2.6.0tar.gz上传到usr,chmod-Rhadoop:hadophadoop-2.6.0,rm掉3.0.32.在etcp ... [详细]
  • linux 禁止指定ip访问
    linux中如何禁止指定的ip访问呢?比如被别人暴力破解,被别人使用不同的密码尝试登录:所以我想直接禁用这些ip的访问.怎么办呢?解决方案:修改配置文件etchosts.deny把 ... [详细]
  • 一、修改注册表去掉桌面图标小箭头1按下win+R组合快捷键,打开windows10系统的“运行”窗口,输入“regedit”,打开注册表编辑器,找到HKEY_CLASSES_ROOT\lnkfi ... [详细]
author-avatar
值兰修女_662
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有