🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / Android RMOS / resim çizmek imza atmak Capture SignatureView

1-) Android RMOS - resim çizmek imza atmak Capture SignatureView

 

kaynak : https://stackoverflow.com/questions/7228191/android-signature-capture

 

1-) Android RMOS - CaptureSignatureView.java

 

package com.rambo.imza;

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

import java.io.ByteArrayOutputStream;

public class CaptureSignatureView extends View {

    
private Bitmap _Bitmap;
    
private Canvas _Canvas;
    
private Path _Path;
    
private Paint _BitmapPaint;
    
private Paint _paint;
    
private float _mX;
    
private float _mY;
    
private float TouchTolerance = 4;
    
private float LineThickness = 4;

    
public CaptureSignatureView(Context context, AttributeSet attr) {
        
super(context, attr);
        
_Path = new Path();
        
_BitmapPaint = new Paint(Paint.DITHER_FLAG);
        
_paint = new Paint();
        
_paint.setAntiAlias(true);
        
_paint.setDither(true);
        
_paint.setColor(Color.argb(255, 0, 0, 0));
        
_paint.setStyle(Paint.Style.STROKE);
        
_paint.setStrokeJoin(Paint.Join.ROUND);
        
_paint.setStrokeCap(Paint.Cap.ROUND);
        
_paint.setStrokeWidth(LineThickness);
    }

    
@Override
    
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        
super.onSizeChanged(w, h, oldw, oldh);
        
_Bitmap = Bitmap.createBitmap(w, (h > 0 ? h : ((View) this.getParent()).getHeight()), Bitmap.Config.ARGB_8888);
        
_Canvas = new Canvas(_Bitmap);
    }

    
@Override
    
protected void onDraw(Canvas canvas) {
        
super.onDraw(canvas);
        canvas.drawColor(Color.
WHITE);
        canvas.drawBitmap(
_Bitmap, 0, 0, _BitmapPaint);
        canvas.drawPath(
_Path, _paint);
    }

    
private void TouchStart(float x, float y) {
        
_Path.reset();
        
_Path.moveTo(x, y);
        
_mX = x;
        
_mY = y;
    }

    
private void TouchMove(float x, float y) {
        
float dx = Math.abs(x - _mX);
        
float dy = Math.abs(y - _mY);

        
if (dx >= TouchTolerance || dy >= TouchTolerance) {
            
_Path.quadTo(_mX, _mY, (x + _mX) / 2, (y + _mY) / 2);
            
_mX = x;
            
_mY = y;
        }
    }

    
private void TouchUp() {
        
if (!_Path.isEmpty()) {
            
_Path.lineTo(_mX, _mY);
            
_Canvas.drawPath(_Path, _paint);
        }
else {
            
_Canvas.drawPoint(_mX, _mY, _paint);
        }

        
_Path.reset();
    }

    
@Override
    
public boolean onTouchEvent(MotionEvent e) {
        
super.onTouchEvent(e);
        
float x = e.getX();
        
float y = e.getY();

        
switch (e.getAction()) {
            
case MotionEvent.ACTION_DOWN:
                TouchStart(x, y);
                invalidate();
                
break;
            
case MotionEvent.ACTION_MOVE:
                TouchMove(x, y);
                invalidate();
                
break;
            
case MotionEvent.ACTION_UP:
                TouchUp();
                invalidate();
                
break;
        }

        
return true;
    }

    
public void ClearCanvas() {
        
_Canvas.drawColor(Color.WHITE);
        invalidate();
    }

    
public byte[] getBytes() {
        Bitmap b = getBitmap();

        ByteArrayOutputStream baos =
new ByteArrayOutputStream();
        b.compress(Bitmap.CompressFormat.
PNG, 100, baos);
        
return baos.toByteArray();
    }

    
public Bitmap getBitmap() {
        View v = (View)
this.getParent();
        Bitmap b = Bitmap.
createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c =
new Canvas(b);
        v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
        v.draw(c);

        
return b;
    }
}

 

2-) MainActivity.java

 

package com.rambo.imza;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    
// kaynak : https://stackoverflow.com/questions/7228191/android-signature-capture

    
@Override
    
protected void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        
// cizz
        
LinearLayout mContent = (LinearLayout) findViewById(R.id.cizz);
       
final CaptureSignatureView mSig =new CaptureSignatureView(this, null);
        mContent.addView(mSig, LinearLayout.LayoutParams.
FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        Button btnGonder = (Button) findViewById(R.id.
btnGonder);
        Button btnTemizle = (Button) findViewById(R.id.
btnTemizle);
        btnGonder.setOnClickListener(
new View.OnClickListener() {
            
@Override
            
public void onClick(View view) {
                
byte[] signature = mSig.getBytes();
                
//Bitmap signature = mSig.getBitmap();

                
Toast.makeText(MainActivity.this, "Gönderildi", Toast.LENGTH_SHORT).show();
            }
        });
        btnTemizle.setOnClickListener(
new View.OnClickListener() {
            
@Override
            
public void onClick(View view) {
                
mSig.ClearCanvas();
                Toast.
makeText(MainActivity.this, "Temizlendi", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

 

3-)  activity_main.xml

 

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

    <
LinearLayout
        
android:layout_width="match_parent"
        
android:layout_height="match_parent"
        
android:orientation="vertical"
        
android:weightSum="100">

        <
LinearLayout
            
android:id="@+id/cizz"
            
android:layout_width="match_parent"
            
android:layout_height="match_parent"
            
android:layout_weight="50"
            
android:orientation="horizontal">

        </
LinearLayout>

        <
LinearLayout
            
android:layout_width="match_parent"
            
android:layout_height="match_parent"
            
android:layout_margin="10dp"
            
android:layout_weight="50"
            
android:orientation="horizontal">

            <
Button
                
android:id="@+id/btnTemizle"
                
android:layout_width="wrap_content"
                
android:layout_height="wrap_content"
                
android:layout_marginRight="10dp"
                
android:layout_weight="1"
                
android:background="#CDDC39"
                
android:text="TEMİZLE" />

            <
Button
                
android:id="@+id/btnGonder"
                
android:layout_width="wrap_content"
                
android:layout_height="wrap_content"
                
android:layout_weight="1"
                
android:background="#CDDC39"
                
android:text="GÖNDER" />
        </
LinearLayout>
    </
LinearLayout>

</
RelativeLayout>

 

 2021 Ocak 18 Pazartesi
 422