Android仿QQ附近的人搜索展示功能

Android仿QQ附近的人搜索展示功能,第1张

概述 1.概述老规矩,先上图原装货(就不录制gif了,大家可以自己在Q群助手开启共享地理位置,返回群聊天页面就看到看到附近的人):

 1.概述

老规矩,先上图



原装货(就不录制gif了,大家可以自己在Q群助手开启共享地理位置,返回群聊天页面就看到看到附近的人):


看起来还是挺像的吧。

通过观察,我们可以获取得到如下关系

1.下面展示列表我们可以使用VIEwPager来实现(当然如果你不觉得麻烦,你也可以用horizontalscrollview来试试)

2.上面的扫描图,肯定是个VIEwGroup(因为里面的小圆点是可以点击的,如果是VIEw的话,对于这些小圆点的位置的判断,以及对小圆点缩放动画的处理都会超级麻烦,难以实现),所以我们肯定需要自定义viewGroup

3.确定好了是自定义viewGroup后,对于里面需要放什么对象呢?没错,就是N个小圆点+一个扫描的大圈圈。

有了上面的分析,我们基本可以确定我们的项目结构如下了:


下面将逐个击破,完成最终效果!

二.展示用的vIEwpager

通过上面的分析,可以确定我们首先需要一个been类 Info

public class Info {private int portraitID;//头像IDprivate String name;//名字private String age;//年龄private boolean sex;//false为男,true为女private float distance;//距离public int getPortraitID() {return portraitID;}public voID setPortraitID(int portraitID) {this.portraitID = portraitID;}public String getAge() {return age;}public voID setAge(String age) {this.age = age;}public float getdistance() {return distance;}public voID setdistance(float distance) {this.distance = distance;}public String getname() {return name;}public voID setname(String name) {this.name = name;}public boolean getSex() {return sex;}public voID setSex(boolean sex) {this.sex = sex;}}

因为我们想要vIEwpager左右滑动的时候,当滑动速度大于一定值,则可以一次滑动两个item,所以我们需要自定义一个获取速度的VIEwPager

public class CustomVIEwPager extends VIEwPager{private long downTime;private float LastX;private float mSpeed;public CustomVIEwPager(Context context) {super(context);}public CustomVIEwPager(Context context,AttributeSet attrs) {super(context,attrs);}@OverrIDepublic boolean dispatchtouchEvent(MotionEvent ev) {float x = ev.getX();switch (ev.getAction()) {case MotionEvent.ACTION_DOWN:downTime = System.currentTimeMillis();LastX = x;break;case MotionEvent.ACTION_MOVE:x = ev.getX();break;case MotionEvent.ACTION_UP://计算得到手指从按下到离开的滑动速度mSpeed = (x - LastX) * 1000 / (System.currentTimeMillis() - downTime);break;}return super.dispatchtouchEvent(ev);}public float getSpeed() {return mSpeed;}public voID setSpeed(float mSpeed) {this.mSpeed = mSpeed;}}

最后就是我们的MainAcitivyt中进行相应的设置

public class MainActivity extends Activity implements VIEwPager.OnPagechangelistener,RadarVIEwGroup.IRadarClickListener {private CustomVIEwPager vIEwPager;private relativeLayout ryContainer;private RadarVIEwGroup radarVIEwGroup;private int[] mimgs = {R.drawable.len,R.drawable.leo,R.drawable.lep,R.drawable.leq,R.drawable.ler,R.drawable.les,R.drawable.mln,R.drawable.mmz,R.drawable.mna,R.drawable.mnj,R.drawable.lep};private String[] mnames = {"ImmortalZ","唐马儒","王尼玛","张全蛋","蛋花","王大锤","叫兽","哆啦A梦"};private int mposition;private FixedSpeedScroller scroller;private SparseArray<Info> mDatas = new SparseArray<>();@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.activity_main);initVIEw();initData();/*** 将VIEwpager所在容器的事件分发交给VIEwPager*/ryContainer.setontouchListener(new VIEw.OntouchListener() {@OverrIDepublic boolean ontouch(VIEw v,MotionEvent event) {return vIEwPager.dispatchtouchEvent(event);}});VIEwpagerAdapter mAdapter = new VIEwpagerAdapter();vIEwPager.setAdapter(mAdapter);//设置缓存数为展示的数目vIEwPager.setoffscreenPagelimit(mimgs.length);vIEwPager.setPagemargin(getResources().getDimensionPixelOffset(R.dimen.vIEwpager_margin));//设置切换动画vIEwPager.setPagetransformer(true,new ZoomOutPagetransformer());vIEwPager.addOnPagechangelistener(this);setVIEwPagerSpeed(250);new Handler().postDelayed(new Runnable() {@OverrIDepublic voID run() {radarVIEwGroup.setDatas(mDatas);}},1500);radarVIEwGroup.setiRadarClickListener(this);}private voID initData() {for (int i = 0; i < mimgs.length; i++) {Info info = new Info();info.setPortraitID(mimgs[i]);info.setAge(((int) Math.random() * 25 + 16) + "岁");info.setname(mnames[(int) (Math.random() * mnames.length)]);info.setSex(i % 3 == 0 ? false : true);info.setdistance(Math.round((Math.random() * 10) * 100) / 100);mDatas.put(i,info);}}private voID initVIEw() {vIEwPager = (CustomVIEwPager) findVIEwByID(R.ID.vp);radarVIEwGroup = (RadarVIEwGroup) findVIEwByID(R.ID.radar);ryContainer = (relativeLayout) findVIEwByID(R.ID.ry_container);}/*** 设置VIEwPager切换速度** @param duration*/private voID setVIEwPagerSpeed(int duration) {try {FIEld fIEld = VIEwPager.class.getDeclaredFIEld("mScroller");fIEld.setAccessible(true);scroller = new FixedSpeedScroller(MainActivity.this,new AccelerateInterpolator());fIEld.set(vIEwPager,scroller);scroller.setmDuration(duration);} catch (NoSuchFIEldException e) {e.printstacktrace();} catch (illegalaccessexception e) {e.printstacktrace();}}@OverrIDepublic voID onPageScrolled(int position,float positionOffset,int positionOffsetPixels) {mposition = position;}@OverrIDepublic voID onPageSelected(int position) {radarVIEwGroup.setCurrentShowItem(position);LogUtil.m("当前位置 " + mposition);LogUtil.m("速度 " + vIEwPager.getSpeed());//当手指左滑速度大于2000时vIEwpager右滑(注意是item+2)if (vIEwPager.getSpeed() < -1800) {vIEwPager.setCurrentItem(mposition + 2);LogUtil.m("位置 " + mposition);vIEwPager.setSpeed(0);} else if (vIEwPager.getSpeed() > 1800 && mposition > 0) {//当手指右滑速度大于2000时vIEwpager左滑(注意item-1即可)vIEwPager.setCurrentItem(mposition - 1);LogUtil.m("位置 " + mposition);vIEwPager.setSpeed(0);}}@OverrIDepublic voID onPageScrollStateChanged(int state) {}@OverrIDepublic voID onRadarItemClick(int position) {vIEwPager.setCurrentItem(position);}class VIEwpagerAdapter extends PagerAdapter {@OverrIDepublic Object instantiateItem(VIEwGroup container,final int position) {final Info info = mDatas.get(position);//设置一大堆演示用的数据,麻里麻烦~~VIEw vIEw = LayoutInflater.from(MainActivity.this).inflate(R.layout.vIEwpager_layout,null);ImageVIEw ivPortrait = (ImageVIEw) vIEw.findVIEwByID(R.ID.iv);ImageVIEw ivSex = (ImageVIEw) vIEw.findVIEwByID(R.ID.iv_sex);TextVIEw tvname = (TextVIEw) vIEw.findVIEwByID(R.ID.tv_name);TextVIEw tvdistance = (TextVIEw) vIEw.findVIEwByID(R.ID.tv_distance);tvname.setText(info.getname());tvdistance.setText(info.getdistance() + "km");ivPortrait.setimageResource(info.getPortraitID());if (info.getSex()) {ivSex.setimageResource(R.drawable.girl);} else {ivSex.setimageResource(R.drawable.boy);}ivPortrait.setonClickListener(new VIEw.OnClickListener() {@OverrIDepublic voID onClick(VIEw v) {Toast.makeText(MainActivity.this,"这是 " + info.getname() + " >.<",Toast.LENGTH_SHORT).show();}});container.addVIEw(vIEw);return vIEw;}@OverrIDepublic int getCount() {return mimgs.length;}@OverrIDepublic boolean isVIEwFromObject(VIEw vIEw,Object object) {return vIEw == object;}@OverrIDepublic voID destroyItem(VIEwGroup container,int position,Object object) {VIEw vIEw = (VIEw) object;container.removeVIEw(vIEw);}}}

在贴出MainAcitivity对应的XML

<relativeLayoutxmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:background="@drawable/lkd"androID:paddingleft="5dp"androID:paddingRight="5dp"><mr_immortalz.com.modelqq.custom.RadarVIEwGroupandroID:ID="@+ID/radar"androID:layout_wIDth="280dp"androID:layout_height="280dp"androID:layout_centerHorizontal="true"androID:layout_margintop="50dp"><mr_immortalz.com.modelqq.custom.RadarVIEwandroID:ID="@ID/ID_scan_circle"androID:layout_wIDth="280dp"androID:layout_height="280dp"/></mr_immortalz.com.modelqq.custom.RadarVIEwGroup><relativeLayoutandroID:ID="@+ID/ry_container"androID:layout_wIDth="match_parent"androID:layout_height="wrap_content"androID:layout_alignParentBottom="true"androID:layout_marginBottom="25dp"androID:clipChildren="false"><mr_immortalz.com.modelqq.custom.CustomVIEwPagerandroID:ID="@+ID/vp"androID:layout_wIDth="130dp"androID:layout_height="160dp"androID:layout_centerInParent="true"androID:layout_marginleft="120dp"androID:layout_marginRight="120dp"androID:clipChildren="false"/></relativeLayout></relativeLayout>

注意如果我们想要让VIEwPager一次显示多个,需要设置其所在 父容器 AndroID:clipChildren=”false”
意思就是不限制子VIEw在其范围内。

细心的你可能会发现MainAcitivity中有

vIEwPager.setPagetransformer(true,new ZoomOutPagetransformer()); 

这个,没错,这个就是用来控制我们的切换动画(我在谷歌官方提供的这个基础上进行了修改,也是很好理解)

public class ZoomOutPagetransformer implements VIEwPager.Pagetransformer {private static final float MIN_SCALE = 0.70f;private static final float MIN_Alpha = 0.5f;public voID transformPage(VIEw vIEw,float position) {int pageWIDth = vIEw.getWIDth();int pageHeight = vIEw.getHeight();if (position < -1) { // [-Infinity,-1)// This page is way off-screen to the left.vIEw.setAlpha(MIN_Alpha);vIEw.setScaleX(MIN_SCALE);vIEw.setScaleY(MIN_SCALE);} else if (position <= 1) { // [-1,1]// Modify the default slIDe Transition to shrink the page as wellfloat scaleFactor = Math.max(MIN_SCALE,1 - Math.abs(position));float vertmargin = pageHeight * (1 - scaleFactor) / 2;float horzmargin = pageWIDth * (1 - scaleFactor) / 2;if (position < 0) {vIEw.setTranslationX(horzmargin - vertmargin / 2);vIEw.setScaleX(1 + 0.3f * position);vIEw.setScaleY(1 + 0.3f * position);} else {vIEw.setTranslationX(-horzmargin + vertmargin / 2);vIEw.setScaleX(1 - 0.3f * position);vIEw.setScaleY(1 - 0.3f * position);}// Scale the page down (between MIN_SCALE and 1)// Fade the page relative to its size.vIEw.setAlpha(MIN_Alpha + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_Alpha));} else { // (1,+Infinity]// This page is way off-screen to the right.vIEw.setScaleX(MIN_SCALE);vIEw.setScaleY(MIN_SCALE);vIEw.setAlpha(MIN_Alpha);}}}

完成了上面这些代码,我们的VIEwPager就算搞定了



三.实现雷达扫描图

代码中也注释得很清楚了,当然因为要扫描,我们需要不停的转动,所以这里我们用到了矩阵变换Matrix,扫描消息的停顿和传递我们用到了Runnable ,如果要是觉得在向主线程一直投递变换的消息对主线程不好,你可以考虑下用SurfaceVIEw来实现

public class RadarVIEw extends VIEw {private Paint mPaintline;//画圆线需要用到的paintprivate Paint mPaintCircle;//画圆需要用到的paintprivate Paint mPaintScan;//画扫描需要用到的paintprivate int mWIDth,mHeight;//整个图形的长度和宽度private Matrix matrix = new Matrix();//旋转需要的矩阵private int scanAngle;//扫描旋转的角度private Shader scanShader;//扫描渲染shaderprivate Bitmap centerBitmap;//最中间icon//每个圆圈所占的比例private static float[] circleProportion = {1 / 13f,2 / 13f,3 / 13f,4 / 13f,5 / 13f,6 / 13f};private int scanSpeed = 5;private int currentScanningCount;//当前扫描的次数private int currentScanningItem;//当前扫描显示的itemprivate int maxScanItemCount;//最大扫描次数private boolean startScan = false;//只有设置了数据后才会开始扫描private IScanningListener iScanningListener;//扫描时监听回调接口public voID setScanningListener(IScanningListener iScanningListener) {this.iScanningListener = iScanningListener;}private Runnable run = new Runnable() {@OverrIDepublic voID run() {scanAngle = (scanAngle + scanSpeed) % 360;matrix.postRotate(scanSpeed,mWIDth / 2,mHeight / 2);invalIDate();postDelayed(run,130);//开始扫描显示标志为true 且 只扫描一圈if (startScan && currentScanningCount <= (360 / scanSpeed)) {if (iScanningListener != null && currentScanningCount % scanSpeed == 0&& currentScanningItem < maxScanItemCount) {iScanningListener.onScanning(currentScanningItem,scanAngle);currentScanningItem++;} else if (iScanningListener != null && currentScanningItem == maxScanItemCount) {iScanningListener.onScanSuccess();}currentScanningCount++;}}};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,defStyleAttr);init();post(run);}private voID init() {mPaintline = new Paint();mPaintline.setcolor(getResources().getcolor(R.color.line_color_blue));mPaintline.setAntiAlias(true);mPaintline.setstrokeWIDth(1);mPaintline.setStyle(Paint.Style.stroke);mPaintCircle = new Paint();mPaintCircle.setcolor(color.WHITE);mPaintCircle.setAntiAlias(true);mPaintScan = new Paint();mPaintScan.setStyle(Paint.Style.FILL_AND_stroke);}@OverrIDeprotected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) {setMeasuredDimension(measureSize(wIDthMeasureSpec),measureSize(wIDthMeasureSpec));mWIDth = getMeasureDWIDth();mHeight = getMeasuredHeight();mWIDth = mHeight = Math.min(mWIDth,mHeight);centerBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.circle_photo);//设置扫描渲染的shaderscanShader = new SweepGradIEnt(mWIDth / 2,mHeight / 2,new int[]{color.transparent,color.parsecolor("#84B5CA")},null);}private int measureSize(int measureSpec) {int result = 0;int specMode = MeasureSpec.getMode(measureSpec);int specsize = MeasureSpec.getSize(measureSpec);if (specMode == MeasureSpec.EXACTLY) {result = specsize;} else {result = 300;if (specMode == MeasureSpec.AT_MOST) {result = Math.min(result,specsize);}}return result;}@OverrIDeprotected voID onDraw(Canvas canvas) {drawCircle(canvas);drawScan(canvas);drawCenterIcon(canvas);}/*** 绘制圆线圈** @param canvas*/private voID drawCircle(Canvas canvas) {canvas.drawCircle(mWIDth / 2,mWIDth * circleProportion[1],mPaintline); // 绘制小圆canvas.drawCircle(mWIDth / 2,mWIDth * circleProportion[2],mPaintline); // 绘制中圆canvas.drawCircle(mWIDth / 2,mWIDth * circleProportion[3],mPaintline); // 绘制中大圆canvas.drawCircle(mWIDth / 2,mWIDth * circleProportion[4],mPaintline); // 绘制大圆canvas.drawCircle(mWIDth / 2,mWIDth * circleProportion[5],mPaintline); // 绘制大大圆}/*** 绘制扫描** @param canvas*/private voID drawScan(Canvas canvas) {canvas.save();mPaintScan.setShader(scanShader);canvas.concat(matrix);canvas.drawCircle(mWIDth / 2,mPaintScan);canvas.restore();}/*** 绘制最中间的图标** @param canvas*/private voID drawCenterIcon(Canvas canvas) {canvas.drawBitmap(centerBitmap,null,new Rect((int) (mWIDth / 2 - mWIDth * circleProportion[0]),(int) (mHeight / 2 - mWIDth * circleProportion[0]),(int) (mWIDth / 2 + mWIDth * circleProportion[0]),(int) (mHeight / 2 + mWIDth * circleProportion[0])),mPaintCircle);}public interface IScanningListener {//正在扫描(此时还没有扫描完毕)时回调voID onScanning(int position,float scanAngle);//扫描成功时回调voID onScanSuccess();}public voID setMaxScanItemCount(int maxScanItemCount) {this.maxScanItemCount = maxScanItemCount;}/*** 开始扫描*/public voID startScan() {this.startScan = true;}}

四.完成小圆点的放置

现在我们的扫描图有了,对比发现我们还差啥?没错,就是小圆点

public class CircleVIEw extends VIEw {private Paint mPaint;private Bitmap mBitmap;private float radius = displayUtils.dp2px(getContext(),9);//半径private float disX;//位置Xprivate float disY;//位置Yprivate float angle;//旋转的角度private float proportion;//根据远近距离的不同计算得到的应该占的半径比例public float getProportion() {return proportion;}public voID setProportion(float proportion) {this.proportion = proportion;}public float getAngle() {return angle;}public voID setAngle(float angle) {this.angle = angle;}public float getdisX() {return disX;}public voID setdisX(float disX) {this.disX = disX;}public float getdisY() {return disY;}public voID setdisY(float disY) {this.disY = disY;}public CircleVIEw(Context context) {this(context,null);}public CircleVIEw(Context context,0);}public CircleVIEw(Context context,defStyleAttr);init();}private voID init() {mPaint = new Paint();mPaint.setcolor(getResources().getcolor(R.color.bg_color_pink));mPaint.setAntiAlias(true);}@OverrIDeprotected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) {super.onMeasure(wIDthMeasureSpec,heightmeasureSpec);setMeasuredDimension(measureSize(wIDthMeasureSpec),measureSize(heightmeasureSpec));}private int measureSize(int measureSpec) {int result = 0;int specMode = MeasureSpec.getMode(measureSpec);int specsize = MeasureSpec.getSize(measureSpec);if (specMode == MeasureSpec.EXACTLY) {result = specsize;} else {result = displayUtils.dp2px(getContext(),18);if (specMode == MeasureSpec.AT_MOST) {result = Math.min(result,specsize);}}return result;}@OverrIDeprotected voID onDraw(Canvas canvas) {canvas.drawCircle(radius,radius,mPaint);if (mBitmap != null) {canvas.drawBitmap(mBitmap,new Rect(0,2 * (int) radius,2 * (int) radius),mPaint);}}public voID setPaintcolor(int resID) {mPaint.setcolor(resID);invalIDate();}public voID setPortraitIcon(int resID) {mBitmap = BitmapFactory.decodeResource(getResources(),resID);invalIDate();}public voID clearPortaitIcon(){mBitmap = null;invalIDate();}}

有了小圆点,我们最后只需要把扫描图和小圆点放在一起就好了 因为我们是想变扫描变出现小圆点,所以我们需要在RadarVIEw中定义一个接口IScanningListener,告诉RadarVIEwGroup我正在扫描,你快让小圆点出现吧

所以在RadarVIEwGroup的onScanning中需要调用requestLayout();

public class RadarVIEwGroup extends VIEwGroup implements RadarVIEw.IScanningListener {private int mWIDth,mHeight;//vIEwgroup的宽高private SparseArray<float> scanAngleList = new SparseArray<>();//记录展示的item所在的扫描位置角度private SparseArray<Info> mDatas;//数据源private int dataLength;//数据源长度private int minItemposition;//最小距离的item所在数据源中的位置private CircleVIEw currentShowChild;//当前展示的itemprivate CircleVIEw minShowChild;//最小距离的itemprivate IRadarClickListener iRadarClickListener;//雷达图中点击监听CircleVIEw小圆点回调接口public voID setiRadarClickListener(IRadarClickListener iRadarClickListener) {this.iRadarClickListener = iRadarClickListener;}public RadarVIEwGroup(Context context) {this(context,null);}public RadarVIEwGroup(Context context,0);}public RadarVIEwGroup(Context context,defStyleAttr);}@OverrIDeprotected voID onMeasure(int wIDthMeasureSpec,measureSize(heightmeasureSpec));mWIDth = getMeasureDWIDth();mHeight = getMeasuredHeight();mWIDth = mHeight = Math.min(mWIDth,mHeight);//测量每个childrenmeasureChildren(wIDthMeasureSpec,heightmeasureSpec);for (int i = 0; i < getChildCount(); i++) {VIEw child = getChildAt(i);if (child.getID() == R.ID.ID_scan_circle) {//为雷达扫描图设置需要的属性((RadarVIEw) child).setScanningListener(this);//考虑到数据没有添加前扫描图在扫描,但是不会开始为CircleVIEw布局if (mDatas != null && mDatas.size() > 0) {((RadarVIEw) child).setMaxScanItemCount(mDatas.size());((RadarVIEw) child).startScan();}continue;}}}@OverrIDeprotected 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,vIEw.getMeasureDWIDth(),vIEw.getMeasuredHeight());}//放置雷达图中需要展示的item圆点for (int i = 0; i < childCount; i++) {final int j = i;final VIEw child = getChildAt(i);if (child.getID() == R.ID.ID_scan_circle) {//如果不是CirclevIEw跳过continue;}//设置CircleVIEw小圆点的坐标信息//坐标 = 旋转角度 * 半径 * 根据远近距离的不同计算得到的应该占的半径比例((CircleVIEw) child).setdisX((float) Math.cos(Math.toradians(scanAngleList.get(i - 1) - 5))* ((CircleVIEw) child).getProportion() * mWIDth / 2);((CircleVIEw) child).setdisY((float) Math.sin(Math.toradians(scanAngleList.get(i - 1) - 5))* ((CircleVIEw) child).getProportion() * mWIDth / 2);//如果扫描角度记录SparseArray中的对应的item的值为0,// 说明还没有扫描到该item,跳过对该item的layout//(scanAngleList设置数据时全部设置的value=0,// 当onScanning时,value设置的值始终不会0,具体可以看onScanning中的实现)if (scanAngleList.get(i - 1) == 0) {continue;}//放置Circle小圆点child.layout((int) ((CircleVIEw) child).getdisX() + mWIDth / 2,(int) ((CircleVIEw) child).getdisY() + mHeight / 2,(int) ((CircleVIEw) child).getdisX() + child.getMeasureDWIDth() + mWIDth / 2,(int) ((CircleVIEw) child).getdisY() + child.getMeasuredHeight() + mHeight / 2);//设置点击事件child.setonClickListener(new OnClickListener() {@OverrIDepublic voID onClick(VIEw v) {resetAnim(currentShowChild);currentShowChild = (CircleVIEw) child;//因为雷达图是childAt(0),所以这里需要作-1才是正确的CirclestartAnim(currentShowChild,j - 1);if (iRadarClickListener != null) {iRadarClickListener.onRadarItemClick(j - 1);}}});}}private int measureSize(int measureSpec) {int result = 0;int specMode = MeasureSpec.getMode(measureSpec);int specsize = MeasureSpec.getSize(measureSpec);if (specMode == MeasureSpec.EXACTLY) {result = specsize;} else {result = 300;if (specMode == MeasureSpec.AT_MOST) {result = Math.min(result,specsize);}}return result;}/*** 设置数据** @param mDatas*/public voID setDatas(SparseArray<Info> mDatas) {this.mDatas = mDatas;dataLength = mDatas.size();float min = float.MAX_VALUE;float max = float.MIN_VALUE;//找到距离的最大值,最小值对应的minItempositionfor (int j = 0; j < dataLength; j++) {Info item = mDatas.get(j);if (item.getdistance() < min) {min = item.getdistance();minItemposition = j;}if (item.getdistance() > max) {max = item.getdistance();}scanAngleList.put(j,0f);}//根据数据源信息动态添加CircleVIEwfor (int i = 0; i < dataLength; i++) {CircleVIEw circleVIEw = new CircleVIEw(getContext());if (mDatas.get(i).getSex()) {circleVIEw.setPaintcolor(getResources().getcolor(R.color.bg_color_pink));} else {circleVIEw.setPaintcolor(getResources().getcolor(R.color.bg_color_blue));}//根据远近距离的不同计算得到的应该占的半径比例 0.312-0.832circleVIEw.setProportion((mDatas.get(i).getdistance() / max + 0.6f) * 0.52f);if (minItemposition == i) {minShowChild = circleVIEw;}addVIEw(circleVIEw);}}/*** 雷达图没有扫描完毕时回调** @param position* @param scanAngle*/@OverrIDepublic voID onScanning(int position,float scanAngle) {if (scanAngle == 0) {scanAngleList.put(position,1f);} else {scanAngleList.put(position,scanAngle);}requestLayout();}/*** 雷达图扫描完毕时回调*/@OverrIDepublic voID onScanSuccess() {LogUtil.m("完成回调");resetAnim(currentShowChild);currentShowChild = minShowChild;startAnim(currentShowChild,minItemposition);}/*** 恢复CircleVIEw小圆点原大小** @param object*/private voID resetAnim(CircleVIEw object) {if (object != null) {object.clearPortaitIcon();ObjectAnimator.offloat(object,"scaleX",1f).setDuration(300).start();ObjectAnimator.offloat(object,"scaleY",1f).setDuration(300).start();}}/*** 放大CircleVIEw小圆点大小** @param object* @param position*/private voID startAnim(CircleVIEw object,int position) {if (object != null) {object.setPortraitIcon(mDatas.get(position).getPortraitID());ObjectAnimator.offloat(object,2f).setDuration(300).start();ObjectAnimator.offloat(object,2f).setDuration(300).start();}}/*** 雷达图中点击监听CircleVIEw小圆点回调接口*/public interface IRadarClickListener {voID onRadarItemClick(int position);}/*** 根据position,放大指定的CircleVIEw小圆点** @param position*/public voID setCurrentShowItem(int position) {CircleVIEw child = (CircleVIEw) getChildAt(position + 1);resetAnim(currentShowChild);currentShowChild = child;startAnim(currentShowChild,position);}}

每次点击雷达图中的小圆点都会告诉VIEwPager切换到指定的页面,所以RadarVIEwGroup中需要定义一个IRadarClickListener,让VIEwPager所在的MainAcitivity去实现该接口
完成的效果就是这样了


五.总结

我们最终实现的效果还是挺棒的,通过实战,我们对于自定义view,自定义viewGroup更加熟练啦~

总结

以上是内存溢出为你收集整理的Android仿QQ附近的人搜索展示功能全部内容,希望文章能够帮你解决Android仿QQ附近的人搜索展示功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149328.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存