实例介绍
【实例简介】
【实例截图】
【核心代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 | package com.zl.moveimage; import java.util.Random; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.View.OnClickListener; import android.widget.AbsoluteLayout; import android.widget.AbsoluteLayout.LayoutParams; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import android.widget.ImageView.ScaleType; @SuppressWarnings ( "deprecation" ) public class SimplePuzzle extends Activity { private static final int MENU_ABOUT = Menu.FIRST; private static final int MENU_EXIT = Menu.FIRST 1 ; // 存储图片序列的int数组,最后一位为图片标记,用于换图片时的判断 // 默认图片 int [] queue = { R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7, R.drawable.p8, R.drawable.p9, 1 }; // 图片p系列 int [] queue1 = { R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7, R.drawable.p8, R.drawable.p9, 1 }; // 图片a系列 int [] queue2 = { R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7, R.drawable.a8, R.drawable.a9, 2 }; // ImageView 图片显示区域,按九宫格分为九个区域 private ImageView iv_1, iv_2, iv_3, iv_4, iv_5, iv_6, iv_7, iv_8, iv_9; // 图片的位置参数对象, // 通过Drawable对象的getLayoutParams()方法取得,可得到图片的位置信息 // 通过设置LayoutParams方法的x,y坐标,并传给图片,可以改变图片的位置,如: // setLayoutParams(paramsIv_1) private AbsoluteLayout.LayoutParams paramsIv_1, paramsIv_2, paramsIv_3, paramsIv_4, paramsIv_5, paramsIv_6, paramsIv_7, paramsIv_8, paramsIv_9; // 记录图片的长宽,与左上角一点的坐标 private int picWidth, picHeight, picX1, picY1, picX2, picY2, picX3, picY3, picX4, picY4, picX5, picY5, picX6, picY6, picX7, picY7, picX8, picY8, picX9, picY9; // 在onTouchEvent方法中设置,标记是否击中图片 private boolean clickPic1, clickPic2, clickPic3, clickPic4, clickPic5, clickPic6, clickPic7, clickPic8, clickPic9; // 图片锁,每次只能对一个图片进行操作。flag为真时表明已经ACTION_DOWN一张图片 private boolean flag = false ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); // 全屏显示 requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 加载layout setContentView(R.layout.main); // 按照queue的顺序显示图片 findImageView(queue); //按钮 Button button01 = (Button) findViewById(R.id.button01); Button button02 = (Button) findViewById(R.id.button02); button01.setOnClickListener(button01Listener); button02.setOnClickListener(button02Listener); } /** * 创建菜单选项 */ @Override public boolean onCreateOptionsMenu(Menu menu) { super .onCreateOptionsMenu(menu); //关于选项 menu.add( 0 , MENU_ABOUT, 0 , R.string.menu_about); //退出选项 menu.add( 0 , MENU_EXIT, 0 , R.string.menu_exit); return true ; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_ABOUT: Toast .makeText( this , Toast.LENGTH_SHORT).show(); break ; case MENU_EXIT: finish(); } return true ; } /** * 打乱数组顺序 * * @param intArray * @return 打乱后的int数组 */ public static int [] shuffle( int [] intArray) { Random ran = new Random(); int num; for ( int i = 0 ; i < 9 ; i ) { for ( int j = 0 ; j < 9 ; j ) { num = ran.nextInt( 9 ); swap(intArray, j, num); } } return intArray; } /** * 用于交换intArray中下标分别为index1和index2的值 */ public static void swap( int [] intArray, int index1, int index2) { int temp = 0 ; temp = intArray[index1]; intArray[index1] = intArray[index2]; intArray[index2] = temp; } /** * 打乱数组 */ private OnClickListener button01Listener = new OnClickListener() { public void onClick(View v) { queue = shuffle(queue); findImageView(queue); } }; /** * 换图片 */ private OnClickListener button02Listener = new OnClickListener() { public void onClick(View v) { //判断数组最后一位的标记,只有两幅图,随便做一个简单判断 if (queue[ 9 ] == 1 ) //数组复制,如果用queue==queue2的话,会让queue指向queue2的地址, //修改queue的同时也会修改queue2 System.arraycopy(queue2, 0 , queue, 0 , queue2.length); else System.arraycopy(queue1, 0 , queue, 0 , queue1.length); findImageView(queue); } }; /** * 按照int数组queue的顺序显示图片 * * @param queue */ @SuppressWarnings ( "deprecation" ) public void findImageView( int [] queue) { //每幅分图的大小 picHeight = 106 ; picWidth = 70 ; Drawable pic1 = getResources().getDrawable(queue[ 0 ]); iv_1 = (ImageView) this .findViewById(R.id.ImageView01); iv_1.setImageDrawable(pic1); // CENTER_INSIDE 将图片的内容完整居中显示,通过按比例缩小原来的size使得图片长/宽等于或小于View的长/宽 iv_1.setScaleType(ScaleType.CENTER_INSIDE); paramsIv_1 = (LayoutParams) iv_1.getLayoutParams(); picX1 = paramsIv_1.x; picY1 = paramsIv_1.y; Drawable pic2 = getResources().getDrawable(queue[ 1 ]); iv_2 = (ImageView) this .findViewById(R.id.ImageView02); iv_2.setImageDrawable(pic2); iv_2.setScaleType(ScaleType.CENTER_INSIDE); paramsIv_2 = (LayoutParams) iv_2.getLayoutParams(); picX2 = paramsIv_2.x; picY2 = paramsIv_2.y; Drawable pic3 = getResources().getDrawable(queue[ 2 ]); iv_3 = (ImageView) this .findViewById(R.id.ImageView03); iv_3.setImageDrawable(pic3); iv_3.setScaleType(ScaleType.CENTER_INSIDE); paramsIv_3 = (LayoutParams) iv_3.getLayoutParams(); picX3 = paramsIv_3.x; picY3 = paramsIv_3.y; Drawable pic4 = getResources().getDrawable(queue[ 3 ]); iv_4 = (ImageView) this .findViewById(R.id.ImageView04); iv_4.setImageDrawable(pic4); iv_4.setScaleType(ScaleType.CENTER_INSIDE); paramsIv_4 = (LayoutParams) iv_4.getLayoutParams(); picX4 = paramsIv_4.x; picY4 = paramsIv_4.y; Drawable pic5 = getResources().getDrawable(queue[ 4 ]); iv_5 = (ImageView) this .findViewById(R.id.ImageView05); iv_5.setImageDrawable(pic5); iv_5.setScaleType(ScaleType.CENTER_INSIDE); paramsIv_5 = (LayoutParams) iv_5.getLayoutParams(); picX5 = paramsIv_5.x; picY5 = paramsIv_5.y; Drawable pic6 = getResources().getDrawable(queue[ 5 ]); iv_6 = (ImageView) this .findViewById(R.id.ImageView06); iv_6.setImageDrawable(pic6); iv_6.setScaleType(ScaleType.CENTER_INSIDE); paramsIv_6 = (LayoutParams) iv_6.getLayoutParams(); picX6 = paramsIv_6.x; picY6 = paramsIv_6.y; Drawable pic7 = getResources().getDrawable(queue[ 6 ]); iv_7 = (ImageView) this .findViewById(R.id.ImageView07); iv_7.setImageDrawable(pic7); iv_7.setScaleType(ScaleType.CENTER_INSIDE); paramsIv_7 = (LayoutParams) iv_7.getLayoutParams(); picX7 = paramsIv_7.x; picY7 = paramsIv_7.y; Drawable pic8 = getResources().getDrawable(queue[ 7 ]); iv_8 = (ImageView) this .findViewById(R.id.ImageView08); iv_8.setImageDrawable(pic8); iv_8.setScaleType(ScaleType.CENTER_INSIDE); paramsIv_8 = (LayoutParams) iv_8.getLayoutParams(); picX8 = paramsIv_8.x; picY8 = paramsIv_8.y; Drawable pic9 = getResources().getDrawable(queue[ 8 ]); iv_9 = (ImageView) this .findViewById(R.id.ImageView09); iv_9.setImageDrawable(pic9); iv_9.setScaleType(ScaleType.CENTER_INSIDE); paramsIv_9 = (LayoutParams) iv_9.getLayoutParams(); picX9 = paramsIv_9.x; picY9 = paramsIv_9.y; } int temp; @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // flag为TRUE时代表已经按下一张图片,跳过此部 if (flag) { break ; } else { flag = true ; // if语句 判断当鼠标点下时,是否击中图片 if (x >= picX1 && x <= picX1 picWidth && y >= picY1 && y <= picY1 picHeight) { clickPic1 = true ; } else { clickPic1 = false ; } if (x >= picX2 && x <= picX2 picWidth && y >= picY2 && y <= picY2 picHeight) { clickPic2 = true ; } else { clickPic2 = false ; } if (x >= picX3 && x <= picX3 picWidth && y >= picY3 && y <= picY3 picHeight) { clickPic3 = true ; } else { clickPic3 = false ; } if (x >= picX4 && x <= picX4 picWidth && y >= picY4 && y <= picY4 picHeight) { clickPic4 = true ; } else { clickPic4 = false ; } if (x >= picX5 && x <= picX5 picWidth && y >= picY5 && y <= picY5 picHeight) { clickPic5 = true ; } else { clickPic5 = false ; } if (x >= picX6 && x <= picX6 picWidth && y >= picY6 && y <= picY6 picHeight) { clickPic6 = true ; } else { clickPic6 = false ; } if (x >= picX7 && x <= picX7 picWidth && y >= picY7 && y <= picY7 picHeight) { clickPic7 = true ; } else { clickPic7 = false ; } if (x >= picX8 && x <= picX8 picWidth && y >= picY8 && y <= picY8 picHeight) { clickPic8 = true ; } else { clickPic8 = false ; } if (x >= picX9 && x <= picX9 picWidth && y >= picY9 && y <= picY9 picHeight) { clickPic9 = true ; } else { clickPic9 = false ; } break ; } case MotionEvent.ACTION_UP: // 保持ImageView的位置不变 paramsIv_1.x = picX1; paramsIv_1.y = picY1; iv_1.setLayoutParams(paramsIv_1); paramsIv_2.x = picX2; paramsIv_2.y = picY2; iv_2.setLayoutParams(paramsIv_2); paramsIv_3.x = picX3; paramsIv_3.y = picY3; iv_3.setLayoutParams(paramsIv_3); paramsIv_4.x = picX4; paramsIv_4.y = picY4; iv_4.setLayoutParams(paramsIv_4); paramsIv_5.x = picX5; paramsIv_5.y = picY5; iv_5.setLayoutParams(paramsIv_5); paramsIv_6.x = picX6; paramsIv_6.y = picY6; iv_6.setLayoutParams(paramsIv_6); paramsIv_7.x = picX7; paramsIv_7.y = picY7; iv_7.setLayoutParams(paramsIv_7); paramsIv_8.x = picX8; paramsIv_8.y = picY8; iv_8.setLayoutParams(paramsIv_8); paramsIv_9.x = picX9; paramsIv_9.y = picY9; iv_9.setLayoutParams(paramsIv_9); flag = false ; if (clickPic1) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 if (x >= picX2 && x <= picX2 picWidth && y >= picY2 && y <= picY2 picHeight) { // 交换全局int数组queue的元素顺序,达到交换图片位置的目的 temp = queue[ 0 ]; queue[ 0 ] = queue[ 1 ]; queue[ 1 ] = temp; // 显示交换顺序后的图片 findImageView(queue); } else if (x >= picX3 && x <= picX3 picWidth && y >= picY3 && y <= picY3 picHeight) { temp = queue[ 0 ]; queue[ 0 ] = queue[ 2 ]; queue[ 2 ] = temp; findImageView(queue); } else if (x >= picX4 && x <= picX4 picWidth && y >= picY4 && y <= picY4 picHeight) { temp = queue[ 0 ]; queue[ 0 ] = queue[ 3 ]; queue[ 3 ] = temp; findImageView(queue); } else if (x >= picX5 && x <= picX5 picWidth && y >= picY5 && y <= picY5 picHeight) { temp = queue[ 0 ]; queue[ 0 ] = queue[ 4 ]; queue[ 4 ] = temp; findImageView(queue); } else if (x >= picX6 && x <= picX6 picWidth && y >= picY6 && y <= picY6 picHeight) { temp = queue[ 0 ]; queue[ 0 ] = queue[ 5 ]; queue[ 5 ] = temp; findImageView(queue); } else if (x >= picX7 && x <= picX7 picWidth && y >= picY7 && y <= picY7 picHeight) { temp = queue[ 0 ]; queue[ 0 ] = queue[ 6 ]; queue[ 6 ] = temp; findImageView(queue); } else if (x >= picX8 && x <= picX8 picWidth && y >= picY8 && y <= picY8 picHeight) { temp = queue[ 0 ]; queue[ 0 ] = queue[ 7 ]; queue[ 7 ] = temp; findImageView(queue); } else if (x >= picX9 && x <= picX9 picWidth && y >= picY9 && y <= picY9 picHeight) { temp = queue[ 0 ]; queue[ 0 ] = queue[ 8 ]; queue[ 8 ] = temp; findImageView(queue); } else { } } if (clickPic2) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 if (x >= picX1 && x <= picX1 picWidth && y >= picY1 && y <= picY1 picHeight) { temp = queue[ 0 ]; queue[ 0 ] = queue[ 1 ]; queue[ 1 ] = temp; findImageView(queue); } else if (x >= picX3 && x <= picX3 picWidth && y >= picY3 && y <= picY3 picHeight) { temp = queue[ 1 ]; queue[ 1 ] = queue[ 2 ]; queue[ 2 ] = temp; findImageView(queue); } else if (x >= picX4 && x <= picX4 picWidth && y >= picY4 && y <= picY4 picHeight) { temp = queue[ 1 ]; queue[ 1 ] = queue[ 3 ]; queue[ 3 ] = temp; findImageView(queue); } else if (x >= picX5 && x <= picX5 picWidth && y >= picY5 && y <= picY5 picHeight) { temp = queue[ 1 ]; queue[ 1 ] = queue[ 4 ]; queue[ 4 ] = temp; findImageView(queue); } else if (x >= picX6 && x <= picX6 picWidth && y >= picY6 && y <= picY6 picHeight) { temp = queue[ 1 ]; queue[ 1 ] = queue[ 5 ]; queue[ 5 ] = temp; findImageView(queue); } else if (x >= picX7 && x <= picX7 picWidth && y >= picY7 && y <= picY7 picHeight) { temp = queue[ 1 ]; queue[ 1 ] = queue[ 6 ]; queue[ 6 ] = temp; findImageView(queue); } else if (x >= picX8 && x <= picX8 picWidth && y >= picY8 && y <= picY8 picHeight) { temp = queue[ 1 ]; queue[ 1 ] = queue[ 7 ]; queue[ 7 ] = temp; findImageView(queue); } else if (x >= picX9 && x <= picX9 picWidth && y >= picY9 && y <= picY9 picHeight) { temp = queue[ 1 ]; queue[ 1 ] = queue[ 8 ]; queue[ 8 ] = temp; findImageView(queue); } else { } } if (clickPic3) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 if (x >= picX1 && x <= picX1 picWidth && y >= picY1 && y <= picY1 picHeight) { temp = queue[ 0 ]; queue[ 0 ] = queue[ 2 ]; queue[ 2 ] = temp; findImageView(queue); } else if (x >= picX2 && x <= picX2 picWidth && y >= picY2 && y <= picY2 picHeight) { temp = queue[ 2 ]; queue[ 2 ] = queue[ 1 ]; queue[ 1 ] = temp; findImageView(queue); } else if (x >= picX4 && x <= picX4 picWidth && y >= picY4 && y <= picY4 picHeight) { temp = queue[ 2 ]; queue[ 2 ] = queue[ 3 ]; queue[ 3 ] = temp; findImageView(queue); } else if (x >= picX5 && x <= picX5 picWidth && y >= picY5 && y <= picY5 picHeight) { temp = queue[ 2 ]; queue[ 2 ] = queue[ 4 ]; queue[ 4 ] = temp; findImageView(queue); } else if (x >= picX6 && x <= picX6 picWidth && y >= picY6 && y <= picY6 picHeight) { temp = queue[ 2 ]; queue[ 2 ] = queue[ 5 ]; queue[ 5 ] = temp; findImageView(queue); } else if (x >= picX7 && x <= picX7 picWidth && y >= picY7 && y <= picY7 picHeight) { temp = queue[ 2 ]; queue[ 2 ] = queue[ 6 ]; queue[ 6 ] = temp; findImageView(queue); } else if (x >= picX8 && x <= picX8 picWidth && y >= picY8 && y <= picY8 picHeight) { temp = queue[ 2 ]; queue[ 2 ] = queue[ 7 ]; queue[ 7 ] = temp; findImageView(queue); } else if (x >= picX9 && x <= picX9 picWidth && y >= picY9 && y <= picY9 picHeight) { temp = queue[ 2 ]; queue[ 2 ] = queue[ 8 ]; queue[ 8 ] = temp; findImageView(queue); } else { } } if (clickPic4) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 if (x >= picX1 && x <= picX1 picWidth && y >= picY1 && y <= picY1 picHeight) { temp = queue[ 0 ]; queue[ 0 ] = queue[ 3 ]; queue[ 3 ] = temp; findImageView(queue); } else if (x >= picX2 && x <= picX2 picWidth && y >= picY2 && y <= picY2 picHeight) { temp = queue[ 3 ]; queue[ 3 ] = queue[ 1 ]; queue[ 1 ] = temp; findImageView(queue); } else if (x >= picX3 && x <= picX3 picWidth && y >= picY3 && y <= picY3 picHeight) { temp = queue[ 2 ]; queue[ 2 ] = queue[ 3 ]; queue[ 3 ] = temp; findImageView(queue); } else if (x >= picX5 && x <= picX5 picWidth && y >= picY5 && y <= picY5 picHeight) { temp = queue[ 4 ]; queue[ 4 ] = queue[ 3 ]; queue[ 3 ] = temp; findImageView(queue); } else if (x >= picX6 && x <= picX6 picWidth && y >= picY6 && y <= picY6 picHeight) { temp = queue[ 5 ]; queue[ 5 ] = queue[ 3 ]; queue[ 3 ] = temp; findImageView(queue); } else if (x >= picX7 && x <= picX7 picWidth && y >= picY7 && y <= picY7 picHeight) { temp = queue[ 6 ]; queue[ 6 ] = queue[ 3 ]; queue[ 3 ] = temp; findImageView(queue); } else if (x >= picX8 && x <= picX8 picWidth && y >= picY8 && y <= picY8 picHeight) { temp = queue[ 7 ]; queue[ 7 ] = queue[ 3 ]; queue[ 3 ] = temp; findImageView(queue); } else if (x >= picX9 && x <= picX9 picWidth && y >= picY9 && y <= picY9 picHeight) { temp = queue[ 8 ]; queue[ 8 ] = queue[ 3 ]; queue[ 3 ] = temp; findImageView(queue); } else { } } if (clickPic5) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 if (x >= picX1 && x <= picX1 picWidth && y >= picY1 && y <= picY1 picHeight) { temp = queue[ 0 ]; queue[ 0 ] = queue[ 4 ]; queue[ 4 ] = temp; findImageView(queue); } else if (x >= picX2 && x <= picX2 picWidth && y >= picY2 && y <= picY2 picHeight) { temp = queue[ 4 ]; queue[ 4 ] = queue[ 1 ]; queue[ 1 ] = temp; findImageView(queue); } else if (x >= picX3 && x <= picX3 picWidth && y >= picY3 && y <= picY3 picHeight) { temp = queue[ 2 ]; queue[ 2 ] = queue[ 4 ]; queue[ 4 ] = temp; findImageView(queue); } else if (x >= picX4 && x <= picX4 picWidth && y >= picY4 && y <= picY4 picHeight) { temp = queue[ 4 ]; queue[ 4 ] = queue[ 3 ]; queue[ 3 ] = temp; findImageView(queue); } else if (x >= picX6 && x <= picX6 picWidth && y >= picY6 && y <= picY6 picHeight) { temp = queue[ 5 ]; queue[ 5 ] = queue[ 4 ]; queue[ 4 ] = temp; findImageView(queue); } else if (x >= picX7 && x <= picX7 picWidth && y >= picY7 && y <= picY7 picHeight) { temp = queue[ 6 ]; queue[ 6 ] = queue[ 4 ]; queue[ 4 ] = temp; findImageView(queue); } else if (x >= picX8 && x <= picX8 picWidth && y >= picY8 && y <= picY8 picHeight) { temp = queue[ 7 ]; queue[ 7 ] = queue[ 4 ]; queue[ 4 ] = temp; findImageView(queue); } else if (x >= picX9 && x <= picX9 picWidth && y >= picY9 && y <= picY9 picHeight) { temp = queue[ 8 ]; queue[ 8 ] = queue[ 4 ]; queue[ 4 ] = temp; findImageView(queue); } else { } } if (clickPic6) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 if (x >= picX1 && x <= picX1 picWidth && y >= picY1 && y <= picY1 picHeight) { temp = queue[ 0 ]; queue[ 0 ] = queue[ 5 ]; queue[ 5 ] = temp; findImageView(queue); } else if (x >= picX2 && x <= picX2 picWidth && y >= picY2 && y <= picY2 picHeight) { temp = queue[ 5 ]; queue[ 5 ] = queue[ 1 ]; queue[ 1 ] = temp; findImageView(queue); } else if (x >= picX3 && x <= picX3 picWidth && y >= picY3 && y <= picY3 picHeight) { temp = queue[ 2 ]; queue[ 2 ] = queue[ 5 ]; queue[ 5 ] = temp; findImageView(queue); } else if (x >= picX4 && x <= picX4 picWidth && y >= picY4 && y <= picY4 picHeight) { temp = queue[ 5 ]; queue[ 5 ] = queue[ 3 ]; queue[ 3 ] = temp; findImageView(queue); } else if (x >= picX5 && x <= picX5 picWidth && y >= picY5 && y <= picY5 picHeight) { temp = queue[ 5 ]; queue[ 5 ] = queue[ 4 ]; queue[ 4 ] = temp; findImageView(queue); } else if (x >= picX7 && x <= picX7 picWidth && y >= picY7 && y <= picY7 picHeight) { temp = queue[ 6 ]; queue[ 6 ] = queue[ 5 ]; queue[ 5 ] = temp; findImageView(queue); } else if (x >= picX8 && x <= picX8 picWidth && y >= picY8 && y <= picY8 picHeight) { temp = queue[ 7 ]; queue[ 7 ] = queue[ 5 ]; queue[ 5 ] = temp; findImageView(queue); } else if (x >= picX9 && x <= picX9 picWidth && y >= picY9 && y <= picY9 picHeight) { temp = queue[ 8 ]; queue[ 8 ] = queue[ 5 ]; queue[ 5 ] = temp; findImageView(queue); } else { } } if (clickPic7) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 if (x >= picX1 && x <= picX1 picWidth && y >= picY1 && y <= picY1 picHeight) { temp = queue[ 0 ]; queue[ 0 ] = queue[ 6 ]; queue[ 6 ] = temp; findImageView(queue); } else if (x >= picX2 && x <= picX2 picWidth && y >= picY2 && y <= picY2 picHeight) { temp = queue[ 6 ]; queue[ 6 ] = queue[ 1 ]; queue[ 1 ] = temp; findImageView(queue); } else if (x >= picX3 && x <= picX3 picWidth && y >= picY3 && y <= picY3 picHeight) { temp = queue[ 2 ]; queue[ 2 ] = queue[ 6 ]; queue[ 6 ] = temp; findImageView(queue); } else if (x >= picX4 && x <= picX4 picWidth && y >= picY4 && y <= picY4 picHeight) { temp = queue[ 6 ]; queue[ 6 ] = queue[ 3 ]; queue[ 3 ] = temp; findImageView(queue); } else if (x >= picX5 && x <= picX5 picWidth && y >= picY5 && y <= picY5 picHeight) { temp = queue[ 6 ]; queue[ 6 ] = queue[ 4 ]; queue[ 4 ] = temp; findImageView(queue); } else if (x >= picX6 && x <= picX6 picWidth && y >= picY6 && y <= picY6 picHeight) { temp = queue[ 6 ]; queue[ 6 ] = queue[ 5 ]; queue[ 5 ] = temp; findImageView(queue); } else if (x >= picX8 && x <= picX8 picWidth && y >= picY8 && y <= picY8 picHeight) { temp = queue[ 7 ]; queue[ 7 ] = queue[ 6 ]; queue[ 6 ] = temp; findImageView(queue); } else if (x >= picX9 && x <= picX9 picWidth && y >= picY9 && y <= picY9 picHeight) { temp = queue[ 8 ]; queue[ 8 ] = queue[ 6 ]; queue[ 6 ] = temp; findImageView(queue); } else { } } if (clickPic8) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 if (x >= picX1 && x <= picX1 picWidth && y >= picY1 && y <= picY1 picHeight) { temp = queue[ 0 ]; queue[ 0 ] = queue[ 7 ]; queue[ 7 ] = temp; findImageView(queue); } else if (x >= picX2 && x <= picX2 picWidth && y >= picY2 && y <= picY2 picHeight) { temp = queue[ 7 ]; queue[ 7 ] = queue[ 1 ]; queue[ 1 ] = temp; findImageView(queue); } else if (x >= picX3 && x <= picX3 picWidth && y >= picY3 && y <= picY3 picHeight) { temp = queue[ 2 ]; queue[ 2 ] = queue[ 7 ]; queue[ 7 ] = temp; findImageView(queue); } else if (x >= picX4 && x <= picX4 picWidth && y >= picY4 && y <= picY4 picHeight) { temp = queue[ 7 ]; queue[ 7 ] = queue[ 3 ]; queue[ 3 ] = temp; findImageView(queue); } else if (x >= picX5 && x <= picX5 picWidth && y >= picY5 && y <= picY5 picHeight) { temp = queue[ 7 ]; queue[ 7 ] = queue[ 4 ]; queue[ 4 ] = temp; findImageView(queue); } else if (x >= picX6 && x <= picX6 picWidth && y >= picY6 && y <= picY6 picHeight) { temp = queue[ 7 ]; queue[ 7 ] = queue[ 5 ]; queue[ 5 ] = temp; findImageView(queue); } else if (x >= picX7 && x <= picX7 picWidth && y >= picY7 && y <= picY7 picHeight) { temp = queue[ 7 ]; queue[ 7 ] = queue[ 6 ]; queue[ 6 ] = temp; findImageView(queue); } else if (x >= picX9 && x <= picX9 picWidth && y >= picY9 && y <= picY9 picHeight) { temp = queue[ 8 ]; queue[ 8 ] = queue[ 7 ]; queue[ 7 ] = temp; findImageView(queue); } else { } } if (clickPic9) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 if (x >= picX1 && x <= picX1 picWidth && y >= picY1 && y <= picY1 picHeight) { temp = queue[ 0 ]; queue[ 0 ] = queue[ 8 ]; queue[ 8 ] = temp; findImageView(queue); } else if (x >= picX2 && x <= picX2 picWidth && y >= picY2 && y <= picY2 picHeight) { temp = queue[ 8 ]; queue[ 8 ] = queue[ 1 ]; queue[ 1 ] = temp; findImageView(queue); } else if (x >= picX3 && x <= picX3 picWidth && y >= picY3 && y <= picY3 picHeight) { temp = queue[ 2 ]; queue[ 2 ] = queue[ 8 ]; queue[ 8 ] = temp; findImageView(queue); } else if (x >= picX4 && x <= picX4 picWidth && y >= picY4 && y <= picY4 picHeight) { temp = queue[ 8 ]; queue[ 8 ] = queue[ 3 ]; queue[ 3 ] = temp; findImageView(queue); } else if (x >= picX5 && x <= picX5 picWidth && y >= picY5 && y <= picY5 picHeight) { temp = queue[ 8 ]; queue[ 8 ] = queue[ 4 ]; queue[ 4 ] = temp; findImageView(queue); } else if (x >= picX6 && x <= picX6 picWidth && y >= picY6 && y <= picY6 picHeight) { temp = queue[ 8 ]; queue[ 8 ] = queue[ 5 ]; queue[ 5 ] = temp; findImageView(queue); } else if (x >= picX7 && x <= picX7 picWidth && y >= picY7 && y <= picY7 picHeight) { temp = queue[ 8 ]; queue[ 8 ] = queue[ 6 ]; queue[ 6 ] = temp; findImageView(queue); } else if (x >= picX8 && x <= picX8 picWidth && y >= picY8 && y <= picY8 picHeight) { temp = queue[ 8 ]; queue[ 8 ] = queue[ 7 ]; queue[ 7 ] = temp; findImageView(queue); } else { } } break ; case MotionEvent.ACTION_MOVE: // 当Action_Down击中图片时,才执行move的动作 // 否则跳过 if (clickPic1) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 paramsIv_1.x = ( int ) (x - picWidth / 2 ); paramsIv_1.y = ( int ) (y - picHeight / 2 ); //通过setLayoutParams达到图片跟随手指移动的目的 iv_1.setLayoutParams(paramsIv_1); } if (clickPic2) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 paramsIv_2.x = ( int ) (x - picWidth / 2 ); paramsIv_2.y = ( int ) (y - picHeight / 2 ); iv_2.setLayoutParams(paramsIv_2); } if (clickPic3) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 paramsIv_3.x = ( int ) (x - picWidth / 2 ); paramsIv_3.y = ( int ) (y - picHeight / 2 ); iv_3.setLayoutParams(paramsIv_3); } if (clickPic4) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 paramsIv_4.x = ( int ) (x - picWidth / 2 ); paramsIv_4.y = ( int ) (y - picHeight / 2 ); iv_4.setLayoutParams(paramsIv_4); } if (clickPic5) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 paramsIv_5.x = ( int ) (x - picWidth / 2 ); paramsIv_5.y = ( int ) (y - picHeight / 2 ); iv_5.setLayoutParams(paramsIv_5); } if (clickPic6) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 paramsIv_6.x = ( int ) (x - picWidth / 2 ); paramsIv_6.y = ( int ) (y - picHeight / 2 ); iv_6.setLayoutParams(paramsIv_6); } if (clickPic7) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 paramsIv_7.x = ( int ) (x - picWidth / 2 ); paramsIv_7.y = ( int ) (y - picHeight / 2 ); iv_7.setLayoutParams(paramsIv_7); } if (clickPic8) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 paramsIv_8.x = ( int ) (x - picWidth / 2 ); paramsIv_8.y = ( int ) (y - picHeight / 2 ); iv_8.setLayoutParams(paramsIv_8); } if (clickPic9) { // picWidth与picHeight分别除以2,定位击中点为图片的中心 paramsIv_9.x = ( int ) (x - picWidth / 2 ); paramsIv_9.y = ( int ) (y - picHeight / 2 ); iv_9.setLayoutParams(paramsIv_9); } break ; } return super .onTouchEvent(event); } } |
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论