本文实例为大家分享了Android实现新手引导半透明蒙层效果的具体代码,供大家参考,具体内容如下

效果图:


其中的文字和我知道啦是ui切得两张透明图片

自定义View:

package com.cymobi.library.view.widget;

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.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 com.cymobi.library.R;

/**
 * Created by xuke on 2017/8/24.
 */

public class GuideView extends RelativeLayout implements ViewTreeObserver.OnGlobalLayoutListener {
  private final String TAG = getClass().getSimpleName();
  private Context mContent;
  private boolean first = true;
  private static final String SHOW_GUIDE_PREFIX = "show_guide";
  private int offsetX, offsetY;
  private int radius;
  private View targetView;
  private View textGuideView;
  private View customGuideView;
  private Paint mCirclePaint;
  private Paint mBackgroundPaint;
  private boolean isMeasured;
  private int[] center;
  private PorterDuffXfermode porterDuffXfermode;
  private Bitmap bitmap;
  private int backgroundColor;
  private Canvas temp;
  private Direction direction;
  private MyShape myShape;
  private int[] location;
  private boolean onClickExit;
  private OnClickCallback onclickListener;
  private int targetViewWidth;
  private int targetViewHeight;
  private boolean isContain = false;
  private boolean needDraw = true;


  public GuideView(Context context) {
    super(context);
    this.mContent = context;
  }

  public int[] getLocation() {
    return location;
  }

  public void setLocation(int[] location) {
    this.location = location;
  }

  public int getRadius() {
    return radius;
  }

  public void setRadius(int radius) {
    this.radius = radius;
  }

  public void setDirection(Direction direction) {
    this.direction = direction;
  }

  public void setShape(MyShape shape) {
    this.myShape = shape;
  }

  public void setBgColor(int background_color) {
    this.backgroundColor = background_color;
  }

  public void setTargetView(View targetView) {
    this.targetView = targetView;
  }

  public int[] getCenter() {
    return center;
  }

  public void setCenter(int[] center) {
    this.center = center;
  }

  public void setOffsetX(int offsetX) {
    this.offsetX = offsetX;
  }

  public void setOffsetY(int offsetY) {
    this.offsetY = offsetY;
  }

  public void setContain(boolean contain) {
    this.isContain = contain;
  }

  public void setCustomGuideView(View customGuideView) {
    this.customGuideView = customGuideView;
    if (!first) {
      restoreState();
    }
  }

  public void setTextGuideView(View textGuideView) {
    this.textGuideView = textGuideView;
    if (!first) {
      restoreState();
    }
  }

  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 void setOnclickListener(OnClickCallback onclickListener) {
    this.onclickListener = onclickListener;
  }

  private void setClickInfo() {
    final boolean exit = onClickExit;
    setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if (onclickListener != null) {
          onclickListener.onClickedGuideView();
        }
        if (exit) {
          hide();
        }
      }
    });
  }

  public void show() {
    Log.v(TAG, "show");
    if (hasShown())
      return;

    if (targetView != null) {
      targetView.getViewTreeObserver().addOnGlobalLayoutListener(this);
    }

    this.setBackgroundResource(R.color.transparent);
    this.bringToFront(); //设置在最上层
    ((FrameLayout) ((Activity) mContent).getWindow().getDecorView()).addView(this);
    first = false;
  }

  public void hide() {
    Log.v(TAG, "hide");
    if (customGuideView != null || textGuideView != null) {
      targetView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
      this.removeAllViews();
      ((FrameLayout) ((Activity) mContent).getWindow().getDecorView()).removeView(this);
      restoreState();
    }
  }

  /**
   * 获得targetView 的宽高
   *
   * @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;
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Log.v(TAG, "onDraw");
    if (!isMeasured)
      return;
    if (targetView == null)
      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.bg_shadow));
    }
    // 绘制屏幕背景
    temp.drawRect(0, 0, temp.getWidth(), temp.getHeight(), bgPaint);

    // targetView 的透明圆形画笔
    if (mCirclePaint == null) {
      mCirclePaint = new Paint();
    }
    //透明效果
    porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);  //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 RECTANGULAR://圆角矩形
          if (isContain) {
            oval.left = location[0] - 8;
            oval.top = center[1] - targetViewHeight / 2 - 8;
            oval.right = location[0]   targetViewWidth   8;
            oval.bottom = center[1]   targetViewHeight / 2   8;
          } else {
            oval.left = location[0]   5;
            oval.top = center[1] - targetViewHeight / 2   1;
            oval.right = location[0]   targetViewWidth - 5;
            oval.bottom = center[1]   targetViewHeight / 2 - 1;
          }
          temp.drawRoundRect(oval, radius, radius, mCirclePaint);
          break;
      }
    } else {
      temp.drawCircle(center[0], center[1], radius, mCirclePaint);
    }

    // 绘制到屏幕
    canvas.drawBitmap(bitmap, 0, 0, bgPaint);
    bitmap.recycle();
  }




  @Override
  public void onGlobalLayout() {
    if (isMeasured)
      return;
    if (targetView.getHeight() > 0 && targetView.getWidth() > 0) {
      isMeasured = true;
      targetViewWidth = targetView.getWidth();
      targetViewHeight = targetView.getHeight();
    }

    // 获取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();
    }

    //文字图片和提示图片
    createView();

  }

  //文字图片和我知道啦图片一起放
  private void createView() {
    Log.v(TAG, "createView");

    //文字提示
    LayoutParams textViewParams;
    textViewParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    textViewParams.setMargins(0, center[1]   radius   10, 0, 0);

    // 我知道提示布局参数
    LayoutParams guideViewParams;
    guideViewParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    guideViewParams.setMargins(0, center[1]   radius   10, 0, 0);

    if (textGuideView != null && customGuideView != null) {
      if (direction != null) {
        int left = center[0]   targetViewWidth / 2;
        int right = center[0]   targetViewWidth / 2;
        int top = center[1] - targetViewHeight / 2;
        int bottom = center[1]   targetViewHeight / 2;

        //我自己的项目只需要这两个方向的, 所以这里就只写了Top和Boottom
        switch (direction) {
          case TOP:
            this.setGravity(Gravity.CENTER_HORIZONTAL);
            textViewParams.setMargins(offsetX, top - offsetY, -offsetX, -top   offsetY);
            guideViewParams.setMargins(offsetX, -3 * offsetY   top, -offsetX, -top   3 * offsetY);
            break;
          case BOTTOM:
            this.setGravity(Gravity.CENTER_HORIZONTAL);
            textViewParams.setMargins(offsetX, bottom   offsetY, -offsetX, -bottom - offsetY);
            guideViewParams.setMargins(offsetX, bottom   3 * offsetY, -offsetX, -bottom - 3 * offsetY);
            break;
        }

        if (this != null)
          this.removeAllViews();
        this.addView(textGuideView, textViewParams);
        this.addView(customGuideView, guideViewParams);
      }
    }
  }


  /**
   * 定义GuideView相对于targetView的方位,
   */
  public enum Direction {
    LEFT, TOP, RIGHT, BOTTOM,
    LEFT_TOP, LEFT_BOTTOM,
    RIGHT_TOP, RIGHT_BOTTOM
  }

  /**
   * 定义目标控件的形状。圆形,矩形
   */
  public enum MyShape {
    CIRCULAR, RECTANGULAR
  }

  /**
   * GuideView点击Callback
   */
  public 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;
    }

    /**
     * 设置目标view
     */
    public Builder setTargetView(View target) {
      guiderView.setTargetView(target);
      return instance;
    }

    /**
     * 设置蒙层颜色
     */
    public Builder setBgColor(int color) {
      guiderView.setBgColor(color);
      return instance;
    }

    /**
     * 设置文字和图片View 在目标view的位置
     */
    public Builder setDirction(Direction dir) {
      guiderView.setDirection(dir);
      return instance;
    }

    /**
     * 设置绘制形状
     */
    public Builder setShape(MyShape shape) {
      guiderView.setShape(shape);
      return instance;
    }

    public Builder setRadius(int radius) {
      guiderView.setRadius(radius);
      return instance;
    }

    /**
     * 设置文字图片
     */
    public Builder setTextGuideView(View view) {
      guiderView.setTextGuideView(view);
      return instance;
    }

    /**
     * 设置"我知道啦"图片
     */
    public Builder setCustomGuideView(View view) {
      guiderView.setCustomGuideView(view);
      return instance;
    }

    /**
     * 设置图片的偏移量
     */
    public Builder setOffset(int x, int y) {
      guiderView.setOffsetX(x);
      guiderView.setOffsetY(y);
      return instance;
    }

    /**
     * 设置时候包含 true:画的透明包含目标view
     */
    public Builder setContain(boolean isContain) {
      guiderView.setContain(isContain);
      return instance;
    }

    /**
     * 点击监听
     */
    public Builder setOnclickListener(final OnClickCallback callback) {
      guiderView.setOnclickListener(callback);
      return instance;
    }

    public GuideView build() {
      guiderView.setClickInfo();
      return guiderView;
    }

  }

  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;
    temp = null;
  }
}

在自己页面应用:

//文字图片
    final ImageView iv1 = new ImageView(context);
    iv1.setImageResource(R.drawable.img_guide_work_text);
    RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    iv1.setLayoutParams(params1);

    //我知道啦
    final ImageView iv2 = new ImageView(context);
    iv2.setImageResource(R.drawable.img_guide_know);
    RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    iv2.setLayoutParams(params2);

 guideView = GuideView.Builder
        .newInstance(context)
        .setTargetView(itemWork) //设置目标view
        .setTextGuideView(iv1)   //设置文字图片
        .setCustomGuideView(iv2)  //设置 我知道啦图片
        .setOffset(0, 80)      //偏移量 x=0 y=80
        .setDirction(GuideView.Direction.BOTTOM)  //方向
        .setShape(GuideView.MyShape.RECTANGULAR)  //矩形
        .setRadius(10)               //圆角
        .setContain(false)             //透明的方块时候包含目标view 默认false
        .setBgColor(getResources().getColor(R.color.bg_shadow))  //背景颜色
        .setOnclickListener(new GuideView.OnClickCallback() {
          @Override
          public void onClickedGuideView() {
            guideView.hide();
          }
        })
        .build();
guideView.show();


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

Android实现新手引导半透明蒙层效果的更多相关文章

  1. html5 canvas合成海报所遇问题及解决方案总结

    这篇文章主要介绍了html5 canvas合成海报所遇问题及解决方案总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Html5 video标签视频的最佳实践

    这篇文章主要介绍了Html5 video标签视频的最佳实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  3. HTML5在微信内置浏览器下右上角菜单的调整字体导致页面显示错乱的问题

    HTML5在微信内置浏览器下,在右上角菜单的调整字体导致页面显示错乱的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

  4. ios – containerURLForSecurityApplicationGroupIdentifier:在iPhone和Watch模拟器上给出不同的结果

    我使用默认的XCode模板创建了一个WatchKit应用程序.我向iOSTarget,WatchkitAppTarget和WatchkitAppExtensionTarget添加了应用程序组权利.(这是应用程序组名称:group.com.lombax.fiveminutes)然后,我尝试使用iOSApp和WatchKitExtension访问共享文件夹URL:延期:iOS应用:但是,测试NSURL

  5. Ionic – Splash Screen适用于iOS,但不适用于Android

    我有一个离子应用程序,其中使用CLI命令离子资源生成的启动画面和图标iOS版本与正在渲染的启动画面完美配合,但在Android版本中,只有在加载应用程序时才会显示白屏.我检查了config.xml文件,所有路径看起来都是正确的,生成的图像出现在相应的文件夹中.(我使用了splash.psd模板来生成它们.我错过了什么?这是config.xml文件供参考,我觉得我在这里做错了–解决方法在config.xml中添加以下键:它对我有用!

  6. ios – 无法启动iPhone模拟器

    /Library/Developer/CoreSimulator/Devices/530A44CB-5978-4926-9E91-E9DBD5BFB105/data/Containers/Bundle/Application/07612A5C-659D-4C04-ACD3-D211D2830E17/ProductName.app/ProductName然后,如果您在Xcode构建设置中选择标准体系结构并再次构建和运行,则会产生以下结果:dyld:lazysymbolbindingFailed:Symbol

  7. Xamarin iOS图像在Grid内部重叠

    heyo,所以在Xamarin我有一个使用并在其中包含一对,所有这些都包含在内.这在Xamarin.Android中看起来完全没问题,但是在Xamarin.iOS中,图像与标签重叠.我不确定它的区别是什么–为什么它在Xamarin.Android中看起来不错但在iOS中它的全部都不稳定?

  8. 在iOS上向后播放HTML5视频

    我试图在iPad上反向播放HTML5视频.HTML5元素包括一个名为playbackRate的属性,它允许以更快或更慢的速率或相反的方式播放视频.根据Apple’sdocumentation,iOS不支持此属性.通过每秒多次设置currentTime属性,可以反复播放,而无需使用playbackRate.这种方法适用于桌面Safari,但似乎在iOS设备上的搜索限制为每秒1次更新–在我的情况下太慢了.有没有办法在iOS设备上向后播放HTML5视频?解决方法iOS6Safari现在支持playbackRat

  9. 使用 Swift 语言编写 Android 应用入门

    Swift标准库可以编译安卓armv7的内核,这使得可以在安卓移动设备上执行Swift语句代码。做梦,虽然Swift编译器可以胜任在安卓设备上编译Swift代码并运行。这需要的不仅仅是用Swift标准库编写一个APP,更多的是你需要一些框架来搭建你的应用用户界面,以上这些Swift标准库不能提供。简单来说,构建在安卓设备上使用的Swiftstdlib需要libiconv和libicu。通过命令行执行以下命令:gitclonegit@github.com:SwiftAndroid/libiconv-libi

  10. Android – 调用GONE然后VISIBLE使视图显示在错误的位置

    我有两个视图,A和B,视图A在视图B上方.当我以编程方式将视图A设置为GONE时,它将消失,并且它正下方的视图将转到视图A的位置.但是,当我再次将相同的视图设置为VISIBLE时,它会在视图B上显示.我不希望这样.我希望视图B回到原来的位置,这是我认为会发生的事情.我怎样才能做到这一点?编辑–代码}这里是XML:解决方法您可以尝试将两个视图放在RelativeLayout中并相对于彼此设置它们的位置.

随机推荐

  1. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Android单选按钮RadioButton的使用详解

    今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  3. 解决android studio 打包发现generate signed apk 消失不见问题

    这篇文章主要介绍了解决android studio 打包发现generate signed apk 消失不见问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

  4. Android 实现自定义圆形listview功能的实例代码

    这篇文章主要介绍了Android 实现自定义圆形listview功能的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 详解Android studio 动态fragment的用法

    这篇文章主要介绍了Android studio 动态fragment的用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android用RecyclerView实现图标拖拽排序以及增删管理

    这篇文章主要介绍了Android用RecyclerView实现图标拖拽排序以及增删管理的方法,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下

  7. Android notifyDataSetChanged() 动态更新ListView案例详解

    这篇文章主要介绍了Android notifyDataSetChanged() 动态更新ListView案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

  8. Android自定义View实现弹幕效果

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

  9. Android自定义View实现跟随手指移动

    这篇文章主要为大家详细介绍了Android自定义View实现跟随手指移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Android实现多点触摸操作

    这篇文章主要介绍了Android实现多点触摸操作,实现图片的放大、缩小和旋转等处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部