Android的SurfaceView闪烁
我想创建一个简单的 Android 应用程序,它将在我触摸屏幕的地方画圆圈。它可以与 View 一起使用(缓慢地),但不能与 SurfaceView 一起使用。结果很奇怪——点击时,整个图像都在移动。我也尝试从另一个线程调用绘图函数,但结果是相同的。还发现了另一个具有这种奇怪行为的例子: http://android-er.blogspot.com/2010/05/android -surfaceview.html 我使用 Android 2.3.3,API 级别 10。如有任何帮助,我们将不胜感激。
package com.samsung.sketchbook;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SketchBook extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BookView bookView = new BookView(this);
setContentView(bookView);
}
class BookView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
private Paint paint = new Paint();
private float x, y;
public BookView(Context context) {
super(context);
holder = getHolder();
holder.addCallback(this);
paint.setColor(Color.WHITE);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
Log.d("BookView", "surfaceCreated!");
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
x = motionEvent.getX();
y = motionEvent.getY();
Canvas canvas = holder.lockCanvas(null);
canvas.drawCircle(x, y, 3, paint);
holder.unlockCanvasAndPost(canvas);
return true;
}
}
}
I want to create simple Android application which will draw circles in place where I touch the screen. It is working with View (slowly), but with SurfaceView not. The result is strange - when clicking, whole image is moving. I also tried to call drawing function from another thread, but results are the same. Also found another example with this strange behavior:
http://android-er.blogspot.com/2010/05/android-surfaceview.html
I work with Android 2.3.3, API level 10. Any help would be appreciated.
package com.samsung.sketchbook;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SketchBook extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BookView bookView = new BookView(this);
setContentView(bookView);
}
class BookView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
private Paint paint = new Paint();
private float x, y;
public BookView(Context context) {
super(context);
holder = getHolder();
holder.addCallback(this);
paint.setColor(Color.WHITE);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
Log.d("BookView", "surfaceCreated!");
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
x = motionEvent.getX();
y = motionEvent.getY();
Canvas canvas = holder.lockCanvas(null);
canvas.drawCircle(x, y, 3, paint);
holder.unlockCanvasAndPost(canvas);
return true;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您不要使用
SurfaceView
。我的经验表明,这并不是比
View
更好的解决方案。是的,在View
上扩展的实现类速度很慢,但只有当您不知道如何有效使用invalidate()
时。我的意思是不要使用
invalidate()
(绘制所有画布),而是使用invalidate(dirtyRect)
(您定义要重绘的矩形,其余部分画布保持像以前一样)速度非常快。I can recommend you don't use
SurfaceView
.My experience say that it isn't a better solution than
View
. Yes, implementation class which extends onView
is slowly, but only when you don't know how to effectively useinvalidate()
.I mean don't use
invalidate()
(which draws all canvas), butinvalidate(dirtyRect)
(You define the rectangular which you want to redraw, and the rest of the canvas stays just like before) it's very fast.