首页 > 其他分享 >android 引导图engine类封装

android 引导图engine类封装

时间:2022-11-24 23:05:34浏览次数:34  
标签:engine currentGuideImage 封装 viewParent new android id view


暂时做的是全局点击区域,如果要做那种热点点击区域也是可以的哈,给我一个填充器就能产生一个view,既然能产生一个view,那么也就能产生一个引导.....



附上我的原创封装代码:

GuidePageEngine.java<pre style="font-family: 宋体; font-size: 12pt; background-color: rgb(255, 255, 255);"><pre name="code" class="java">import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.meimi.youganjue.interf.INotify;

import java.util.ArrayList;

/**
* Created by luozheng on 16/1/14.
*/
public class GuidePageEngine {


private static final String TAG = "GuidePageEngine";
private int nextResouceIndex =0;
private ArrayList<Integer> lists;
private Activity activity;
int currentLayoutId;
private INotify iNotify;
private ImageView currentGuideImage;
private ViewParent viewParent;

/**
*
* @param activity 当前
* @param currentLayoutId 当前布局的顶级id
* @param lists 引导图片集合
* @param iNotify 引导完毕的通知
*/
public void init(final Activity activity,int currentLayoutId,ArrayList<Integer> lists,INotify iNotify){
this.activity =activity;
this.lists =lists;
this.currentLayoutId=currentLayoutId;
this.iNotify =iNotify;
}
public void guide() {
View view = activity.getWindow().getDecorView().findViewById(currentLayoutId);
if(view==null){
new RuntimeException("找不到id对应的view,请给当前布局文件的顶级节点添加id为你传递过来的id");
}
viewParent = view.getParent();
if (viewParent instanceof FrameLayout) {
final FrameLayout frameLayout = (FrameLayout) viewParent;
if (nextResouceIndex <lists.size()) {//设置了引导图片
currentGuideImage = new ImageView(activity);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
currentGuideImage.setLayoutParams(params);
currentGuideImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
currentGuideImage.setImageResource(lists.get(nextResouceIndex));
currentGuideImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextResouceIndex++;
frameLayout.removeView(v);
guide();
}
});
frameLayout.addView(currentGuideImage);//添加引导图片

} else {
//已经完毕了
Log.i(TAG,"引导完毕");
currentGuideImage=null;
iNotify.onNotify(null);
// SPUtils.setValue(getApplicationContext(), ParamConstant.STR_FIRST_RUN, false);//设为已引导
}
}else{
Log.i(TAG,"无法引导! viewParent:"+ viewParent);
}
}

/**
* 当不小心弹出了引导但是要关闭
*/
public void canelGuide(){
if(currentGuideImage!=null && viewParent!=null && currentGuideImage.getParent()!=null && viewParent instanceof ViewGroup){
((ViewGroup) viewParent).removeView(currentGuideImage);
currentGuideImage=null;
}
}
public boolean guideIsShow(){
return currentGuideImage!=null;
}
}






INotify.java 其实就是个回调接口 

package space.qssq.interfaces;

/**
* Created by luozheng on 15/12/2.
*/
public interface INotify<T> {
void onNotify(T param);
}


用法:

第一步:给那个用的activity的顶级加一个id,其实你也可以给一个android 根id,叫啥来着,R.id.content_view???反正都可以的,只要不是线性布局等影响显示就好了.

下面是直接添加图片资源id了。也就是你每给添加一个id,它都会new一个图片...

if(SPUtils.getValue(this, ParamConstant.SP_GUIDE_7_8_ChatActivity,false)){//sp的判断不用说了吧..
<strong> GuidePageEngine guidePageEngine=new GuidePageEngine();</strong>
ArrayList<Integer> listGuide=new ArrayList<>();
listGuide.add(R.drawable.guide_7_friend);
listGuide.add(R.drawable.guide_8_gift);
guidePageEngine.init(ConversationActivity.this, R.id.my_content_view, listGuide, new INotify() {
@Override
public void onNotify(Object o) {
//执行完毕了
SPUtils.setValue(MainActivity.this,ParamConstant.SP_GUIDE_7_8_ChatActivity,true);
}
});
}

能用图片肯定直接给布局文件也是可以的,修改engine为直接读取布局资源,inflate一个view然后add进去就行了一样的道理..不过我们公司的太懒直接给整个屏幕的引导图片.





标签:engine,currentGuideImage,封装,viewParent,new,android,id,view
From: https://blog.51cto.com/u_15458814/5885143

相关文章