在Android的SurfaceView上绘图

时间:2019-08-06 11:19:43

标签: java android canvas android-canvas surfaceview

当我单击按钮时,我想在surfaceView上涂成黑色,但是,似乎我的代码弄乱了一些东西,它始终是白色的,我已经测试过按钮并且可以使用:

SurfaceView surfaceView;
   SurfaceHolder holder;
   Canvas canvas;

@Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

     setContentView(R.layout.main_layout);

      surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
      surfaceView.getHolder().addCallback(this);



      ImageButton paintInBlack;
      paintInBlack = (ImageButton) findViewById(R.id.button1);

      paintInBlack .setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Log.d( "test", "test" );
            Canvas canvas = new Canvas(mBitmap);
            Paint blackPaint = new Paint();
            blackPaint.setColor(Color.BLACK);
            blackPaint.setStyle(Paint.Style.FILL);
            canvas.drawPaint(blackPaint);
            canvas = holder.lockCanvas();
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
            holder.unlockCanvasAndPost(canvas);
         }

我在哪里做错了?谢谢!

3 个答案:

答案 0 :(得分:0)

在lockCanvas()之后绘制

        SurfaceHolder surfaceHolder = getHolder();
        Canvas canvas = surfaceHolder.lockCanvas();
        render(canvas); //here draw what you want
        surfaceHolder.unlockCanvasAndPost(canvas);

答案 1 :(得分:0)

Canvas canvas = holder.lockCanvas();

Paint blackPaint = new Paint();
blackPaint.setColor(Color.CYAN);
blackPaint.setStyle(Paint.Style.FILL);
canvas.drawPaint(blackPaint);

holder.unlockCanvasAndPost(canvas);

OR

// Get and lock canvas object from surfaceHolder.
Canvas canvas = holder.lockCanvas();
Paint surfaceBackground = new Paint();
// Set the surfaceview background color.
surfaceBackground.setColor(Color.CYAN);
// Draw the surfaceview background color.
canvas.drawRect(0, 0, surfaceView.getWidth(), surfaceView.getHeight(), surfaceBackground);
// Unlock the canvas object and post the new draw.
holder.unlockCanvasAndPost(canvas);

https://www.dev2qa.com/android-surfaceview-drawing-example/

答案 2 :(得分:0)

使用以下示例实现您想要的:

1)MainActivity.class:

public class MainActivity extends AppCompatActivity {

private final String TAG = MainActivity.class.getSimpleName();
private Button b_draw_all;
private Button b_draw_color;
private Button b_draw_bitmap;
private Button b_draw_text;
private Button b_clear;
private SurfaceView sv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b_clear = (Button) findViewById(R.id.b_clear);
    b_clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (sv.getHolder().getSurface().isValid()) {
                Canvas canvas = sv.getHolder().lockCanvas();
                clearCanvas(canvas);
                sv.getHolder().unlockCanvasAndPost(canvas);
            }
        }
    });

    b_draw_color = (Button) findViewById(R.id.b_draw_color);
    b_draw_color.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (sv.getHolder().getSurface().isValid()) {
                Canvas canvas = sv.getHolder().lockCanvas();
                clearCanvas(canvas);
                drawColor(canvas, Color.MAGENTA);
                sv.getHolder().unlockCanvasAndPost(canvas);
            }
        }
    });

    b_draw_bitmap = (Button) findViewById(R.id.b_draw_bitmap);
    b_draw_bitmap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (sv.getHolder().getSurface().isValid()) {
                Canvas canvas = sv.getHolder().lockCanvas();
                clearCanvas(canvas);
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
                drawBitmap(canvas, bitmap);
                sv.getHolder().unlockCanvasAndPost(canvas);
            }
        }
    });

    b_draw_text = (Button) findViewById(R.id.b_draw_text);
    b_draw_text.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (sv.getHolder().getSurface().isValid()) {
                Canvas canvas = sv.getHolder().lockCanvas();
                clearCanvas(canvas);
                drawText(canvas, "Android");
                sv.getHolder().unlockCanvasAndPost(canvas);
            }
        }
    });

    b_draw_all = (Button) findViewById(R.id.b_draw_all);
    b_draw_all.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (sv.getHolder().getSurface().isValid()) {
                Canvas canvas = sv.getHolder().lockCanvas();
                //order is important
                //draw color, bitmap and then text
                drawColor(canvas, Color.MAGENTA);
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
                drawBitmap(canvas, bitmap);
                drawText(canvas, "Android");
                sv.getHolder().unlockCanvasAndPost(canvas);
            }
        }
    });

    sv = (SurfaceView) findViewById(R.id.sv);
    sv.post(new Runnable() {
        @Override
        public void run() {
            if (sv.getHolder().getSurface().isValid()) {
                Canvas canvas = sv.getHolder().lockCanvas();
                clearCanvas(canvas);
                sv.getHolder().unlockCanvasAndPost(canvas);
            }
        }
    });
}


private int[] getSurfaceDimensions(){
    return new int[]{sv.getWidth(), sv.getHeight()};
}

/**
 *
 * @param canvas
 * @param color color
 */
private void drawColor(Canvas canvas, int color){
    canvas.drawColor(color, PorterDuff.Mode.SRC);
}

private void drawBitmap(Canvas canvas, Bitmap bitmap){
    int[] surfaceDimensions = getSurfaceDimensions();
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.CYAN);
    paint.setStyle(Paint.Style.FILL);
    paint.setTextSize(100);
    paint.setTextAlign(Paint.Align.CENTER);
    Rect rectE = new Rect(surfaceDimensions[0] / 4, surfaceDimensions[1] / 4,
            3 * surfaceDimensions[0] / 4, 3 * surfaceDimensions[1] / 4);
    canvas.drawBitmap(bitmap, null, rectE, paint);
}

private void drawText(Canvas canvas, String text){
    int[] surfaceDimensions = getSurfaceDimensions();
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.CYAN);
    paint.setStyle(Paint.Style.FILL);
    paint.setTextSize(100);
    paint.setTextAlign(Paint.Align.CENTER);
    canvas.drawText(text, surfaceDimensions[0]/ 2f,
            surfaceDimensions[1] / 2f, paint);
}

private void clearCanvas(Canvas canvas) {
    drawColor(canvas, Color.WHITE);
}

}

2)activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/ll"
    android:padding="10dp"
    android:layout_weight="100">

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:text="Draw All"
        android:textSize="20sp"
        android:textColor="@color/colorAccent"
        android:background="@android:color/transparent"
        android:textAllCaps="false"
        android:id="@+id/b_draw_all"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:text="Clear"
        android:textSize="20sp"
        android:textColor="@color/colorAccent"
        android:background="@android:color/transparent"
        android:textAllCaps="false"
        android:id="@+id/b_clear"/>

</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/ll"
    android:id="@+id/rl">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/ll1"
    android:padding="10dp"
    android:layout_weight="100">

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:text="Draw Color"
        android:textSize="20sp"
        android:textColor="@color/colorAccent"
        android:background="@android:color/transparent"
        android:textAllCaps="false"
        android:id="@+id/b_draw_color"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:text="Draw Bitmap"
        android:textSize="20sp"
        android:textColor="@color/colorAccent"
        android:background="@android:color/transparent"
        android:textAllCaps="false"
        android:id="@+id/b_draw_bitmap"/>

</LinearLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Draw Text"
        android:textSize="20sp"
        android:layout_below="@id/ll1"
        android:textColor="@color/colorAccent"
        android:background="@android:color/transparent"
        android:textAllCaps="false"
        android:id="@+id/b_draw_text"/>

</RelativeLayout>

<SurfaceView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/rl"
    android:id="@+id/sv"/>

</RelativeLayout>

3)结果:

result