问题描述
我正在为 android 手机制作自己的 Pattern Lock,我已经完成了编码,因为当我单击图像时,它将整数存储在数组中,当用户重新输入相同的密码时,它与数组和 Open 匹配相应地锁定,我的代码工作正常但是现在我必须以线的形式添加手势,同时从一个图像到另一个图像(如模式锁定)我还想在触摸图像时将整数存储在数组中而不是点击它...
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);
}
推荐答案
使用 ACTION_DOWN, ACTION_MOVE, ACTION_UP
实现 onTouchEvent(MotionEvent ev)
.当您的手指移动时,从前一个坐标到当前坐标画一条线.
使用 ev.getX()
ev.getY()
获取坐标目前我刚刚想到了两种解决方案:
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:
- 检测到触摸事件时,在
ACTION_DOWN
处,在父view之上绘制一个透明的VIEW,做成Canvas,只要ACTION_MOVE
在处理中就可以绘制.
- 使用
SurfaceView
代替常规视图.SurfaceView 上要绘制的示例:http://www.droidnova.com/playing-with-graphics-in-android-part-ii,160.html
- 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
这篇关于如何在从一个图像移动到另一个图像时添加一条线作为手势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!