我正在创建一个用户可以在屏幕上签名的程序。但是,当用户签名时,屏幕上不显示任何内容。我能够访问签名屏幕,因此我不确定我的代码是否正确,因为程序没有崩溃
public class SignaturePadView extends View {
private Path path;
private int x;
private int y;
private Canvas canvas;
private Paint paint = new Paint();
// A "Stroke" is a collection of points drawn by the user as she puts
// her finger down, moves, and then lifts her finger up
private static class Stroke {
private ArrayList<Point> mPoints = new ArrayList<Point>();
}
// A list of all the Strokes the user has drawn with their finger
private ArrayList<Stroke> mStrokes = new ArrayList<SignaturePadView.Stroke>();
public SignaturePadView(Context context) {
super(context);
}
public SignaturePadView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SignaturePadView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Implement this method
//return super.onTouchEvent(event);
super.onTouchEvent(event);
Log.d ("motionEvent", event.toString());
if (event.getAction()==MotionEvent.ACTION_UP){
Log.d("motionEvent", "action_up");
this.x+=(int)event.getX();
this.y+=(int)event.getY();
}
this.postInvalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO implement this
//create path object
super.onDraw(canvas);
// canvas.drawPath(path, paint);
}
}
答案 0 :(得分:0)
尝试下面的捕获签名代码: -
<强>的java 强>
public class CaptureSignature extends Activity {
LinearLayout mContent;
signature mSignature;
Button mClear, mCancel;
Button mGetSign;
public static String tempDir;
public int count = 1;
public String current = null;
View mView;
public static File mypath;
public static Bitmap mBitmap=null;
public static Bitmap checkBitmap;
private String uniqueId;
private EditText yourName;
int check_sign=0;
public static String signaturePth;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.signature);
tempDir = Environment.getExternalStorageDirectory() + "/" + getResources().getString(R.string.external_dir) + "/";
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir(getResources().getString(R.string.external_dir), Context.MODE_PRIVATE);
prepareDirectory();
uniqueId = getTodaysDate() + "_" + getCurrentTime() + "_" + Math.random();
current = uniqueId + ".png";
mypath= new File(directory,current);
mContent = (LinearLayout) findViewById(R.id.linearLayout);
mSignature = new signature(this, null);
mSignature.setBackgroundColor(Color.WHITE);
mContent.addView(mSignature, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
mClear = (Button)findViewById(R.id.clear);
mGetSign = (Button)findViewById(R.id.getsign);
// mGetSign.setEnabled(false);
mCancel = (Button)findViewById(R.id.cancel);
mView = mContent;
yourName = (EditText) findViewById(R.id.yourName);
yourName.setText("sign");
mClear.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
check_sign=0;
mSignature.clear();
// mGetSign.setEnabled(false);
}
});
mGetSign.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if(check_sign==1)
{
boolean error = captureSignature();
if(!error)
{
mView.setDrawingCacheEnabled(true);
mSignature.save(mView);
Bundle b = new Bundle();
GetWaiver.flag=true;
b.putString("status", "done");
Intent intent = new Intent();
intent.putExtras(b);
setResult(RESULT_OK,intent);
finish();
}
}
else if(check_sign==0)
{
Toast.makeText(CaptureSignature.this, "Signature Required..", Toast.LENGTH_LONG).show();
}
}
});
mCancel.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Log.v("log_tag", "Panel Canceled");
Bundle b = new Bundle();
b.putString("status", "cancel");
Intent intent = new Intent();
intent.putExtras(b);
setResult(RESULT_OK,intent);
finish();
}
});
}
@Override
protected void onDestroy()
{
Log.w("GetSignature", "onDestory");
super.onDestroy();
}
private boolean captureSignature() {
boolean error = false;
String errorMessage = "";
if(yourName.getText().toString().equalsIgnoreCase(""))
{
errorMessage = errorMessage + "Please enter your Name\n";
error = true;
}
if(error)
{
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 105, 50);
toast.show();
}
return error;
}
private String getTodaysDate() {
final Calendar c = Calendar.getInstance();
int todaysDate = (c.get(Calendar.YEAR) * 10000) +
((c.get(Calendar.MONTH) + 1) * 100) +
(c.get(Calendar.DAY_OF_MONTH));
Log.w("DATE:",String.valueOf(todaysDate));
return(String.valueOf(todaysDate));
}
private String getCurrentTime() {
final Calendar c = Calendar.getInstance();
int currentTime = (c.get(Calendar.HOUR_OF_DAY) * 10000) +
(c.get(Calendar.MINUTE) * 100) +
(c.get(Calendar.SECOND));
Log.w("TIME:",String.valueOf(currentTime));
return(String.valueOf(currentTime));
}
private boolean prepareDirectory()
{
try
{
if (makedirs())
{
return true;
} else
{
return false;
}
} catch (Exception e)
{
e.printStackTrace();
Toast.makeText(this, "Could not initiate File System.. Is Sdcard mounted properly?", Toast.LENGTH_LONG).show();
return false;
}
}
private boolean makedirs()
{
File tempdir = new File(tempDir);
if (!tempdir.exists())
tempdir.mkdirs();
if (tempdir.isDirectory())
{
File[] files = tempdir.listFiles();
for (File file : files)
{
if (!file.delete())
{
System.out.println("Failed to delete " + file);
}
}
}
return (tempdir.isDirectory());
}
public class signature extends View
{
private static final float STROKE_WIDTH = 5f;
private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;
private Paint paint = new Paint();
private Path path = new Path();
private float lastTouchX;
private float lastTouchY;
private final RectF dirtyRect = new RectF();
public signature(Context context, AttributeSet attrs)
{
super(context, attrs);
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(STROKE_WIDTH);
}
public void save(View v)
{
Log.v("log_tag", "Width: " + v.getWidth());
Log.v("log_tag", "Height: " + v.getHeight());
if(mBitmap == null)
{
mBitmap = Bitmap.createBitmap (mContent.getWidth(), mContent.getHeight(), Bitmap.Config.RGB_565);;
}
Canvas canvas = new Canvas(mBitmap);
try
{
FileOutputStream mFileOutStream = new FileOutputStream(mypath);
v.draw(canvas);
mBitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream);
ByteArrayOutputStream bao1 = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 95, bao1);
byte[] ba11 = bao1.toByteArray();
String ba111 = Base64.encodeToString(ba11, 0);
ClientDetail.m=ba111;
mFileOutStream.flush();
mFileOutStream.close();
}
catch(Exception e)
{
Log.v("log_tag", e.toString());
}
}
public void clear()
{
path.reset();
invalidate();
}
@Override
protected void onDraw(Canvas canvas)
{
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
float eventX = event.getX();
float eventY = event.getY();
check_sign=1;
// mGetSign.setEnabled(true);
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
lastTouchX = eventX;
lastTouchY = eventY;
return true;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
resetDirtyRect(eventX, eventY);
int historySize = event.getHistorySize();
for (int i = 0; i < historySize; i++)
{
float historicalX = event.getHistoricalX(i);
float historicalY = event.getHistoricalY(i);
expandDirtyRect(historicalX, historicalY);
path.lineTo(historicalX, historicalY);
}
path.lineTo(eventX, eventY);
break;
default:
debug("Ignored touch event: " + event.toString());
return false;
}
invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH),
(int) (dirtyRect.top - HALF_STROKE_WIDTH),
(int) (dirtyRect.right + HALF_STROKE_WIDTH),
(int) (dirtyRect.bottom + HALF_STROKE_WIDTH));
lastTouchX = eventX;
lastTouchY = eventY;
return true;
}
private void debug(String string){
}
private void expandDirtyRect(float historicalX, float historicalY)
{
if (historicalX < dirtyRect.left)
{
dirtyRect.left = historicalX;
}
else if (historicalX > dirtyRect.right)
{
dirtyRect.right = historicalX;
}
if (historicalY < dirtyRect.top)
{
dirtyRect.top = historicalY;
}
else if (historicalY > dirtyRect.bottom)
{
dirtyRect.bottom = historicalY;
}
}
private void resetDirtyRect(float eventX, float eventY)
{
dirtyRect.left = Math.min(lastTouchX, eventX);
dirtyRect.right = Math.max(lastTouchX, eventX);
dirtyRect.top = Math.min(lastTouchY, eventY);
dirtyRect.bottom = Math.max(lastTouchY, eventY);
}
}
}
<强> XML 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/dashboard_bg"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/cancel"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="3dp"
android:layout_weight=".30"
android:background="@drawable/btn_click"
android:text="@string/cancel"
android:textColor="@color/white" />
<Button
android:id="@+id/clear"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="3dp"
android:layout_weight=".35"
android:background="@drawable/btn_click"
android:text="@string/clear"
android:textColor="@color/white" />
<Button
android:id="@+id/getsign"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="3dp"
android:layout_weight=".35"
android:background="@drawable/btn_click"
android:text="@string/save"
android:textColor="@color/white" />
</LinearLayout>
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingLeft="10sp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/yourName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLength="30" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLength="30"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLength="30"
android:paddingLeft="15dp"
android:text="Please Sign below ..."
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
</TableRow>
</TableLayout>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:background="@android:color/transparent"
android:layout_height="match_parent" />
</LinearLayout>