Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

android - 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);
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  1. When detecting touch event, at ACTION_DOWN, draw a transparent VIEW on top of parent view, make it Canvas and draw as long as ACTION_MOVE is under processing.

  2. 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...