【Androidプログラム開発】図案解錠(3)-完全demo

15683 ワード

  • これは私が実際に書いた最初の完全なAndroidプロジェクトでしょう.本当にAndroidプログラム開発の面白さを感じました.授業中に先生と一緒に書いて、一つのモジュールと一つのモジュールの理解を書いて、書き終わってから自分がそんなに長いdemoを書いたことに気づいて、とても満足しています.

  • activity_main.xml
    
    
        
        
        
        
        
        
    
    

    MainAcitvity.java
    public class MainActivity extends AppCompatActivity{
        //               
        ArrayList dotsList;
        ArrayList lineTagsList;
        ArrayList selectedList;
        int tag;
        //             
        ImageView lastSelectedDot;
        //       
        StringBuilder password;
        //      
        String orgPassword;
        //          
        String firstPassword;
        //       
        TextView alertTextView;
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            //        
            if (hasFocus){
                //    
                RelativeLayout rl = findViewById(R.id.root_layout);
                //      
                ImageView iv = findViewById(R.id.opView);
                //  x   y  
                int x = iv.getLeft();
                int y = iv.getTop();
                //      
                float scale = getResources().getDisplayMetrics().density;
                //     6 
                //12 23
                //45 56
                //78 89
                tag = 12;
                for (int i = 0; i < 3; i++) {
                    for (int j = 0; j < 2; j++) {
                        //           
                        ImageView lineView = new ImageView(this);
                        lineView.setBackgroundResource(R.drawable.normal_highlight1);
                        lineView.setVisibility(View.INVISIBLE);
                        lineView.setTag(tag);
                        lineTagsList.add(tag);//    tag 
                        tag += 11; //     11
                        //      
                        RelativeLayout.LayoutParams params = new
                                RelativeLayout.LayoutParams(
                                ViewGroup.LayoutParams.WRAP_CONTENT,
                                ViewGroup.LayoutParams.WRAP_CONTENT);
                        params.leftMargin = (int)(x + 46.6*scale) + (int)(99*scale*j);
                        params.topMargin = (int)(y + 170*scale) + (int)(99*scale*i);
                        rl.addView(lineView, params);
                    }
                    //      22
                    tag += 11;
                }
                //     4 
                //14 25 36
                //47 58 69
                tag = 14;
                for (int i = 0; i < 2; i++) {
                    for (int j = 0; j < 3; j++) {
                        //           
                        ImageView lineView = new ImageView(this);
                        lineView.setBackgroundResource(R.drawable.normal_highlight2);
                        lineView.setVisibility(View.INVISIBLE);
                        lineView.setTag(tag);
                        lineTagsList.add(tag);//    tag 
                        tag += 11;
                        //      
                        RelativeLayout.LayoutParams params = new
                                RelativeLayout.LayoutParams(
                                ViewGroup.LayoutParams.WRAP_CONTENT,
                                ViewGroup.LayoutParams.WRAP_CONTENT);
                        params.leftMargin = (int)(x + 42*scale) + (int)(99*scale*j);
                        params.topMargin = (int)(y + 170*scale) + (int)(99*scale*i);
                        rl.addView(lineView, params);
                    }
                }
                //    
                //  
                // 24 35
                // 57 68
                //   
                // 15 26
                // 48 59
                tag = 24;
                int rTag = 15;
                for (int i = 0; i < 2; i++) {
                    for (int j = 0; j < 2; j++) {
                        //           
                        ImageView rLineView = new ImageView(this);
                        rLineView.setTag(rTag);
                        lineTagsList.add(rTag);//    tag 
                        rTag += 11;
                        //    
                        rLineView.setBackgroundResource(R.drawable.normal_highlight3);
                        //      
                        rLineView.setVisibility(View.INVISIBLE);
                        RelativeLayout.LayoutParams params = new
                                RelativeLayout.LayoutParams(
                                ViewGroup.LayoutParams.WRAP_CONTENT,
                                ViewGroup.LayoutParams.WRAP_CONTENT);
                        params.leftMargin = (int)(x + 42*scale) + (int)(99*scale*j);
                        params.topMargin = (int)(y + 170*scale) + (int)(99*scale*i);
                        rl.addView(rLineView, params);
                        ImageView lLineView = new ImageView(this);
                        lLineView.setTag(tag);
                        lineTagsList.add(tag);//    tag 
                        tag += 11;
                        lLineView.setVisibility(View.INVISIBLE);
                        lLineView.setBackgroundResource(R.drawable.normal_highlight4);
                        params.leftMargin = (int)(x + 53.3*scale) + (int)(99*scale*j);
                        params.topMargin = (int)(y + 170*scale) + (int)(99*scale*i);
                        rl.addView(lLineView,params);
                    }
                    tag += 11;
                    rTag += 11;
                }
                //  9  
                tag = 1;
                for (int i = 0; i < 3; i++) {
                    for (int j = 0; j < 3; j++) {
                        //          
                        ImageView dotView = new ImageView(this);;
                        //     tag 
                        dotView.setTag(tag);
                        tag++;
                        //    
                        dotView.setVisibility(View.INVISIBLE);
                        //       
                        dotView.setBackgroundResource(R.drawable.selected_dot);
                        //       
                        RelativeLayout.LayoutParams params = new
                                RelativeLayout.LayoutParams(
                                ViewGroup.LayoutParams.WRAP_CONTENT,
                                ViewGroup.LayoutParams.WRAP_CONTENT);
                        params.leftMargin = (int)(x + 35.33*scale) + (int)(98.66*scale*j);
                        params.topMargin = (int)(y + 162*scale) + (int)(98.66*scale*i);
                        //         
                        rl.addView(dotView, params);
                        //          
                        dotsList.add(dotView);
                    }
                }
            }
        }
        //      
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            //       
            int action = event.getAction();
            ImageView selected;
            float x;
            float y;
            //       
            switch (action){
                case MotionEvent.ACTION_DOWN:
                    //  
                    //        
                    x = event.getX();
                    y = event.getY();
                    //  x y           
                    selected = dotOfTouch(x, y);
                    if (selected != null) {
                        //  
                        selected.setVisibility(View.VISIBLE);
                        //       
                        lastSelectedDot = selected;
                        // tag       
                        password.append(selected.getTag());
                        //           
                        selectedList.add(selected);
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    //  
                    //        
                    x = event.getX();
                    y = event.getY();
                    //  x y           
                    selected = dotOfTouch(x, y);
                    if (selected != null) {
                        //            
                        if (lastSelectedDot == null){
                            //    
                            selected.setVisibility(View.VISIBLE);
                            //  
                            lastSelectedDot = selected;
                            // tag       
                            password.append(selected.getTag());
                            //           
                            selectedList.add(selected);
                        } else{
                            //      
                            //           tag
                            int lTag = (Integer) lastSelectedDot.getTag();
                            int cTag = (Integer) selected.getTag();
                            //      tag  small * 10 + big
                            int lineTag = lTag > cTag ? cTag*10+lTag: lTag*10+cTag;
                            //         
                            if (lineTagsList.contains(lineTag)){
                                //   
                                //   
                                selected.setVisibility(View.VISIBLE);
                                // tag       
                                password.append(selected.getTag());
                                //     
                                //      
                                RelativeLayout rl = findViewById(R.id.root_layout);
                                //  tag     
                                ImageView iv = rl.findViewWithTag(lineTag);
                                //   
                                iv.setVisibility(View.VISIBLE);
                                //     
                                lastSelectedDot = selected;
                                //           
                                selectedList.add(selected);
                                //           
                                selectedList.add(iv);
                            }
                        }
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    //  
                    // 1.            
                    // 2.        
                    // 3.        
                    if (orgPassword != null){
                        //    
                        if (password.toString().equals(orgPassword)){
                            alertTextView.setText("      ");
                        } else{
                            alertTextView.setText("      ");
                        }
                    } else{
                        //    
                        //               
                        if (firstPassword == null){
                            //        
                            firstPassword = password.toString();
                            //      
                            alertTextView.setText("       ");
                        } else{
                            //       
                            //        
                            if (firstPassword.equals(password.toString())){
                                //    
                                alertTextView.setText("      ");
                                //    
                                SharedPreferences sp = getSharedPreferences("password",0);
                                SharedPreferences.Editor editor = sp.edit();
                                editor.putString("pwd",firstPassword);
                                editor.commit();
                            } else{
                                //    
                                alertTextView.setText("             ");
                                firstPassword = null;
                            }
                        }
                    }
                    clean();
                    break;
                default:
                    break;
            }
            return true;
        }
        //  
        public void clean(){
            password.setLength(0);
            //             
            for (ImageView iv:selectedList){
                iv.setVisibility(View.INVISIBLE);
            }
            //    
            selectedList.clear();
        }
        //                       
        public ImageView dotOfTouch(float x, float y){
            //             
     /*
     1.                
     Point p = new Point();
     getWindowManager().getDefaultDisplay().getSize(p);
     //         
     RelativeLayout rl = findViewById(R.id.root_layout);
     //        
     float padding = p.y - rl.getHeight();
     */
     /*
     //2.           
     ImageView firt = dotsList.get(0);
     int[] loc = new int[2];
     firt.getLocationOnScreen(loc);
     System.out.println("    y:"+loc[1]);
     System.out.println("    y:"+firt.getY());
     System.out.println("    :"+firt.getWidth());
     */
            //    
            for (ImageView dot:dotsList){
                //    dot      x y
                int[] loc = new int[2];
                dot.getLocationOnScreen(loc);
                int dx = loc[0];
                int dy = loc[1];
                //        
                int r = dx + dot.getWidth();
                //         
                int b = dy + dot.getHeight();
                //             
                if ((x <= r && x >= dx) &&
                        (y <= b && y >= dy)){
                    return dot;
                }
            }
            return null;
        }
    /**
     *                window  /  
     * window -> viewGroup ->    
     *    onCreate、onStart、onResume            
     *
     *                 
     *            
     */
        /**
         * Android    4 
         * 1. sharedPreference     
         * 2. file
         * 3. sqlite3
         * 4. network
         */
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //           
            dotsList = new ArrayList<>();
            lineTagsList = new ArrayList<>();
            password = new StringBuilder();
            selectedList = new ArrayList<>();
            //  xml     
            alertTextView = findViewById(R.id.tv_alert);
            //                  
            //key-value Map
            //name:     
            //mode:  
            //   
     /*
     SharedPreferences sp = getSharedPreferences("abc",MODE_PRIVATE);
     String result = sp.getString("pwd",null);
     System.out.println(result);
     */
     /*
     //             Editor  
     SharedPreferences.Editor editor = sp.edit();
     editor.putString("pwd", "123");
     //  
     editor.commit(); //    
     //editor.apply(); //                 
     */
            //                pwd
            SharedPreferences sp =
                    getSharedPreferences("password",MODE_PRIVATE);
            //  pwd    
            orgPassword = sp.getString("pwd",null);
            if (orgPassword == null){
                alertTextView.setText("       ");
            }else{
                alertTextView.setText("       ");
            }
        }
        @Override
        protected void onResume() {
            super.onResume();
            Point p = new Point();
            getWindowManager().getDefaultDisplay().getSize(p);
            float w = p.x;
            float h = p.y;
            if (w > h){
                System.out.println("  ");
            } else{
                System.out.println("  ");
            }
        }
    }
    

    実行結果:
    20190829_152101.gif