【问题标题】:Android SurfaceView draws only twiceAndroid SurfaceView 只绘制两次
【发布时间】:2018-05-07 18:42:12
【问题描述】:

我试图在surfaceView上简单地绘制一些东西,即使我使用它也只会渲染两次

setWillNotDraw(假)

我怎样才能让我的 onDraw 方法每次都被调用,而不仅仅是两次?

screen of log

代码:

public class MainActivity extends Activity implements SurfaceHolder.Callback {

private static final String TAG = "SurfaceView";
private SurfaceView surfaceView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    surfaceView = new SurfaceView(this);
    surfaceView.setWillNotDraw(false);

    setContentView(surfaceView);
    surfaceView.getHolder().addCallback(this);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    tryDrawing(holder);
    surfaceView.setWillNotDraw(false);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int frmt, int w, int h) {
    tryDrawing(holder);
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {}

private void tryDrawing(SurfaceHolder holder) {
    Log.i(TAG, "Trying to draw...");
    Canvas canvas = holder.lockCanvas();
    if (canvas == null) {
        Log.e(TAG, "Cannot draw onto the canvas as it's null");
    } else {
        drawMyStuff(canvas);
        holder.unlockCanvasAndPost(canvas);
    }
}

private void drawMyStuff(final Canvas canvas) {
    Log.i(TAG, "Drawing...");
    Random random = new Random();
    canvas.drawRGB(random.nextInt(255), random.nextInt(255), random.nextInt(255));
}

}

【问题讨论】:

    标签: java android rendering


    【解决方案1】:

    尝试像这样从 View 扩展您自己的类,并在 onDraw() 方法中无效:

    public class ScreenView extends View {
    
        private Context context;
        private Paint paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
    
        public ViewStats(Context context) {
            super(context);
            this.context = context;
            init();
        }
    
        public ViewStats(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.context = context;
            init();
        }
    
        public ViewStats(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            this.context = context;
            init();
        }
    
        private void init() {
            paintLine.setStyle(Paint.Style.FILL_AND_STROKE);
            paintLine.setColor(0xFFE4455B);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            Random random = new Random();
            canvas.drawRGB(random.nextInt(255), random.nextInt(255), random.nextInt(255));
            invalidate();
        }
    }
    

    【讨论】:

    • 这行得通,谢谢!为什么surfaceView 不能那样工作?
    猜你喜欢
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多