I'm having problems in the following context, I need to make signatures on a form that is an image in PNG or JPG, this functionality belongs to a mobile application with Android system. The status is in the following situation, I can place the signature in the place where the user clicked. But I'm having trouble resizing the signature bitmap proportionally to the amount of zoom that was given. You need to position the signature Bitmap at the point the user clicked on, but referring to the image and not the screen.
I have browsed a lot of topics on the internet, many here in StackOverflow, but I do not find any that gives me any idea how I can do such an activity. I have already arrived several times on the third page of Google.
Can anyone help me, or can you give me some insight into how I can do this?
My code
public class SignatureActivity extends Activity implements OnClickListener {
private LinearLayout mContent;
private Button mClear, mSave, mCancel;
private View view;
private Dialog dialog;
private Signature mSignature;
private TouchImageView img;
private float xPosition;
private float yPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_signature);
img = (TouchImageView) findViewById(R.id.iv_form);
img.setImageResource(R.drawable.imp_009_tiss_1200x859);
img.setOnClickListener(this);
// Dialog Function
dialog = new Dialog(SignatureActivity.this);
// Removing the features of Normal Dialogs
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_signature);
dialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mSignature = (Signature) dialog.getWindow().findViewById(R.id.signatureView1);
dialog.setCancelable(true);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_form:
dialog_action();
break;
default:
break;
}
}
public void carregaComponentes() {
mContent = (LinearLayout) dialog.findViewById(R.id.ll_area_assinatura);
mClear = (Button) dialog.findViewById(R.id.clear);
mSave = (Button) dialog.findViewById(R.id.save);
mCancel = (Button) dialog.findViewById(R.id.cancel);
mSignature = (Signature) dialog.findViewById(R.id.signatureView1);
}
public void dialog_action() {
carregaComponentes();
mSignature.setBackgroundColor(Color.WHITE);
mSignature.setSigColor(255, 0, 0, 255);
view = mContent;
mClear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v("log_tag", "Panel Cleared");
mSignature.clearSignature();
}
});
mSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v("log_tag", "Panel Saved");
view.setDrawingCacheEnabled(true);
if (saveSignature(view)) {
dialog.dismiss();
} else {
mSignature.clearSignature();
}
}
});
mCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v("log_tag", "Panel Canceled");
dialog.dismiss();
mSignature.clearSignature();
}
});
dialog.show();
}
public boolean saveSignature(View view) {
DisplayMetrics dm = new DisplayMetrics();
float dpi = dm.scaledDensity;
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenW = dm.widthPixels;
int screenH = dm.heightPixels;
Bitmap assinatura = mSignature.getImage();
Bitmap assinaturaRedimensionada = Bitmap.createScaledBitmap(
assinatura,
(Integer.valueOf((int) (screenW * 0.20))),
(Integer.valueOf((int) (screenH * 0.070))),
false);
img.buildDrawingCache();
Bitmap imgGto = img.getDrawingCache();
Bitmap gtoAssinada = Bitmap.createBitmap(imgGto.getWidth(), imgGto.getHeight(), imgGto.getConfig());
Canvas canvas = new Canvas(gtoAssinada);
canvas.drawBitmap(imgGto, new Matrix(), null);
canvas.drawBitmap(assinaturaRedimensionada, TouchImageViewListener.x, TouchImageViewListener.y, null);
// Creating Separate Directory for saving Generated Images
File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String pic_name = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String StoredPath = "GTO_assinada" + pic_name + ".png";
File fichero = new File(sd, StoredPath);
try {
if (sd.canWrite()) {
fichero.createNewFile();
OutputStream os = new FileOutputStream(fichero);
gtoAssinada.compress(Bitmap.CompressFormat.PNG, 90, os);
os.close();
img.setImageBitmap(gtoAssinada);
mSignature.clearSignature();
Toast.makeText(getApplicationContext(), "GTO Assinada com Sucesso!", Toast.LENGTH_LONG).show();
}
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}