波浪动画实现方法的改进——用自定义SurfaceView实现
来源:互联网 发布:手机精确定位软件 编辑:程序博客网 时间:2024/06/05 14:53
由于之前实现WaveView需要大量的重绘,用SurfaceView实现能有效提升性能。代码如下:
public class WaveSurfaceView extends SurfaceView implements SurfaceHolder.Callback { private int width; private int height; private float[] y; private float[] y2; private float waveLength; private Paint paint; private Paint paint2; private Canvas canvas; private boolean isDraw = false; private float vScale; SurfaceHolder holder; public WaveSurfaceView(Context context) { this(context, null); } public WaveSurfaceView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public WaveSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); paint = new Paint(); paint.setARGB(80, 0, 137, 228); paint.setAntiAlias(true); paint2 = new Paint(); paint2.setARGB(150, 0, 137, 228); paint2.setAntiAlias(true); holder = getHolder(); holder.addCallback(this); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); width = MeasureSpec.getSize(widthMeasureSpec); height = MeasureSpec.getSize(heightMeasureSpec); /*以720*1280分辨率屏幕为基准得出速度和振幅的放大因子(由于控件不一定占满屏幕,以width和height计算是不严谨的)*/ vScale = (float) (width / 720.0); float aScale = (float) (height / 1280.0); /*振幅*/ int a = (int) (25 * aScale); /*初相位*/ int b = 0; /*人为设定的波长*/ waveLength = (float) (3.0 / 2 * width); /*波长确定后计算出的频率*/ float w = (float) (4.0 / 3 * Math.PI / width); /*存储y坐标的数组(横坐标为0的点+波长的长度)*/ y = new float[(int) (waveLength + 1)]; /*为每一个y赋值*/ for (int i = 0; i < waveLength + 1; i++) { y[i] = (float) (a * Math.sin(w * i + b)); } int a2 = (int) (35 * aScale); float b2 = (float) (Math.PI / 3); float w2 = (float) (2 * Math.PI / width); y2 = new float[width + 1]; for (int i = 0; i < width + 1; i++) { y2[i] = (float) (a2 * Math.sin(w2 * i + b2)); } } @Override public void surfaceCreated(final SurfaceHolder holder) { isDraw = true; new Thread(new Runnable() { @Override public void run() { while (isDraw) { try { drawWave(); } catch (Exception e) { e.printStackTrace(); } finally { /*提交每次更新*/ if (canvas != null) { holder.unlockCanvasAndPost(canvas); } } } } }).start(); } private void drawWave() { /*移动速度*/ int v = (int) (4 * vScale); int v2 = (int) (4 * vScale); canvas = holder.lockCanvas(); /*获取到的canvas对象不是新的,所以每次更新前用drawRGB方法进行擦除以更新而不是覆盖*/ canvas.drawRGB(249, 245, 237); for (int i = 0; i < waveLength + 1; i++) { if (i + v <= waveLength) { y[i] = y[i + v]; } else { y[i] = y[((int) (i + v - waveLength))]; } if (i <= width) { /*细分法画出每一条竖直的线*/ canvas.drawLine(i, y[i] + height * 2 / 3, i, height, paint); } } for (int i = 0; i < width + 1; i++) { if (i + v2 <= width) { y2[i] = y2[i + v2]; } else { y2[i] = y2[i + v2 - width]; } canvas.drawLine(i, y2[i] + height * 2 / 3, i, height, paint2); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { isDraw = false; }}
0 0
- 波浪动画实现方法的改进——用自定义SurfaceView实现
- 自定义View——实现波浪动画
- Android自定义控件--波浪球SurfaceView实现
- Android 自定义View实现波浪动画
- android自定义SurfaceView实现跑男动画
- Android 自定义SurfaceView实现加载GIF动画
- UIBezierPath、CADisplayLink实现波浪动画
- SurfaceView实现动画
- Android SurfaceView实现动画
- SurfaceView实现帧动画
- 实现一个自定义波浪View
- Android自定义View的动画实现方法
- 实现自定义View,SurfaceView的基本框架
- 猎豹清理大师波浪动画实现
- html5Canvas动画实现球内波浪效果
- Android实现波浪动画效果(Bezier)
- 纯css实现波浪动画,超级简单
- Android自定义SurfaceView——实现画板功能
- VM下CentOS7x86-64bit+JDK1.8+hadoop2.7.2安装部署
- 机房重构——存储过程详解
- mybatis连接mysql数据库插入中文乱码
- NYOJ 超级台阶
- PHP实现多线程抓取网页
- 波浪动画实现方法的改进——用自定义SurfaceView实现
- Leetcode 4. Median of Two Sorted Arrays
- 过程质量和结果质量
- IM同步协议
- 4.5.3 Async耗时操作必须由子线程完成,更新UI需要由主线程完成
- [leetcode] 166. Fraction to Recurring Decimal
- 应邀ITGeGe在线教育社区嵌入式基础开发讲师
- 加权GN算法的Java实现
- curl_init()和curl_multi_init()多线程的速度比较