android 实时绘图的问题 android实时绘图,根据服务器推来的数据实时绘图,使用的缓冲技术。但还是会出现屏幕闪烁。如何解决。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 最好在真机上测试下看看之前做video的demo 2.1的模拟器都不能运行(1.5的可以)模拟器效果和真机效果也是不一样的 过程是这样的:a.当有数据推过来。b.整个图向左边移动一个像素,同时,在图的最右边绘这一数据c.如此反复。如果,数据来的快,这样整个图不断地向左边移动,就会产生闪烁。我使用了缓冲技术,但解决不了这一问题。 如果LZ采用的是front buffer, back buffer的话,个人建议是不要一获得数据就马上放到事件处理队列里。最好间隔5-6个数据处理一次或者间隔多少时间刷新一次,你再看看效果吧。很奇怪有必要那么精确么?来一个数据就马上处理,很容易出现闪烁的。 高手,想不到android进展如此神速。 以下是我的测试程序,使用了缓冲技术,但还会出现闪烁,即使是调整了数据产生频率。大家有兴趣可试试。看看如何解决。谢谢大家package com.test.activity;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.Bitmap.Config;import android.graphics.Paint.Style;import android.os.AsyncTask;import android.os.Bundle;import android.view.View;public class FirstAct extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ViewA sv = new ViewA(this.getApplicationContext()); setContentView(sv); } class ViewA extends View { private int width = 0; private int high = 0; private boolean ifFirstRun = true; private Bitmap cachebit = null; private Canvas cachecan = null; private Paint paint = null; private int i = 0; private int offset = 0; private Bitmap bitmapcache = null; private Canvas canvascache = null; private int MAX = 34; private int MIN = 0; public ViewA(Context context) { super(context); } protected void onDraw(Canvas canvas) { if(ifFirstRun) { ifFirstRun = false; width = this.getWidth(); high = this.getHeight(); cachebit = Bitmap.createBitmap(3000, high, Config.ARGB_8888); cachecan = new Canvas(cachebit); cachecan.translate(0, cachebit.getHeight()); paint = new Paint(Paint.ANTI_ALIAS_FLAG); bitmapcache = Bitmap.createBitmap(width, high, Config.ARGB_8888); canvascache = new Canvas(bitmapcache); new CanvasImageTask().execute(this,null,null); } canvascache.drawRect(new Rect(0,0,width,high), paint); canvascache.drawBitmap(cachebit, -offset, 0, paint); canvas.drawBitmap(bitmapcache, 0, 0, paint); } class CanvasImageTask extends AsyncTask<ViewA, Void, Void> { protected Void doInBackground(ViewA... view) { Paint paint = new Paint(); paint.setColor(Color.BLUE); paint.setStyle(Style.STROKE); double height_val = (double)(MAX - MIN) / (double)high; while(true) { double volume = Math.random() * 25; int a = i + 1; cachecan.drawLine(a, -(float)((MAX - volume) / height_val), a, 0, paint); if(i > width) { offset += 1; paint.setColor(Color.YELLOW); } i = a + 1; view[0].postInvalidateDelayed(1000); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } }} 或者LZ试试在画线的代码前后加上canvas的save和restore方法看看?这样你画线的操作只对线有影响,其他不会动。个人还是认为实时更新如果是整个screen的话就会造成闪烁,改成某一部分会好一点吧? android实时绘图坐标图绘制代码怎么写? 双缓存的实现机制问题:http://blog.chinaunix.net/u2/85805/showart_1882368.html参考 理论上不会这么闪,一般android底层就是自带的双缓存技术 楼主 ,想问个问题,Android的实时性,到底如何啊?能不能做到实时显示接收到的数据? 对于绘图的复杂操作可以用android 提供的SurfaceView 抽象类解决。 刚刚学习Android.也想解决楼主的问题。帮不上什么忙。帮忙顶吧。 关于android布局由于屏幕大小产生的问题 http onCreate 函数中无法 获得控件 ,宽 ,高 ,位置信息??? 把声音播放的输出设置成MIC Android Tab使用问题 模拟器问题:部署时不报错,模拟器可正常运行,但是程序没被有正确部署 Android Listview Listactivity 一个手机空闲状态广播问题 synchronized一个Object对象是为了什么? 【求助】Android视频预览图像拉伸的问题 关于GPS定位 出现坐标一样的问题 android中这个控件用什么做,或者如何做?
最好在真机上测试下看看
之前做video的demo 2.1的模拟器都不能运行(1.5的可以)
模拟器效果和真机效果也是不一样的
a.当有数据推过来。
b.整个图向左边移动一个像素,同时,在图的最右边绘这一数据
c.如此反复。
如果,数据来的快,这样整个图不断地向左边移动,就会产生闪烁。
我使用了缓冲技术,但解决不了这一问题。
最好间隔5-6个数据处理一次或者间隔多少时间刷新一次,你再看看效果吧。
很奇怪有必要那么精确么?来一个数据就马上处理,很容易出现闪烁的。
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.Style;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;public class FirstAct extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ViewA sv = new ViewA(this.getApplicationContext());
setContentView(sv);
}
class ViewA extends View
{
private int width = 0;
private int high = 0;
private boolean ifFirstRun = true;
private Bitmap cachebit = null;
private Canvas cachecan = null;
private Paint paint = null;
private int i = 0;
private int offset = 0;
private Bitmap bitmapcache = null;
private Canvas canvascache = null;
private int MAX = 34;
private int MIN = 0;
public ViewA(Context context)
{
super(context);
}
protected void onDraw(Canvas canvas)
{
if(ifFirstRun)
{
ifFirstRun = false;
width = this.getWidth();
high = this.getHeight();
cachebit = Bitmap.createBitmap(3000, high, Config.ARGB_8888);
cachecan = new Canvas(cachebit);
cachecan.translate(0, cachebit.getHeight());
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
bitmapcache = Bitmap.createBitmap(width, high, Config.ARGB_8888);
canvascache = new Canvas(bitmapcache);
new CanvasImageTask().execute(this,null,null);
}
canvascache.drawRect(new Rect(0,0,width,high), paint);
canvascache.drawBitmap(cachebit, -offset, 0, paint);
canvas.drawBitmap(bitmapcache, 0, 0, paint);
}
class CanvasImageTask extends AsyncTask<ViewA, Void, Void>
{
protected Void doInBackground(ViewA... view)
{
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Style.STROKE);
double height_val = (double)(MAX - MIN) / (double)high;
while(true)
{
double volume = Math.random() * 25;
int a = i + 1;
cachecan.drawLine(a, -(float)((MAX - volume) / height_val), a, 0, paint);
if(i > width)
{
offset += 1;
paint.setColor(Color.YELLOW);
}
i = a + 1; view[0].postInvalidateDelayed(1000);
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
}
这样你画线的操作只对线有影响,其他不会动。
个人还是认为实时更新如果是整个screen的话就会造成闪烁,改成某一部分会好一点吧?
http://blog.chinaunix.net/u2/85805/showart_1882368.html
参考
也想解决楼主的问题。帮不上什么忙。帮忙顶吧。