Android实现新手引导半透明蒙层效果

这篇文章主要为大家详细介绍了Android实现新手引导半透明蒙层效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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)

以上就是Android实现新手引导半透明蒙层效果的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动