1.概述
最近一直到在带实习生,因为人比较多,所以很长一段时间没有更新博客了,今天更新一篇雷达扫描附近好友效果,以后尽量每周更新一篇,先看一下效果:
2.实现
1、效果分析
效果分为两个部分,一个是上半部分的自定义RadarView,还有就是下半部分的ViewPager,至于怎么做到缩放和背景虚化的效果大家可以去看看LazyViewPager这里不详细介绍,这里主要实现扫描效果部分。
2、扫描效果实现
2.1自定义RadarView在onDraw()方法中画六个圆圈,至于圆圈的半径是多少我们需要通过onMeasure(int widthMeasureSpec, int heightMeasureSpec)测量方法获取控件的宽高来确定圆的半径,每个圆的半径是宽度的1 / 13f, 2 / 13f, 3 / 13f, 4 / 13f, 5 / 13f, 6 / 13f,这只是自己测试出来感觉比较舒适的效果,下面请看代码:
//每个圆圈所占的比例 private static float[] circleProportion = {1 / 13f, 2 / 13f, 3 / 13f, 4 / 13f, 5 / 13f, 6 / 13f}; private Paint mPaintCircle;//画圆需要用到的paint public class RadarView extends View { public RadarView(Context context) { this(context, null); } public RadarView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RadarView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaintCircle = new Paint(); mPaintCircle.setColor(Color.WHITE); mPaintCircle.setAntiAlias(true); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 获取控件的宽高 setMeasuredDimension(measureSize(widthMeasureSpec), measureSize(widthMeasureSpec)); mWidth = getMeasuredWidth(); mHeight = getMeasuredHeight(); mWidth = mHeight = Math.min(mWidth, mHeight); } @Override protected void onDraw(Canvas canvas) { // 绘制六个白色圆圈 drawCircle(canvas); } /** * 绘制圆线圈 * * @param canvas */ private void drawCircle(Canvas canvas) { canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth * circleProportion[1], mPaintLine); // 绘制最小圆 canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth * circleProportion[1], mPaintLine); // 绘制小圆 canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth * circleProportion[2], mPaintLine); // 绘制中圆 canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth * circleProportion[3], mPaintLine); // 绘制中大圆 canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth * circleProportion[4], mPaintLine); // 绘制大圆 canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth * circleProportion[5], mPaintLine); // 绘制大大圆 } }
2.2下面需要去画中间的用户图像,可以运行看看中间的六个圆圈有没有达到效果,这里就不看了直接在onDraw()方法中画中间图像:
private Bitmap centerBitmap;//最中间icon private void init(){ // 通过bitmap工厂区获取用户图像的bitmap centerBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.circle_photo); } @Override protected void onDraw(Canvas canvas) { drawCenterIcon(canvas); } /** * 绘制最中间的图标 * * @param canvas */ private void drawCenterIcon(Canvas canvas) { int icOnWidth= mWidth * circleProportion[0]; canvas.drawBitmap(centerBitmap, 0,0,iconWidth ,iconWidth , null); }
2.3最后只需要实现这个扫描的效果这个控件基本就完成了,第一需要开启线程不断调用invalidate()去更新onDraw()方法,第二需要熟悉扫描渲染SweepGradient这个类,如果这两个都没问题那么大功告成:
private Paint mPaintScan;//画扫描需要用到的paint private Matrix matrix = new Matrix();//旋转需要的矩阵 private int mRoteDegree;//扫描旋转的角度 private Shader scanShader;//扫描渲染shader public Runnable run = new Runnable() { @Override public void run() { mRoteDegree +=2; mRoteMatrix.postRotate(mRoteDegree,cx,cy); invalidate(); postDelayed(run,60); } }; @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //设置扫描渲染的shader scanShader = new SweepGradient(mWidth / 2, mHeight / 2, new int[]{Color.TRANSPARENT, Color.parseColor("#84B5CA")}, null); } @Override protected void onDraw(Canvas canvas) { drawScan(canvas); } /** * 绘制扫描 * * @param canvas */ private void drawScan(Canvas canvas) { canvas.save(); mPaintScan.setShader(scanShader); canvas.concat(matrix); canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth * circleProportion[4], mPaintScan); canvas.restore(); }
2.4.到这里我们来看一下扫描RadarView的效果
3. 实现添加数据效果RadarViewGroup,我们的图像附近点需要加入ViewGroup这里又需要自定义了,这里简单说一下自定ViewGroup:
1).onMeasure()测量方法这里就不说了
2).只要搞清楚onLayout()方法是干嘛的就Ok,ViewGroup里面的子View都显示在什么位置就是写在这个方法里面的,换句话说有的隔得近有的隔得远都是由 child.layout(int l, int t, int r, int b)决定的,下面我们看一下代码:
public class RadarViewGroup extends ViewGroup implements RadarView.IScanningListener { private int mWidth, mHeight;//viewgroup的宽高 private SparseArrayscanAngleList = new SparseArray<>();//记录展示的item所在的扫描位置角度 private SparseArray mDatas;//数据源 private int dataLength;//数据源长度 private int minItemPosition;//最小距离的item所在数据源中的位置 private CircleView currentShowChild;//当前展示的item private CircleView minShowChild;//最小距离的item private IRadarClickListener iRadarClickListener;//雷达图中点击监听CircleView小圆点回调接口 public void setiRadarClickListener(IRadarClickListener iRadarClickListener) { this.iRadarClickListener = iRadarClickListener; } public RadarViewGroup(Context context) { this(context, null); } public RadarViewGroup(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RadarViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(measureSize(widthMeasureSpec), measureSize(heightMeasureSpec)); mWidth = getMeasuredWidth(); mHeight = getMeasuredHeight(); mWidth = mHeight = Math.min(mWidth, mHeight); //测量每个children measureChildren(widthMeasureSpec, heightMeasureSpec); for (int i = 0; i 0) { ((RadarView) child).setMaxScanItemCount(mDatas.size()); ((RadarView) child).startScan(); } continue; } } } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int childCount = getChildCount(); //首先放置雷达扫描图 View view = findViewById(R.id.id_scan_circle); if (view != null) { view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); } //放置雷达图中需要展示的item圆点 for (int i = 0; i mDatas) { this.mDatas = mDatas; dataLength = mDatas.size(); float min = Float.MAX_VALUE; float max = Float.MIN_VALUE; //找到距离的最大值,最小值对应的minItemPosition for (int j = 0; j max) { max = item.getDistance(); } scanAngleList.put(j, 0f); } //根据数据源信息动态添加CircleView for (int i = 0; i
源码下载:http://xiazai.jb51.net/201611/yuanma/AndroidRadarScan(jb51.net).rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。