概述
摘要:
今天群里(QQ群64026923)的同学在讨论雷达的效果怎么实现,机智的小伙伴们马上想到让美工切图,然后再给图片做动画不就行了吗???确实这个方法也不错,那么还有没有其他的方法呢?先来看看效果图:。
API分析
SweepGradient扫描/梯度渲染,
public SweepGradient(float cx, float cy, int[] colors, float[] positions)
Parameters:
cx | 渲染中心点x 坐标 |
---|---|
cy | 渲染中心y 点坐标 |
colors | 围绕中心渲染的颜色数组,至少要有两种颜色值 |
positions | 相对位置的颜色数组,可为null, 若为null,可为null,颜色沿渐变线均匀分布 |
public SweepGradient(float cx, float cy, int color0, int color1)
Parameters:
cx | 渲染中心点x 坐标 |
---|---|
cy | 渲染中心点y 坐标 |
color0 | 起始渲染颜色 |
color1 | 结束渲染颜色 |
效果实现:
import java.util.ArrayList;
import java.util.Random;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
/**
* SweepGradient扫描/梯度渲染
* @author mick
*
*/
public class RadaView extends FrameLayout {
private int viewSize = 800;
private Paint mPaintLine;
private Paint mPaintSector;
// 扫描效果的画笔
public boolean isstart = false;
private ScanThread mThread;
// 扫描的线程
private int start = 0;
private Matrix matrix;
public RadaView(Context context, AttributeSet attrs) {
super(context, attrs);
initPaint();
mThread = new ScanThread(this);
setBackgroundColor(Color.TRANSPARENT);
}
public RadaView(Context context) {
this(context, null);
}
/**
* 画笔的基本设置
*/
private void initPaint() {
mPaintLine = new Paint();
mPaintLine.setStrokeWidth(10);
mPaintLine.setAntiAlias(true);
mPaintLine.setStyle(Style.STROKE);
mPaintLine.setColor(0xff000000);
mPaintSector = new Paint();
mPaintSector.setColor(0x9D00ff00);
mPaintSector.setAntiAlias(true);
}
public void setViewSize(int size) {
this.viewSize = size;
setMeasuredDimension(viewSize, viewSize);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(viewSize, viewSize);
}
/**
* 开始扫描
*/
public void startScann() {
mThread.start();
isstart = true;
}
/**
* 暂停扫描
*/
public void stopScann() {
if (isstart) {
Thread.interrupted();
isstart = false;
}
}
public void setList(ArrayList<View> list) {
for (int i = 0; i < list.size(); i++) {
int xy[] = getRamdomXY();
list.get(i).setX(xy[0]);
list.get(i).setY(xy[1]);
addView(list.get(i));
}
}
private int[] getRamdomXY() {
Random rand = new Random();
int x = rand.nextInt(900);
int y = rand.nextInt(900);
int r = (int) ((float) viewSize / 2);
if ((x >= r - 350 || x <= r + 350) && (y >= r - 350 || y <= r + 350)) {
int xy[] = new int[2];
xy[0] = x;
xy[1] = y;
return xy;
} else {
return getRamdomXY();
}
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(viewSize / 2, viewSize / 2, 175, mPaintLine);
canvas.drawCircle(viewSize / 2, viewSize / 2, 350, mPaintLine);
canvas.drawLine(viewSize / 2, 0, viewSize / 2, viewSize, mPaintLine);
canvas.drawLine(0, viewSize / 2, viewSize, viewSize / 2, mPaintLine); //
/**
* SweepGradient扫描/梯度渲染
* SweepGradient(float cx, float cy, int color0, int color1)
* cx 渲染中心点x 坐标
* cy 渲染中心点y 坐标
* color0 起始渲染颜色
* color1 结束渲染颜色
*
*/
Shader mShader = new SweepGradient(viewSize / 2, viewSize / 2, Color.TRANSPARENT, Color.GREEN);
mPaintSector.setShader(mShader);
canvas.concat(matrix);
canvas.drawCircle(viewSize / 2, viewSize / 2, 350, mPaintSector);
super.onDraw(canvas);
}
protected class ScanThread extends Thread {
private RadaView view;
public ScanThread(RadaView view) {
this.view = view;
}
@Override
public void run() {
while (true) {
if (isstart) {
view.post(new Runnable() {
public void run() {
start = start + 1;
matrix = new Matrix();
matrix.postRotate(start, viewSize / 2, viewSize / 2);
view.invalidate();
}
});
try {
Thread.sleep(10);
// 设置扫描的停止时间
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
------Android开发基友群:64026923,众多妹纸、基友等着你哟。加的是群,涨的是技术:
最后
以上就是单身香烟为你收集整理的Android 雷达扫描效果、动画SweepGradient的全部内容,希望文章能够帮你解决Android 雷达扫描效果、动画SweepGradient所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复