最近项目需求是实现高亮功能引导页的效果,查了很多资料Android确实没有类似iOS的抠图的现成控件,并且看了一些github上的效果没有扩展性也不是很强。所以将重点转移到代替效果上来。

这是一个非常简单易行的方法,扩展性很强可以将任何View 作为目标view,其功能演变的出发点是activity截图。

思路:1.将需高亮View A转换成bitmap。

   2.获取A的坐标以及大小。

   3.将A的坐标以及大小设置在BView上。



暂时引导功能页面代码:

<?xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:fitsSystemWindows="true"    tools:context="com.guide1.MainActivity">            android:layout_width="match_parent"        android:layout_height="wrap_content"        android:theme="@style/AppTheme.AppBarOverlay">                    android:id="@+id/toolbar"            android:layout_width="match_parent"            android:layout_height="?attr/actionBarSize"            android:background="?attr/colorPrimary"            app:popupTheme="@style/AppTheme.PopupOverlay" />        layout="@layout/content_main" />            android:id="@+id/fab"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="bottom|end"        android:layout_margin="@dimen/fab_margin"        android:src="@android:drawable/ic_dialog_email" />

activity代码:


public class MainActivity extends Activity implements View.OnClickListener{    private TextView tv_guide;    private TextView tv_btn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv_guide = (TextView) findViewById(R.id.tv_guide);        tv_btn = (TextView) findViewById(R.id.tv_btn);        tv_btn.setOnClickListener( this );    }    @Override    public void onClick(View v) {        GuideDalog.showGuideDialog( this, tv_guide);    }}

更容易理解我们这里使用dialog展示提示功能:

public class GuideDalog {    public static Dialog DetemainDialog;    public static void showGuideDialog(Activity activity, View view ) {        if (activity != null && !activity.isFinishing() ) {            if (DetemainDialog == null) {                DetemainDialog = new Dialog(activity, R.style.common_dialog);                View v = LayoutInflater.from(activity).inflate(R.layout.dialog_guidedalog, null);                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);                DetemainDialog.addContentView(v, params);                Window window = DetemainDialog.getWindow();                WindowManager.LayoutParams lp = window.getAttributes();                lp.dimAmount = 0.6f;                window.setAttributes(lp);                window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);                RelativeLayout rl_dialog = (RelativeLayout) v.findViewById(R.id.rl_dialog);                final ImageView tv_plan_name = (ImageView) v.findViewById(R.id.tv_plan_name);                ImageView tv_plan_remind = (ImageView) v.findViewById(R.id.tv_plan_remind);                tv_plan_remind.setImageResource(R.mipmap.ic_plan_name);                tv_plan_name.setImageBitmap(ViewUtil.getViewBitmap(view));                int[] location = ViewUtil.getViewLocation(view);                int height = view.getHeight();                int width= view.getWidth();                ViewUtil.setViewSize(tv_plan_name, height,width);                ViewUtil.setViewLocation(tv_plan_name, location[0], 0,location[1]- ViewUtil.dip2px(activity, 20), 0);            }            if (DetemainDialog != null && activity != null && !activity.isFinishing() && !DetemainDialog.isShowing()) {                DetemainDialog.show();            }        }    }    public static void dismissDialog() {        if (DetemainDialog != null) {            DetemainDialog.dismiss();            DetemainDialog = null;        }    }}

dialog_guidedialog.xml

xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/rl_dialog"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#96000000">            android:id="@+id/tv_plan_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />            android:id="@+id/tv_plan_remind"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dip"        android:layout_below="@+id/tv_plan_name"        android:layout_centerHorizontal="true"/>

bg_5dip

<?xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" >    android:color="@android:color/white" />            android:dashGap="0dp"        android:dashWidth="15dp"        android:width="0.4dp"        android:color="@android:color/white" />    android:radius="5dip"/>

viewUtils

public class ViewUtil {    /**     * 获取控件的大小     * @param view     * @return     */    public static int[] getViewLocation(View view){        int[] position = new int[2];//        view.getLocationOnScreen(position);//相对屏幕        view.getLocationInWindow(position);//相对activity        return position;    }    /**     * 获取控件的高     * @param view     * @return     */    public static int getViewHeight(View view){        if( view == null ) return 0;        int height = view.getHeight();        return height;    }    /**     * 获取控件的款     * @param view     * @return     */    public static int getVieWidth(View view){        if( view == null ) return 0;        int width = view.getWidth();        return  width;    }    public static void setViewLocation(View view, int left, int right, int top, int bottom ){        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)view.getLayoutParams();        params.setMargins(left, top, right, bottom);// 通过自定义坐标来放置你的控件        view .setLayoutParams(params);    }    public static void setViewSize(View view, int height, int width ){        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)view.getLayoutParams();        params.height = height;        params.width = width;        view .setLayoutParams(params);    }    public static void setViewHeight(View view, int height){        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)view.getLayoutParams();        params.height = height;        view .setLayoutParams(params);    }    /**     * * 将View 转换成bitmap    * @param view    * @return            */    public static android.graphics.Bitmap getViewBitmap(View view) {        android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap(view.getWidth(), view.getHeight(), android.graphics.Bitmap.Config.ARGB_8888);        Canvas canvas = new Canvas(bitmap);        view.draw(canvas);        return bitmap;    }    /**     * 根据dip转换成像素     *     * @param context     * @param dipValue     * @return     */    public static int dip2px(Context context, float dipValue) {        final float scale = context.getResources().getDisplayMetrics().density;        return (int) (dipValue * scale + 0.5f);    }

未启动

已启动

下载地址:http://download.csdn.net/detail/haileijingshi/9679387

更多相关文章

  1. android 各种常用,不易记的dialog,对话框,等控件整理
  2. Android(安卓)获取Root权限之后的静默安装实现 代码示例分析
  3. Android(安卓)MediaStore仿微信朋友圈获取图片及视频
  4. 在Ubuntu下获取Android4.0源代码并编译
  5. [开源]在iOS上实现Android风格的控件Toast
  6. Android获取父类容器中控件的方法
  7. Android(安卓)USB串口开发
  8. Android(安卓)Studio 中高德地图申请key和获取sha1及配置的几点
  9. Android异步加载全解析之大图处理

随机推荐

  1. Android(安卓)根据屏幕大小设置字体
  2. 【Android外文翻译 - 02】判断是否可以使
  3. This Android(安卓)SDK requires Android
  4. Android studio中音频播放与进度条联动
  5. Android动态创建ListView视图,动态增加和
  6. 3DGallery
  7. Android 经历过的坑
  8. android 重启自身app
  9. android之PendingIntent使用
  10. Android写文件到SDCard的简单代码