如何制作出類似雷達(dá)掃描的效果,具體方法如下
創(chuàng)新互聯(lián)專注于永寧網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供永寧營銷型網(wǎng)站建設(shè),永寧網(wǎng)站制作、永寧網(wǎng)頁設(shè)計、永寧網(wǎng)站官網(wǎng)定制、重慶小程序開發(fā)公司服務(wù),打造永寧網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供永寧網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
一、效果圖
二、實現(xiàn)思路
1、自定義控件RadarView用來畫雷達(dá)的效果圖,可以自定義屬性包括
backgroundColor:背景顏色
circleNum:圓的數(shù)量
startColor:開始顏色
endColor:結(jié)束顏色
lineColor:線的顏色
2、通過Handler循環(huán)發(fā)送消息到MessageQueue中,將mRotate加3,使Matrix旋轉(zhuǎn)mRotate,重繪雷達(dá)掃描的圓。
3、通過梯度漸變掃描渲染器SweepGradient,在繪制圓的過程中,將顏色從startColor變?yōu)閑ndColor。
三、實例代碼
public class RadarView extends View { private final String TAG = "RadarView"; private static final int MSG_WHAT = 1; private static final int DELAY_TIME = 20; //設(shè)置默認(rèn)寬高,雷達(dá)一般都是圓形,所以我們下面取寬高會取Math.min(寬,高) private final int DEFAULT_WIDTH = 200; private final int DEFAULT_HEIGHT = 200; //雷達(dá)的半徑 private int mRadarRadius; //雷達(dá)畫筆 private Paint mRadarPaint; //雷達(dá)底色畫筆 private Paint mRadarBg; //雷達(dá)圓圈的個數(shù),默認(rèn)4個 private int mCircleNum = 4; //雷達(dá)線條的顏色,默認(rèn)為白色 private int mCircleColor = Color.WHITE; //雷達(dá)圓圈背景色 private int mRadarBgColor = Color.BLACK; //paintShader private Shader mRadarShader; //雷達(dá)掃描時候的起始和終止顏色 private int mStartColor = 0x0000ff00; private int mEndColor = 0xaa00ff00; private Matrix mMatrix; //旋轉(zhuǎn)的角度 private int mRotate = 0; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); mRotate += 3; postInvalidate(); mMatrix.reset(); mMatrix.preRotate(mRotate, 0, 0); //延時DELAY_TIME后再發(fā)送消息 mHandler.sendEmptyMessageDelayed(MSG_WHAT, DELAY_TIME); } }; 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(context, attrs); //設(shè)置抗鋸齒 mRadarBg = new Paint(Paint.ANTI_ALIAS_FLAG); //畫筆顏色 mRadarBg.setColor(mRadarBgColor); //畫實心圓 mRadarBg.setStyle(Paint.Style.FILL); //設(shè)置抗鋸齒 mRadarPaint = new Paint(Paint.ANTI_ALIAS_FLAG); //畫筆顏色 mRadarPaint.setColor(mCircleColor); //設(shè)置空心的畫筆,只畫圓邊 mRadarPaint.setStyle(Paint.Style.STROKE); //畫筆寬度 mRadarPaint.setStrokeWidth(2); //使用梯度漸變渲染器, mRadarShader = new SweepGradient(0, 0, mStartColor, mEndColor); mMatrix = new Matrix(); } //初始化,拓展可設(shè)置參數(shù)供布局使用 private void init(Context context, AttributeSet attrs) { if (attrs != null) { TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RadarView); mStartColor = ta.getColor(R.styleable.RadarView_startColor, mStartColor); mEndColor = ta.getColor(R.styleable.RadarView_endColor, mEndColor); mRadarBgColor = ta.getColor(R.styleable.RadarView_backgroundColor, mRadarBgColor); mCircleColor = ta.getColor(R.styleable.RadarView_lineColor, mCircleColor); mCircleNum = ta.getInteger(R.styleable.RadarView_circleNum, mCircleNum); ta.recycle(); } } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); //雷達(dá)的半徑為寬的一半或高的一半的最小值 mRadarRadius = Math.min(w / 2, h / 2); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //獲取寬度 int width = measureSize(1, DEFAULT_WIDTH, widthMeasureSpec); //獲取高度 int height = measureSize(0, DEFAULT_HEIGHT, heightMeasureSpec); //取最大的 寬|高 int measureSize = Math.max(width, height); setMeasuredDimension(measureSize, measureSize); } /** * 測繪measure * * @param specType 1為寬, 其他為高 * @param contentSize 默認(rèn)值 */ private int measureSize(int specType, int contentSize, int measureSpec) { int result; //獲取測量的模式和Size int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { result = Math.max(contentSize, specSize); } else { result = contentSize; if (specType == 1) { // 根據(jù)傳入方式計算寬 result += (getPaddingLeft() + getPaddingRight()); } else { // 根據(jù)傳入方式計算高 result += (getPaddingTop() + getPaddingBottom()); } } return result; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Log.d(TAG, "onDraw " + mRotate); mRadarBg.setShader(null); //將畫布移動到屏幕的中心點 canvas.translate(mRadarRadius, mRadarRadius); //繪制底色,讓雷達(dá)的線看起來更清晰 canvas.drawCircle(0, 0, mRadarRadius, mRadarBg); //畫圓圈 for (int i = 1; i <= mCircleNum; i++) { canvas.drawCircle(0, 0, (float) (i * 1.0 / mCircleNum * mRadarRadius), mRadarPaint); } //繪制雷達(dá)基線 x軸 canvas.drawLine(-mRadarRadius, 0, mRadarRadius, 0, mRadarPaint); //繪制雷達(dá)基線 y軸 canvas.drawLine(0, mRadarRadius, 0, -mRadarRadius, mRadarPaint); //設(shè)置顏色漸變從透明到不透明 mRadarBg.setShader(mRadarShader); //設(shè)置矩陣 canvas.concat(mMatrix); canvas.drawCircle(0, 0, mRadarRadius, mRadarBg); } public void startScan() { mHandler.removeMessages(MSG_WHAT); mHandler.sendEmptyMessage(MSG_WHAT); } public void stopScan() { mHandler.removeMessages(MSG_WHAT); } }
布局文件:
<RelativeLayout 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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" > <com.android.demo.ui.shader.RadarView android:id="@+id/radarview" android:layout_width="300dp" android:layout_height="300dp" android:layout_centerInParent="true" app:backgroundColor="#000000" app:circleNum="4" app:endColor="#aaff0000" app:lineColor="#00ff00" app:startColor="#aa0000ff"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" android:onClick="start" android:text="開始" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:onClick="stop" android:text="停止" /> </RelativeLayout>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
分享文章:基于Android自定義控件實現(xiàn)雷達(dá)效果
路徑分享:http://www.muchs.cn/article4/pgdgie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、虛擬主機(jī)、定制開發(fā)、云服務(wù)器、網(wǎng)站設(shè)計、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)