MWhy is my bitmaps not drawn on the canvas?

0

I am trying to make an app where the user selects an image from the device's gallery and then can draw rectangles on the selected image, I can already navigate in the gallery and get the image then I convert it into bitmap and with the Canvas class the I try to draw and then lock in it but I can't draw anything here I leave the Canvas and MainActivity class to see if anyone can help me, thank you very much in advance

`

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.FileNotFoundException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

ImageView galeria;
Button btn;
private static final int PICK_IMAGE = 100;
Uri imageUri;
private static Lienzo lienzo;

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


    lienzo=(Lienzo)findViewById(R.id.view3);
    btn=(Button)findViewById(R.id.button);

    btn.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    openGallery();
}

private void openGallery() {
    Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery, PICK_IMAGE);

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
        imageUri = data.getData();
        try {
            Bitmap bmp= BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
            lienzo.NuevoDibujo(bmp);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        //galeria.setImageURI(imageUri);
    }
}
}`

`

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

public class Lienzo extends View {

//Path que utilizaré para ir pintando las lineas
private Path drawPath;
//Paint de dibujar y Paint de Canvas
private static Paint drawPaint;
private Paint canvasPaint;
//Color Inicial
private static int paintColor = 0xFFFF0000;
//canvas
private Canvas drawCanvas;
//canvas para guardar
private Bitmap canvasBitmap;


public Lienzo(Context context, AttributeSet attrs) {
    super(context, attrs);
}

private void setupDrawing(){

    drawPath = new Path();
    drawPaint = new Paint();
    drawPaint.setColor(paintColor);
    drawPaint.setAntiAlias(true);
    drawPaint.setStrokeWidth(20);
    drawPaint.setStyle(Paint.Style.STROKE);
    drawPaint.setStrokeJoin(Paint.Join.ROUND);
    drawPaint.setStrokeCap(Paint.Cap.ROUND);
    canvasPaint = new Paint(Paint.DITHER_FLAG);


}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    canvasBitmap = Bitmap.createBitmap(w, h/2, Bitmap.Config.ARGB_8888);
    drawCanvas = new Canvas(canvasBitmap);
}

public void NuevoDibujo(Bitmap bitmap){
    drawCanvas.drawBitmap(bitmap,0,0,drawPaint);

    invalidate();

}
}

`

java
android
android-studio
canvas
asked on Stack Overflow Nov 13, 2019 by Manuel Hurtado

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0