Android SurfaceView实现静态于动态画图效果
本文是基于Android的SurfaceView的动态画图效果,实现静态和动态下的正弦波画图,可作为自己做图的简单参考,废话不多说,先上图,
静态效果:
动态效果:
比较简单,代码注释的也比较详细,易懂,我就直接上代码了吧:
1.main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <SurfaceView
- android:layout_width="fill_parent"
- android:layout_height="600px"
- android:id="@+id/surfaceview_draw"/>
- <RelativeLayout android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <Button android:layout_below="@id/surfaceview_draw"
- android:layout_marginLeft="7dip"
- android:id="@+id/simpledraw_btn"
- android:text="简单绘图"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20dip"/>
- <Button
- android:id="@+id/timerdraw_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@id/simpledraw_btn"
- android:text="定时绘图"
- android:textSize="20dip"/>
- <Button
- android:id="@+id/cleardraw_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@id/timerdraw_btn"
- android:text="清除图像"
- android:textSize="20dip"/>
- </RelativeLayout>
- </LinearLayout>
2.SurfaceViewDrawActivity.java
- package com.huangcheng.draw;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.app.Activity;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class SurfaceViewDrawActivity extends Activity {
- /** Called when the activity is first created. */
- private SurfaceView surface_draw;
- private SurfaceHolder surfaceholder;
- private Button simpledraw_btn;
- private Button timerdraw_btn;
- private Button cleardraw_btn;
- private Timer timer;
- private TimerTask timertask;
- private int[] Y;// 保存图像Y轴上的点
- private int centerY, oldX, oldY, currentX;// 保存水平中心线位置,上一个(x,y)点,当先绘制到的x
- ;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- surface_draw = (SurfaceView) findViewById(R.id.surfaceview_draw);
- surfaceholder = surface_draw.getHolder();
- simpledraw_btn = (Button) findViewById(R.id.simpledraw_btn);
- timerdraw_btn = (Button) findViewById(R.id.timerdraw_btn);
- cleardraw_btn = (Button) findViewById(R.id.cleardraw_btn);
- cleardraw_btn.setEnabled(false);
- centerY = ;
- Y = new int[getWindowManager().getDefaultDisplay().getWidth()];
- ; i < Y.length + ; i++) {
- Y[i - ] = centerY + ( * Math.sin(i * * Math.PI / ));
- // System.out.println("sin:"+100*Math.sin(i*2*Math.PI/180));
- }
- simpledraw_btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- simpledraw();// 静态绘制正弦图
- signal=;
- simpledraw_btn.setEnabled(false);
- timerdraw_btn.setEnabled(false);
- cleardraw_btn.setEnabled(true);
- }
- });
- timerdraw_btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- oldX=;
- oldY=centerY;
- currentX=;
- timer=new Timer();
- timertask=new TimerTask() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- currentX++;
- complexdraw(currentX);
- ){
- ClearDraw();
- currentX = ;
- oldX=;
- oldY=centerY;
- }
- }
- };
- timer.schedule(timertask, , );// 动态绘制正弦图
- signal=;
- simpledraw_btn.setEnabled(false);
- timerdraw_btn.setEnabled(false);
- cleardraw_btn.setEnabled(true);
- }
- });
- cleardraw_btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- ){
- ClearDraw();
- simpledraw_btn.setEnabled(true);
- timerdraw_btn.setEnabled(true);
- }
- ){
- timertask.cancel();
- timer.cancel();
- ClearDraw();
- simpledraw_btn.setEnabled(true);
- timerdraw_btn.setEnabled(true);
- }
- }
- });
- }
- // 简单画图
- void simpledraw() {
- oldX = ;
- oldY = centerY;
- Canvas canvas = surfaceholder
- .lockCanvas(, oldX + Y.length,
- getWindowManager().getDefaultDisplay().getHeight()));// 关键:获取画布
- Log.i("Canvas:X:",
- String.valueOf(oldX) + "," + String.valueOf(oldX + Y.length));
- Paint mPaint = new Paint();
- mPaint.setColor(Color.GREEN);// 画笔为绿色
- mPaint.setStrokeWidth();// 设置画笔粗细
- int y;
- ; i < Y.length; i++) {// 绘画正弦波
- y = Y[i - ];
- canvas.drawLine(oldX, oldY, i, y, mPaint);
- // System.out.println("oldX:"+oldX+" oldY:"+oldY+",x:"+i+" y:"+y);
- oldX = i;
- oldY = y;
- }
- surfaceholder.unlockCanvasAndPost(canvas);
- }
- // 消除画得图
- void ClearDraw() {
- Canvas canvas = surfaceholder.lockCanvas(null);
- canvas.drawColor(Color.BLACK);// 清除画布
- surfaceholder.unlockCanvasAndPost(canvas);
- }
- //动态画图
- void complexdraw(int current){
- Canvas canvas = surfaceholder
- .lockCanvas(, current,
- getWindowManager().getDefaultDisplay().getHeight()));// 关键:获取画布
- Log.i("Canvas:X:",
- String.valueOf(oldX) + "," + String.valueOf(oldX + Y.length));
- Paint mPaint = new Paint();
- mPaint.setColor(Color.GREEN);// 画笔为绿色
- mPaint.setStrokeWidth();// 设置画笔粗细
- ];
- canvas.drawLine(oldX, oldY, current, y, mPaint);
- // System.out.println("oldX:"+oldX+" oldY:"+oldY+",x:"+i+" y:"+y);
- oldX = current;
- oldY = y;
- surfaceholder.unlockCanvasAndPost(canvas);
- }
- }
Android SurfaceView实现静态于动态画图效果的更多相关文章
- 关于Android中Fragment静态和动态加载的方法
一.静态加载 1.首先创建一个layout布局fragment.xml,里面放要显示和操作的控件 2.创建一个layout布局main1.xml,用来实现页面的跳转(跳转为要实现静态加载的界面) 3. ...
- Android中BroadcastReceiver的两种注册方式(静态和动态)详解
今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...
- Android使用SurfaceView实现墨迹天气的风车效果
SurfaceView也是继承自View,它和我们以前接触到的View(Button.TextView等)最大的不同是,SurfaceView可以有一个单独的线程进行绘制,这个线程区别于UI线程(主线 ...
- android 静态和动态设置 Receiver的 android:enabled值
0x 01 前提约束: 0x001 静态检查:指用action限定Intent,并使用包管理器的queryBroadCastReceivers方法,在flags字段置为0时查找ResolveInfo, ...
- Android自定义控件 -Canvas绘制折线图(实现动态报表效果)
有时候我们在项目中会遇到使用折线图等图形,Android的开源项目中为我们提供了很多插件,但是很多时候我们需要根据具体项目自定义这些图表,这一篇文章我们一起来看看如何在Android中使用Canvas ...
- android优化中国风应用、完整NBA客户端、动态积分效果、文件传输、小说阅读器等源码
Android精选源码 android拖拽下拉关闭效果源码 一款优雅的中国风Android App源码 EasySignSeekBar一个漂亮而强大的自定义view15 android仿蘑菇街,蜜芽宝 ...
- Android原理揭秘系列之一动态墙纸
Livewallpaper,即动态墙纸,是Android的一大3D特色功能,用户可以在桌面选择加载动态墙纸,让自己的手机桌面背景旋动起来. 相对于静态桌面壁纸,动态墙纸可以展示各种动态变化的背景,而与 ...
- Android SurfaceView实战 带你玩转flabby bird (下)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43063331,本文出自:[张鸿洋的博客] 1.概述 在Android Surfa ...
- Android SurfaceView实战 带你玩转flabby bird (上)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/42965779 ,本文出自:[张鸿洋的博客] 1.概述 哈,记得以前写过Andro ...
随机推荐
- cocos2dx进阶学习之CCSprite
继承关系 CCSprite -> CCNodeRGBA -> CCNode, CCRGBAProtocol CCTextureProtocol 从继承关系可以看出,CCSp ...
- Fedora 启动 SSH服务
一.Fedora 启动sshd服务: 1.先确认是否已安装ssh服务: [root@localhost ~]# rpm -qa | grep openssh-server openssh-server ...
- Battle ships(二分图,建图,好题)
Battle ships Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- Android Testing(1) 浅尝Android测试的奥秘
------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 仅供学习和交流使用,翻译不好勿喷,请只摘除不合适的地方 Testing The Android fram ...
- 类之string类、Math类、DateTime类
String类 string a = "abcdef123456"; 注:字符串的长度是从0开始计数的如:0,1,2,3,4,5,6,7,8,9........ a.Length; ...
- 【linux】 linux gpio操作
欢迎转载,转载时需保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http:// ...
- [Swust OJ 603]--吃饺子大王
题目链接:http://acm.swust.edu.cn/problem/603/ Time limit(ms): 1000 Memory limit(kb): 65535 同学们还记得我们班 ...
- BZOJ 3384: [Usaco2004 Nov]Apple Catching 接苹果( dp )
dp dp( x , k ) = max( dp( x - 1 , k - 1 ) + *** , dp( x - 1 , k ) + *** ) *** = 0 or 1 ,根据情况 (BZOJ 1 ...
- Oracle AWR 报告详解
转自:http://blog.csdn.net/laoshangxyc/article/details/8615187 持续更新中... Oracle awr报告详解 DB Name DB Id In ...
- 基于visual Studio2013解决算法导论之015第二小元素
题目 查找第二小元素 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> ...