0

我在以下情况下遇到问题,我需要在 PNG 或 JPG 格式的图像表单上签名,此功能属于 Android 系统的移动应用程序。状态是下面这种情况,我可以把签名放在用户点击的地方。但是我无法根据给定的缩放量按比例调整签名位图的大小。您需要将签名位图放置在用户单击的位置,但指的是图像而不是屏幕。

我在互联网上浏览了很多主题,其中很多都在 StackOverflow 中,但我没有找到任何可以让我知道如何进行此类活动的主题。我已经多次到达谷歌的第三页。

任何人都可以帮助我,或者你能给我一些关于如何做到这一点的见解吗?

问题草图

我的代码

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;
    }
}

}
4

0 回答 0