Android雷达扫描动态界面制作
程序员文章站
2024-03-03 23:23:10
先看看效果图:
源码:
package com.zihao.radar;
import android.app.activity;
imp...
先看看效果图:
源码:
package com.zihao.radar; import android.app.activity; import android.os.bundle; import android.view.window; import android.view.windowmanager; import com.zihao.radar.view.radarview; public class mainactivity extends activity { private radarview mradarview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); // 取消状态栏 getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.activity_main); mradarview = (radarview) findviewbyid(r.id.radar_view); mradarview.setsearching(true); mradarview.addpoint(); mradarview.addpoint(); } }
package com.zihao.radar.view; import java.util.arraylist; import java.util.list; import java.util.random; import android.annotation.suppresslint; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.paint; import android.graphics.paint.style; import android.util.attributeset; import android.view.view; import com.zihao.radar.r; /** * @classname:radarview * @description:todo<雷达扫描视图> * @author:zihao * @date:2015年11月11日 上午12:26:11 * @version:v1.1 */ @suppresslint("drawallocation") public class radarview extends view { private context mcontext; private boolean issearching = false;// 标识是否处于扫描状态,默认为不在扫描状态 private paint mpaint;// 画笔 private bitmap mscanbmp;// 执行扫描运动的图片 private int moffsetargs = 0;// 扫描运动偏移量参数 private bitmap mdefaultpointbmp;// 标识设备的圆点-默认 private bitmap mlightpointbmp;// 标识设备的圆点-高亮 private int mpointcount = 0;// 圆点总数 private list<string> mpointarray = new arraylist<string>();// 存放偏移量的map private random mrandom = new random(); private int mwidth, mheight;// 宽高 int moutwidth;// 外圆宽度(w/4/5*2=w/10) int mcx, mcy;// x、y轴中心点 int moutsideradius, minsideradius;// 外、内圆半径 public radarview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); // todo auto-generated constructor stub init(context); } public radarview(context context, attributeset attrs) { super(context, attrs); // todo auto-generated constructor stub init(context); } public radarview(context context) { super(context); // todo auto-generated constructor stub init(context); } /** * todo<提前初始化好需要使用的对象,避免在绘制过程中多次初始化> * * @return void */ private void init(context context) { mpaint = new paint(); this.mcontext = context; this.mdefaultpointbmp = bitmap.createbitmap(bitmapfactory .decoderesource(mcontext.getresources(), r.drawable.radar_default_point_ico)); this.mlightpointbmp = bitmap.createbitmap(bitmapfactory.decoderesource( mcontext.getresources(), r.drawable.radar_light_point_ico)); } /** * 测量视图及其内容,以确定所测量的宽度和高度(测量获取控件尺寸). */ @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { // todo auto-generated method stub super.onmeasure(widthmeasurespec, heightmeasurespec); // 获取控件区域宽高 if (mwidth == 0 || mheight == 0) { final int minimumwidth = getsuggestedminimumwidth(); final int minimumheight = getsuggestedminimumheight(); mwidth = resolvemeasured(widthmeasurespec, minimumwidth); mheight = resolvemeasured(heightmeasurespec, minimumheight); mscanbmp = bitmap.createscaledbitmap(bitmapfactory.decoderesource( mcontext.getresources(), r.drawable.radar_scan_img), mwidth - moutwidth, mwidth - moutwidth, false); // 获取x/y轴中心点 mcx = mwidth / 2; mcy = mheight / 2; // 获取外圆宽度 moutwidth = mwidth / 10; // 计算内、外半径 moutsideradius = mwidth / 2;// 外圆的半径 minsideradius = (mwidth - moutwidth) / 4 / 2;// 内圆的半径,除最外层,其它圆的半径=层数*insideradius } } /** * 绘制视图--从外部向内部绘制 */ @override protected void ondraw(canvas canvas) { // todo auto-generated method stub super.ondraw(canvas); // 开始绘制最外层的圆 mpaint.setantialias(true);// 设置抗锯齿 mpaint.setstyle(style.fill);// 设置填充样式 mpaint.setcolor(0xffb8dcfc);// 设置画笔颜色 // 1.开始绘制圆形 canvas.drawcircle(mcx, mcy, moutsideradius, mpaint); // 开始绘制内4圆 mpaint.setcolor(0xff3278b4); canvas.drawcircle(mcx, mcy, minsideradius * 4, mpaint); // 开始绘制内3圆 mpaint.setstyle(style.stroke); mpaint.setcolor(0xff31c9f2); canvas.drawcircle(mcx, mcy, minsideradius * 3, mpaint); // 开始绘制内2圆 canvas.drawcircle(mcx, mcy, minsideradius * 2, mpaint); // 开始绘制内1圆 canvas.drawcircle(mcx, mcy, minsideradius * 1, mpaint); // 2.开始绘制对角线 canvas.drawline(moutwidth / 2, mcy, mwidth - moutwidth / 2, mcy, mpaint);// 绘制0°~180°对角线 canvas.drawline(mcx, mheight - moutwidth / 2, mcx, moutwidth / 2, mpaint);// 绘制90°~270°对角线 // 根据角度绘制对角线 int startx, starty, endx, endy; double radian; // 绘制45°~225°对角线 // 计算开始位置x/y坐标点 radian = math.toradians((double) 45);// 将角度转换为弧度 startx = (int) (mcx + minsideradius * 4 * math.cos(radian));// 通过圆心坐标、半径和当前角度计算当前圆周的某点横坐标 starty = (int) (mcy + minsideradius * 4 * math.sin(radian));// 通过圆心坐标、半径和当前角度计算当前圆周的某点纵坐标 // 计算结束位置x/y坐标点 radian = math.toradians((double) 45 + 180); endx = (int) (mcx + minsideradius * 4 * math.cos(radian)); endy = (int) (mcy + minsideradius * 4 * math.sin(radian)); canvas.drawline(startx, starty, endx, endy, mpaint); // 绘制135°~315°对角线 // 计算开始位置x/y坐标点 radian = math.toradians((double) 135); startx = (int) (mcx + minsideradius * 4 * math.cos(radian)); starty = (int) (mcy + minsideradius * 4 * math.sin(radian)); // 计算结束位置x/y坐标点 radian = math.toradians((double) 135 + 180); endx = (int) (mcx + minsideradius * 4 * math.cos(radian)); endy = (int) (mcy + minsideradius * 4 * math.sin(radian)); canvas.drawline(startx, starty, endx, endy, mpaint); // 3.绘制扫描扇形图 canvas.save();// 用来保存canvas的状态.save之后,可以调用canvas的平移、放缩、旋转、错切、裁剪等操作. if (issearching) {// 判断是否处于扫描 canvas.rotate(moffsetargs, mcx, mcy);// 绘制旋转角度,参数一:角度;参数二:x中心;参数三:y中心. canvas.drawbitmap(mscanbmp, mcx - mscanbmp.getwidth() / 2, mcy - mscanbmp.getheight() / 2, null);// 绘制bitmap扫描图片效果 moffsetargs += 3; } else { canvas.drawbitmap(mscanbmp, mcx - mscanbmp.getwidth() / 2, mcy - mscanbmp.getheight() / 2, null); } // 4.开始绘制动态点 canvas.restore();// 用来恢复canvas之前保存的状态.防止save后对canvas执行的操作对后续的绘制有影响. if (mpointcount > 0) {// 当圆点总数>0时,进入下一层判断 if (mpointcount > mpointarray.size()) {// 当圆点总数大于存储坐标点数目时,说明有增加,需要重新生成随机坐标点 int mx = minsideradius + mrandom.nextint(minsideradius * 6); int my = minsideradius + mrandom.nextint(minsideradius * 6); mpointarray.add(mx + "/" + my); } // 开始绘制坐标点 for (int i = 0; i < mpointarray.size(); i++) { string[] result = mpointarray.get(i).split("/"); // 开始绘制动态点 if (i < mpointarray.size() - 1) canvas.drawbitmap(mdefaultpointbmp, integer.parseint(result[0]), integer.parseint(result[1]), null); else canvas.drawbitmap(mlightpointbmp, integer.parseint(result[0]), integer.parseint(result[1]), null); } } if (issearching) this.invalidate(); } /** * todo<设置扫描状态> * * @return void */ public void setsearching(boolean status) { this.issearching = status; this.invalidate(); } /** * todo<新增动态点> * * @return void */ public void addpoint() { mpointcount++; this.invalidate(); } /** * todo<解析获取控件宽高> * * @return int */ private int resolvemeasured(int measurespec, int desired) { int result = 0; int specsize = measurespec.getsize(measurespec); switch (measurespec.getmode(measurespec)) { case measurespec.unspecified: result = desired; break; case measurespec.at_most: result = math.min(specsize, desired); break; case measurespec.exactly: default: result = specsize; } return result; } }
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bgp" tools:context="com.zihao.radar.mainactivity" > <com.zihao.radar.view.radarview android:id="@+id/radar_view" android:layout_width="600px" android:layout_height="600px" android:layout_centerinparent="true" /> </relativelayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Android仿微信、QQ附近好友雷达扫描效果
-
Android自定义View制作仪表盘界面
-
Android ViewPager制作新手导航页(动态加载)
-
Android仿微信、QQ附近好友雷达扫描效果
-
Android开发框架之自定义ZXing二维码扫描界面并解决取景框拉伸问题
-
Android自定义View制作仪表盘界面
-
PagerSlidingTabStrip制作Android带标签的多界面滑动切换
-
Android ViewPager制作新手导航页(动态加载)
-
PagerSlidingTabStrip制作Android带标签的多界面滑动切换
-
Android音乐播放器制作 扫描本地音乐显示在手机(一)