Soru & Cevap

Parmağımı kaldırdığımda çizimim kayboluyor ...

17.05.2020 - 11:20

Merhaba arkadaşlar, tuval üzerine kendi kişisel fırçam(bitmap) ile çizim yapmak istedim paint kullanarak, şöyle bir problem oldu ama, çizim yaparken yani move ederken gayet iyi çalışıyor, ama parmağımı kaldırdığımda tüm çizimim bir anda yok oluyor, şöyle ki paint ile beyaz bir çizgi çiziyorum mesela problem olmuyor ama kişisel kalem(bitmap) ile bir çizgi çiziyorum parmağımı kaldırdığımda kayboluyor ama beyaz çizgi kaybolmuyor sadece bitmap kayboluyor, aşağıda linkini verdiğim sitenin draw kısmına kullandığım kodları yazıyorum, sebebi ne olabilir, yardımcı olabilirseniz sevinirim.Teşekkürler.

 

Kullandığım kod:

Bitmap mBitmap = getScaledBitmap();

    // find the center of the bitmap.
    centerX = mBitmap.getWidth() / 2;
    centerY = mBitmap.getHeight() / 2;

    // wrap the path with a measurement tool for paths - PathMeasure
    pathMeasure = new PathMeasure(Path, false);

    // initialize the distance to the center of the bitmap.
    distance = mBitmap.getWidth() / 2;

    // initialize position and slope buffers.
    position = new float[2];
    slope = new float[2];

    // draw so long as the distance traveled on the path isn't longer than
    // the total distance of the path.
    while (distance < pathMeasure.getLength())
    {
        // grab the position & slope (tangent) on a particular distance along the path.
        pathMeasure.getPosTan(distance, position, slope);

        // convert the vector to a degree.
        slopeDegree = (float)((Math.atan2(slope[1], slope[0]) * 180f) / Math.PI);

        // preserve the current state of the canvas
        canvas.save();

        // translate the canvas to the position on the path.
        canvas.translate(position[0] - centerX, position[1] - centerY);

        // rotate the canvas around the center of the bitmap the amount of degrees needed.
        canvas.rotate(slopeDegree, centerX, centerY);

        // draw the bitmap
        canvas.drawBitmap(mBitmap, 0, 0,DrawPaint);

        // revert the bitmap to the previous state
        canvas.restore();

        // increase the distance by the bitmap's width + the desired margin.
        distance += mBitmap.getWidth() + 10;
    }

private Bitmap getScaledBitmap()
{
    Bitmap mBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.heart);
    // no bitmap or resId, return null (no special handing of this! add if you like).
    if (mBitmap == null)
        return null;

    // width / height of the bitmap[
    float width = mBitmap.getWidth();
    float height = mBitmap.getHeight();

    // ratio of the bitmap
    float ratio = width / height;

    // set the height of the bitmap to the width of the path (from the paint object).
    float scaledHeight = DrawPaint.getStrokeWidth();

    // to maintain aspect ratio of the bitmap, use the height * ratio for the width.
    float scaledWidth = scaledHeight * ratio;

    // return the generated bitmap, scaled to the correct size.
    return Bitmap.createScaledBitmap(mBitmap, (int)scaledWidth, (int)scaledHeight, true);
}

Bu sitedeki kodları kullanarak çizimimi yapıyorum:https://github.com/burhanrashid52/PhotoEditor/blob/master/photoeditor/src/main/java/ja/burhanrashid52/photoeditor/BrushDrawingView.java

Bu kısma yazıyorum yukarıdaki kodları, aynı kodları touch up kısmına yazdım ancak değişen birşey olmadı:

@Override
protected void onDraw(Canvas canvas) {
    for (LinePath linePath : mDrawnPaths) {
        canvas.drawPath(linePath.getDrawPath(), linePath.getDrawPaint());
    }
    canvas.drawPath(mPath, mDrawPaint);

    //kodları buraya yazıyorum
}

 

11 Görüntülenme

1 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

Profile picture for user f.erenyucal
f.erenyucal
16.02.2023 - 04:53

Merhaba, sorunuzda bahsettiğiniz gibi, parmağınızı kaldırdığınızda çizimlerinizin kaybolması, kodlarınızda şu anda yer alan çizim işlemlerinin onDraw metodunda gerçekleştiğini gösteriyor. Ancak, onDraw metodunun amacı, önceki çizimlerin yeniden çizilmesi ve kullanıcı tarafından oluşturulan yeni çizimlerin eklenmesidir. Yani, herhangi bir kullanıcı girişi olmadan, bu metoda yapılan herhangi bir çağrı, önceki çizimlerin yeniden çizilmesine ve dolayısıyla tüm çizimlerin silinmesine neden olacaktır

Bu nedenle, kullanıcının dokunma hareketleri sırasında yapılan çizimlerin saklanması ve kullanıcının işlemi tamamlamasından sonra, çizimlerin onDraw metoduna eklenmesi gerekiyor. Bu işlem için, kullanıcının dokunma hareketlerini takip etmek için onTouchEvent metodunu kullanabilirsiniz

Ayrıca, çizimleri tutmak için bir Path nesnesi oluşturmanız gerekiyor. Kullanıcının dokunma hareketleri sırasında, her bir dokunuşa karşılık gelen bir Path nesnesi oluşturabilirsiniz. Dokunma hareketleri sırasında oluşturulan Path nesnelerini bir diziye ekleyebilir ve bu diziyi onDraw metodunda döngü ile çizebilirsiniz

Aşağıdaki kod örneği, kullanıcının dokunma hareketleri sırasında oluşturulan çizimleri onDraw metodunda çizmenize yardımcı olabilir:

 

public class MyView extends View {
    private Path mPath;
    private Paint mPaint;
    private ArrayList<Path> mPaths = new ArrayList<Path>();

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for (Path path : mPaths) {
            canvas.drawPath(path, mPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPath = new Path();
                mPath.moveTo(event.getX(), event.getY());
                break;
            case MotionEvent.ACTION_MOVE:
                mPath.lineTo(event.getX(), event.getY());
                break;
            case MotionEvent.ACTION_UP:
                mPaths.add(mPath);
                break;
        }

        invalidate();
        return true;
    }
}