how to add a line as a gesture while moving from one image to another?
I am making my own Pattern Lock for android phone, i have Done the coding as when i click on an image it stores an integer in an array and when the user re-enters the same password it matches both the arrays and Open the lock accordingly, my code is working fine But Now i have to add gesture in the form of a line while going from one image to another (as in pattern lock) also i want to store the integers in the array when i touch an image instead of clicking it...
guide me how to do this below is my sample code for image click events
public void Image1(View view) {
// Toast.makeText(this, "You clicked Image 1!",
// Toast.LENGTH_SHORT).show();
myArray[0] = 1;
// builder.append("" + myArray[0] + " ");
// Toast.makeText(this, myArray, Toast.LENGTH_LONG).show();
ImageView kk = (ImageView) view;
Drawable d = getResources().getDrawable(R.drawable.unlock);
kk.setImageDrawable(d);
}
public void Image2(View view) {
// Toast.makeText(this, "You clicked Image 2!",
// Toast.LENGTH_SHORT).show();
myArray[1] = 2;
ImageView kk = (ImageView) view;
Drawable d = getResources().getDrawable(R.drawable.unlock);
kk.setImageDrawable(d);
}
public void Image3(View view) {
// Toast.makeText(this, "You clicked Image 3!",
// Toast.LENGTH_SHORT).show();
myArray[2] = 3;
ImageView kk = (ImageView) view;
Drawable d = getResources().getDrawable(R.drawable.unlock);
kk.setImageDrawable(d);
}
public void Image4(View view) {
// Toast.makeText(this, "You clicked Image 4!",
// Toast.LENGTH_SHORT).show();
开发者_高级运维 myArray[3] = 4;
ImageView kk = (ImageView) view;
Drawable d = getResources().getDrawable(R.drawable.unlock);
kk.setImageDrawable(d);
}
public void Image5(View view) {
// Toast.makeText(this, "You clicked Image 5!",
// Toast.LENGTH_SHORT).show();
myArray[4] = 5;
ImageView kk = (ImageView) view;
Drawable d = getResources().getDrawable(R.drawable.unlock);
kk.setImageDrawable(d);
}
public void Image6(View view) {
// Toast.makeText(this, "You clicked Image 6!",
// Toast.LENGTH_SHORT).show();
myArray[5] = 6;
ImageView kk = (ImageView) view;
Drawable d = getResources().getDrawable(R.drawable.unlock);
kk.setImageDrawable(d);
}
public void Image7(View view) {
// Toast.makeText(this, "You clicked Image 7!",
// Toast.LENGTH_SHORT).show();
myArray[6] = 7;
ImageView kk = (ImageView) view;
Drawable d = getResources().getDrawable(R.drawable.unlock);
kk.setImageDrawable(d);
}
public void Image8(View view) {
// Toast.makeText(this, "You clicked Image 8!",
// Toast.LENGTH_SHORT).show();
myArray[7] = 8;
ImageView kk = (ImageView) view;
Drawable d = getResources().getDrawable(R.drawable.unlock);
kk.setImageDrawable(d);
}
public void Image9(View view) {
// Toast.makeText(this, "You clicked Image 9!",
// Toast.LENGTH_SHORT).show();
myArray[8] = 9;
ImageView kk = (ImageView) view;
Drawable d = getResources().getDrawable(R.drawable.unlock);
kk.setImageDrawable(d);
}
Implement onTouchEvent(MotionEvent ev)
, with ACTION_DOWN, ACTION_MOVE, ACTION_UP
. As your finger moving, draw a line from previous Coordinate to current Coordinate.
Get coordinate by using ev.getX()
ev.getY()
I've just thought of two solution currently:
- When detecting touch event, at
ACTION_DOWN
, draw a transparent VIEW on top of parent view, make it Canvas and draw as long asACTION_MOVE
is under processing.
- Use
SurfaceView
instead of regular View. A sample on SurfaceView to draw: http://www.droidnova.com/playing-with-graphics-in-android-part-ii,160.html
精彩评论