Android的SurfaceView闪烁

发布于 2024-11-30 03:22:54 字数 2009 浏览 1 评论 0原文

我想创建一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁我热情 2024-12-07 03:22:54

我建议您不要使用 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 on View is slowly, but only when you don't know how to effectively use invalidate().

I mean don't use invalidate() (which draws all canvas), but invalidate(dirtyRect) (You define the rectangular which you want to redraw, and the rest of the canvas stays just like before) it's very fast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文