zoukankan      html  css  js  c++  java
  • android UI 仿 win 8 模块化 标题,并实现 可长按拖动交换图片位置、可点击,且伴随动画特效

    转载请声明出处,谢谢!http://www.cnblogs.com/linguanh/

    先上效果图,给大家个直观效果,后上实现代码:

     ->  ->-> 

    ok,现在简单说下我上面的图片被做了什么操作,长按“休闲场所”,然后代码实现 震动,告诉用户,现在可以移动了,然后我把它和“海滨沿岸” 互换位置,注意此时的 图片是 半透明的,这些都是自定义特效,可以任意改。

    代码来了:

          这里我先给出,布局文件不含(ViewPager),这个根据个人修改的。

      1 <?xml version="1.0" encoding="utf-8"?>
      2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      3     android:layout_width="fill_parent"
      4     android:layout_height="fill_parent"
      5     android:orientation="horizontal"
      6     android:background="@color/common_bg"
      7     >
      8     <LinearLayout
      9         android:id="@+id/ll"
     10         android:layout_width="wrap_content"
     11         android:layout_height="wrap_content"
     12         android:orientation="vertical"
     13         android:layout_centerInParent="true">
     14         <LinearLayout
     15             android:layout_width="fill_parent"
     16             android:layout_height="wrap_content"
     17             android:orientation="horizontal">
     18             <RelativeLayout
     19                 android:id="@+id/rl1"
     20                 android:layout_width="160dp"
     21                 android:layout_height="160dp"
     22                 android:clickable="true"
     23                 android:background="@drawable/one">
     24                 <ImageView
     25                     android:id="@+id/img1"
     26                     android:layout_width="fill_parent"
     27                     android:layout_height="fill_parent"
     28                     android:layout_margin="30dp"
     29                     android:contentDescription="@string/hello"/>
     30                 <RelativeLayout
     31                     android:alpha="0.4"
     32                     android:background="#000000"
     33                     android:layout_width="wrap_content"
     34                     android:layout_alignParentBottom="true"
     35                     android:layout_alignParentRight="true"
     36                     android:layout_height="20dp">
     37                     <TextView
     38                         android:id="@+id/tv1"
     39                         android:alpha="1"
     40                         android:layout_width="wrap_content"
     41                         android:layout_height="wrap_content"
     42                         android:layout_alignParentBottom="true"
     43                         android:textColor="@color/white"
     44                         android:textSize="@dimen/text_normal_size"
     45                         android:singleLine="true"
     46                         android:layout_alignParentRight="true"/>
     47                 </RelativeLayout>
     48             </RelativeLayout>
     49             <RelativeLayout
     50                 android:id="@+id/rl2"
     51                 android:layout_width="160dp"
     52                 android:layout_height="160dp"
     53                 android:layout_marginLeft="5dp"
     54                 android:clickable="true"
     55                 android:background="@drawable/two">
     56                 <ImageView
     57                     android:id="@+id/img2"
     58                     android:layout_width="fill_parent"
     59                     android:layout_height="fill_parent"
     60                     android:layout_margin="30dp"
     61                     android:contentDescription="@string/hello"/>
     62                 <RelativeLayout
     63                     android:alpha="0.4"
     64                     android:background="#000000"
     65                     android:layout_width="wrap_content"
     66                     android:layout_alignParentBottom="true"
     67                     android:layout_alignParentRight="true"
     68                     android:layout_height="20dp">
     69                     <TextView
     70                         android:id="@+id/tv2"
     71                         android:layout_width="wrap_content"
     72                         android:layout_height="wrap_content"
     73                         android:layout_alignParentBottom="true"
     74                         android:textColor="@color/white"
     75                         android:textSize="@dimen/text_normal_size"
     76                         android:singleLine="true"
     77                         android:layout_alignParentRight="true"/>
     78                 </RelativeLayout>
     79             </RelativeLayout>
     80         </LinearLayout>
     81 
     82         <LinearLayout
     83             android:layout_width="fill_parent"
     84             android:layout_height="wrap_content"
     85             android:layout_marginTop="5dp"
     86             android:orientation="horizontal">
     87             <RelativeLayout
     88                 android:id="@+id/rl3"
     89                 android:layout_width="160dp"
     90                 android:layout_height="160dp"
     91                 android:clickable="true"
     92                 android:background="@drawable/three">
     93                 <ImageView
     94                     android:id="@+id/img3"
     95                     android:layout_width="fill_parent"
     96                     android:layout_height="fill_parent"
     97                     android:layout_margin="30dp"
     98                     android:contentDescription="@string/hello"/>
     99                 <RelativeLayout
    100                     android:alpha="0.4"
    101                     android:background="#000000"
    102                     android:layout_width="wrap_content"
    103                     android:layout_alignParentBottom="true"
    104                     android:layout_alignParentRight="true"
    105                     android:layout_height="20dp">
    106                     <TextView
    107                         android:id="@+id/tv3"
    108                         android:layout_width="wrap_content"
    109                         android:layout_height="wrap_content"
    110                         android:layout_alignParentBottom="true"
    111                         android:textColor="@color/white"
    112                         android:textSize="@dimen/text_normal_size"
    113                         android:singleLine="true"
    114                         android:layout_alignParentRight="true"/>
    115                 </RelativeLayout>
    116             </RelativeLayout>
    117 
    118             <RelativeLayout
    119                 android:id="@+id/rl4"
    120                 android:layout_width="160dp"
    121                 android:layout_height="160dp"
    122                 android:layout_marginLeft="5dp"
    123                 android:clickable="true"
    124                 android:background="@drawable/four">
    125                 <ImageView
    126                     android:id="@+id/img4"
    127                     android:layout_width="fill_parent"
    128                     android:layout_height="fill_parent"
    129                     android:layout_margin="30dp"
    130                     android:contentDescription="@string/hello"/>
    131                 <RelativeLayout
    132                     android:alpha="0.4"
    133                     android:background="#000000"
    134                     android:layout_width="wrap_content"
    135                     android:layout_alignParentBottom="true"
    136                     android:layout_alignParentRight="true"
    137                     android:layout_height="20dp">
    138                     <TextView
    139                         android:id="@+id/tv4"
    140                         android:layout_width="wrap_content"
    141                         android:layout_height="wrap_content"
    142                         android:layout_alignParentBottom="true"
    143                         android:textColor="@color/white"
    144                         android:textSize="@dimen/text_normal_size"
    145                         android:singleLine="true"
    146                         android:layout_alignParentRight="true"/>
    147                 </RelativeLayout>
    148             </RelativeLayout>
    149         </LinearLayout>
    150 
    151         <LinearLayout
    152             android:layout_width="fill_parent"
    153             android:layout_height="wrap_content"
    154             android:layout_marginTop="5dp"
    155             android:orientation="horizontal">
    156             <RelativeLayout
    157                 android:id="@+id/rl5"
    158                 android:layout_width="160dp"
    159                 android:layout_height="160dp"
    160                 android:clickable="true"
    161                 android:background="@drawable/backg">
    162                 <ImageView
    163                     android:id="@+id/img5"
    164                     android:layout_width="fill_parent"
    165                     android:layout_height="fill_parent"
    166                     android:layout_margin="30dp"
    167                     android:contentDescription="@string/hello"/>
    168                 <RelativeLayout
    169                     android:alpha="0.4"
    170                     android:background="#000000"
    171                     android:layout_width="wrap_content"
    172                     android:layout_alignParentBottom="true"
    173                     android:layout_alignParentRight="true"
    174                     android:layout_height="20dp">
    175                     <TextView
    176                         android:id="@+id/tv5"
    177                         android:layout_width="wrap_content"
    178                         android:layout_height="wrap_content"
    179                         android:layout_alignParentBottom="true"
    180                         android:textColor="@color/white"
    181                         android:textSize="@dimen/text_normal_size"
    182                         android:singleLine="true"
    183                         android:layout_alignParentRight="true"/>
    184                 </RelativeLayout>
    185             </RelativeLayout>
    186             <RelativeLayout
    187                 android:id="@+id/rl6"
    188                 android:layout_width="160dp"
    189                 android:layout_height="160dp"
    190                 android:layout_marginLeft="5dp">
    191                 <RelativeLayout
    192                     android:id="@+id/rl7"
    193                     android:layout_width="fill_parent"
    194                     android:layout_height="60dp"
    195                     android:layout_marginBottom="5dp"
    196                     android:clickable="true"
    197                     android:background="@drawable/five">
    198                     <ImageView
    199                         android:id="@+id/img7"
    200                         android:layout_width="36dp"
    201                         android:layout_height="36dp"
    202                         android:layout_centerVertical="true"
    203                         android:layout_alignParentLeft="true"
    204                         android:layout_marginLeft="10dp"
    205                         android:contentDescription="@string/hello"/>
    206                     <RelativeLayout
    207                         android:alpha="0.4"
    208                         android:background="#000000"
    209                         android:layout_width="wrap_content"
    210                         android:layout_alignParentBottom="true"
    211                         android:layout_alignParentRight="true"
    212                         android:layout_height="20dp">
    213                         <TextView
    214                             android:id="@+id/tv7"
    215                             android:layout_width="wrap_content"
    216                             android:layout_height="wrap_content"
    217                             android:layout_alignParentBottom="true"
    218                             android:textColor="@color/white"
    219                             android:textSize="@dimen/text_normal_size"
    220                             android:singleLine="true"
    221                             android:layout_alignParentRight="true"/>
    222                     </RelativeLayout>
    223                 </RelativeLayout>
    224                 <RelativeLayout
    225                     android:id="@+id/rl8"
    226                     android:layout_width="80dp"
    227                     android:layout_height="wrap_content"
    228                     android:layout_below="@id/rl7"
    229                     android:layout_alignParentLeft="true"
    230                     android:clickable="true"
    231                     android:background="@drawable/six">
    232                     <ImageView
    233                         android:id="@+id/img8"
    234                         android:layout_width="36dp"
    235                         android:layout_height="36dp"
    236                         android:layout_centerInParent="true"
    237                         android:contentDescription="@string/hello"/>
    238                     <RelativeLayout
    239                         android:alpha="0.4"
    240                         android:background="#000000"
    241                         android:layout_width="wrap_content"
    242                         android:layout_alignParentBottom="true"
    243                         android:layout_alignParentRight="true"
    244                         android:layout_height="20dp">
    245                         <TextView
    246                             android:id="@+id/tv8"
    247                             android:layout_width="wrap_content"
    248                             android:layout_height="wrap_content"
    249                             android:layout_alignParentBottom="true"
    250                             android:textColor="@color/white"
    251                             android:textSize="@dimen/text_little_size"
    252                             android:singleLine="true"
    253                             android:layout_alignParentRight="true"/>
    254                     </RelativeLayout>
    255                 </RelativeLayout>
    256 
    257                 <RelativeLayout
    258                     android:id="@+id/rl9"
    259                     android:layout_width="75dp"
    260                     android:layout_height="wrap_content"
    261                     android:layout_below="@id/rl7"
    262                     android:layout_alignParentRight="true"
    263                     android:clickable="true"
    264                     android:background="@drawable/seven">
    265                     <ImageView
    266                         android:id="@+id/img9"
    267                         android:layout_width="36dp"
    268                         android:layout_height="36dp"
    269                         android:layout_centerInParent="true"
    270                         android:contentDescription="@string/hello"/>
    271                     <RelativeLayout
    272                         android:alpha="0.4"
    273                         android:background="#000000"
    274                         android:layout_width="wrap_content"
    275                         android:layout_alignParentBottom="true"
    276                         android:layout_alignParentRight="true"
    277                         android:layout_height="20dp">
    278                         <TextView
    279                             android:id="@+id/tv9"
    280                             android:layout_width="wrap_content"
    281                             android:layout_height="wrap_content"
    282                             android:layout_alignParentBottom="true"
    283                             android:textColor="@color/white"
    284                             android:textSize="@dimen/text_little_size"
    285                             android:singleLine="true"
    286                             android:layout_alignParentRight="true"/>
    287                     </RelativeLayout>
    288                 </RelativeLayout>
    289             </RelativeLayout>
    290         </LinearLayout>
    291     </LinearLayout>
    292 </RelativeLayout>

    这里再给出,里面的动画xml,可能有读者会需要到

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <set xmlns:android="http://schemas.android.com/apk/res/android" >
     3     <alpha
     4         android:interpolator="@android:anim/linear_interpolator" 
     5         android:fromAlpha="1.0"
     6         android:toAlpha="0.1"
     7         android:duration="500"
     8         android:repeatCount="1"
     9         android:repeatMode="reverse"
    10         />
    11     <scale 
    12         android:fromXScale="1.0" 
    13         android:toXScale="0.8"
    14         android:fromYScale="1.0" 
    15         android:toYScale="0.8" 
    16         android:pivotX="50%"
    17         android:pivotY="50%" 
    18         android:duration="500" 
    19         android:repeatCount="1"
    20         android:repeatMode="reverse"
    21         android:interpolator="@android:anim/linear_interpolator" 
    22         />
    23 </set>

    java代码:

          使用前需要知道,由于我是把这个页面 装载 在 viewPager 里面的,它是一个 fragment,本身 viewPager 有 onTouchEvent 事件,即可以左右侧滑,所以,当我在 这个页面里,长按图片试图拖动它的时候,就会造成 viewPager 和 子页面 长按滑动 冲突问题,嗯,是的。不过我已经把这个问题解决了,方法请见我的另外一篇博文链接http://www.cnblogs.com/linguanh/p/4540099.html

          

           代码里面一些必要的注释,我已详细给出,相信能帮助大家理解。

      1 package com.LGH.weixin;
      2 
      3 import android.app.Activity;
      4 import android.content.Context;
      5 import android.content.Intent;
      6 import android.content.SharedPreferences;
      7 import android.graphics.Bitmap;
      8 import android.graphics.drawable.Drawable;
      9 import android.os.Bundle;
     10 import android.support.v4.app.Fragment;
     11 import android.util.DisplayMetrics;
     12 import android.util.Log;
     13 import android.view.Gravity;
     14 import android.view.LayoutInflater;
     15 import android.view.MenuItem;
     16 import android.view.MotionEvent;
     17 import android.view.View;
     18 import android.view.View.OnClickListener;
     19 import android.view.View.OnLongClickListener;
     20 import android.view.View.OnTouchListener;
     21 import android.view.ViewGroup;
     22 import android.view.WindowManager;
     23 import android.view.animation.Animation;
     24 import android.view.animation.AnimationUtils;
     25 import android.widget.ImageView;
     26 import android.widget.RelativeLayout;
     27 import android.widget.TextView;
     28 import android.widget.Toast;
     29 
     30 /**
     31  * Created by Administrator on 2015/5/25.
     32  */
     33 public class apartFragment extends Fragment implements OnTouchListener, OnClickListener, OnLongClickListener, Animation.AnimationListener{
     34 
     35     View main;
     36     View mainActivity;//为了改变 主页面的viewPager 而设置
     37 
     38     myViewPager temp;
     39     Activity myActivity = new Activity();
     40     //MainActivity in_order_to_forbid_viewPager_slip = new MainActivity();
     41 
     42     private WindowManager windowManager;
     43     private WindowManager.LayoutParams    windowParams;
     44     private View animationView; //当前单次点击播放动画的 view
     45 
     46     public static int displayWidth;  //屏幕宽度
     47     public static int displayHeight; //屏幕高度
     48 
     49     // 标志长按控件动作是否激活
     50     private boolean isMove = false;
     51     private boolean isAnimotionFinish = false;
     52 
     53     private ImageView dragImageView;        // 被拖动控件的preview
     54     private int            fromPoint = -1;//记录被拖动的View
     55     private int            toPoint = -1;//记录停止拖动时被碰撞的View
     56     private Drawable temp_img;//缓存被拖动控件的ImageView的内容
     57     private Drawable    temp_view_img;//缓存被拖动控件的 View的内容
     58     private Drawable    temp_view_img_topoint;//缓存被拖动控件 经过 的 View的内容
     59     private int[] relativeLayout_bgs = //最终只能通过它来解决 在拖动小图经过大图过程中,经过的大图被失真的问题
     60             new int[]{R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.backg,R.drawable.five,R.drawable.six,R.drawable.seven};
     61 
     62     private String        temp_str;//缓存被拖动控件的TextView的内容
     63 
     64     //移动的位置
     65     private int                            dragPointX;
     66     private int                            dragPointY;
     67     //当前位置距离边界的位置
     68     private int                            dragOffsetX;
     69     private int                            dragOffsetY;
     70     private int x, y;
     71     //用于循环碰撞的数组
     72     private View[] views = new View[8];
     73     private TextView[] tvs = new TextView[8];
     74     private ImageView[] ivs = new ImageView[8];
     75     //用来显示经纬度、发包数
     76     private TextView bottom_bar_up, bottom_bar_down;
     77     //用于记录碰撞的面积
     78     //图标出现和消失的过度动画
     79     private Animation flash;
     80     //    private Animation disappear;
     81     private Animation blink;
     82     private Animation original;
     83     /*
     84      * 用于记录所有View的坐标的二位数组
     85      * points[0][0]用于记录左上角的X
     86      * points[0][1]用于记录左上角的Y
     87      * points[0][2]用于记录右下角的X
     88      * points[0][3]用于记录右下角的Y
     89      */
     90     private int[][] points = new int[8][4];
     91 
     92     @Override
     93     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     94         main = inflater.inflate(R.layout.apart_center,container,false);
     95         mainActivity = inflater.inflate(R.layout.activity_main,container,false);
     96 
     97         myActivity = getActivity();
     98 
     99         //temp = (myViewPager) mainActivity.findViewById(R.id.vp);
    100         //temp = in_order_to_forbid_viewPager_slip.getVP();
    101         //temp.requestDisallowInterceptTouchEvent(true);
    102         //temp.setStopViewPagerSlip(false);
    103 
    104         initView(main);//初始化页面
    105 
    106         for(int i = 0; i < views.length; i++){
    107             views[i].setOnClickListener(this);//绑定点击(短按)监听器
    108             views[i].setOnLongClickListener(this);//绑定长按监听器
    109             views[i].setOnTouchListener(this);//绑定触摸监听器
    110         }
    111         flash = AnimationUtils.loadAnimation(getActivity(), R.anim.flash); //自定义缩放动画
    112         //disappear = AnimationUtils.loadAnimation(this, R.anim.disappear);
    113         blink = AnimationUtils.loadAnimation(getActivity(), R.anim.blink);
    114         original = AnimationUtils.loadAnimation(getActivity(), R.anim.original);
    115 
    116         return main;
    117 
    118     }
    119 
    120 
    121 
    122     //触摸监听器
    123 
    124     @Override
    125     public boolean onTouch(View v, MotionEvent event) {
    126         // TODO Auto-generated method stub
    127         x = (int) event.getX();
    128         y = (int) event.getY();
    129         switch(event.getAction()){
    130             case MotionEvent.ACTION_DOWN:
    131                 if(points[0][0] == 0){
    132                     initPoints();
    133                 }
    134                 break;
    135             case MotionEvent.ACTION_MOVE:
    136 
    137                 if(isMove){
    138                     if(dragImageView == null){
    139 
    140                         dragPointX = x;
    141                         dragPointY = y;
    142                         dragOffsetX = (int)event.getRawX() - x;
    143                         dragOffsetY = (int)event.getRawY() - y;
    144                         //移动细节的 logcat 记录
    145                         Log.v("getX", String.valueOf(x) + "=====" + String.valueOf(event.getX()));
    146                         Log.v("getY", String.valueOf(y) + "=====" + String.valueOf(event.getY()));
    147                         Log.v("getLeft", String.valueOf(v.getLeft()) + "=====" + String.valueOf(event.getX()-v.getLeft()));
    148                         Log.v("getgetTop", String.valueOf(v.getTop()) + "=====" + String.valueOf(event.getY()-v.getTop()));
    149                         Log.v("getRawX", String.valueOf(event.getRawX()) + "=====" + String.valueOf(event.getRawX() - event.getX()));
    150                         Log.v("getRawY", String.valueOf(event.getRawY()) + "=====" + String.valueOf(event.getRawY() - event.getY()));
    151 
    152                         v.destroyDrawingCache();
    153                         v.setDrawingCacheEnabled(true);
    154                         v.setDrawingCacheBackgroundColor(0x000000);
    155                         Bitmap bm = Bitmap.createBitmap(v.getDrawingCache(true));
    156                         Bitmap bitmap = Bitmap.createBitmap(bm, 8, 8, bm.getWidth()-8, bm.getHeight()-8);
    157                         startDrag(bitmap, x, y); //实时生成 拖动效果,参数一是 当前图片,x y为目标标
    158 //                        v.startAnimation(blink);
    159                         v.setVisibility(View.INVISIBLE);//隐藏当前被长按的控件
    160                         Log.v("OnTouch>>>>", "===隐藏!!!");
    161                     }else{
    162                         onDrag(x, y);
    163                         hide();
    164                         Log.v("OnTouch>>>>", "===动了!!!");
    165                     }
    166                 }else{
    167                     Log.v("OnTouch>>>>", "===不移动");
    168                 }
    169                 break;
    170             case MotionEvent.ACTION_UP:
    171                 //if(!isMove){ //
    172 
    173                 //}
    174                 isMove = false;
    175                 stopDrag();
    176 //                fromPoint = -1;
    177 //                toPoint = -1;
    178                 show();
    179                 exchange();
    180 
    181 
    182                 break;
    183         }
    184         return false;
    185     }
    186     /**
    187      * 长按监听器
    188      */
    189     @Override
    190     public boolean onLongClick(View v) {
    191         // TODO Auto-generated method stub
    192         Log.v("onLongClick>>>>>", "LongClick");
    193         for(int i = 0; i < views.length; i ++){
    194             if(v.equals(views[i])){
    195                 fromPoint = i;
    196                 isMove = true;
    197                 getZhenDong.Vibrate(getActivity(), 100);//震动提醒
    198 
    199                 //temp.requestDisallowInterceptTouchEvent(true); //这个阻止申请的函数必须放在 onTouchListener 里面
    200                 // 在 该 fragment 长按,先去掉 viewPager 的侧滑,防止冲突
    201                 // requestDisallowInterceptTouchEvent(true); 在主页面可以实现,在这里还是不行,妈的
    202                 main.getParent().requestDisallowInterceptTouchEvent(true); //终于!!!,这样可以
    203 
    204                 //temp.setStopViewPagerSlip(false);//这样也是不行,fuck the dog !
    205 
    206                 temp_img = ivs[i].getDrawable();//初始化被拖动的View的 ImageView的缓存
    207                 temp_view_img = views[i].getBackground();
    208                 temp_str = tvs[i].getText().toString();//初始化被拖动的View的TextView的缓存
    209 
    210                 return true;
    211             }
    212         }
    213         return true;
    214     }
    215     /**
    216      * 短按监听器
    217      */
    218     @Override
    219     public void onClick(View v) {
    220         // TODO Auto-generated method stub
    221         animationView = v;
    222         Animation temp = AnimationUtils.loadAnimation(getActivity(), R.anim.flash);
    223         v.startAnimation(temp); //设置单点 时播放缩放动画
    224         for(int i = 0; i < views.length; i++){
    225             views[i].setOnClickListener(null);//一次点击后,移除所有的监听事件
    226         }
    227         temp.setAnimationListener(this);
    228         //跳转 操作 放置 动画 播放完毕的 监听里面,实现 播放完 动画 再 跳转
    229 
    230     }
    231     @Override
    232     public void onAnimationStart(Animation animation) {
    233         if (isAnimotionFinish) {
    234             isAnimotionFinish = false;
    235         }
    236     }
    237 
    238     @Override
    239     public void onAnimationEnd(Animation animation) {
    240         isAnimotionFinish = true;
    241         for (int i = 0; i < views.length; i++) {
    242             views[i].setOnClickListener(this);//恢复监听
    243         }
    244         Toast.makeText(getActivity(), "finish animation", Toast.LENGTH_LONG).show();
    245         for (int i = 0; i < views.length; i++) {
    246             Intent readToJump = new Intent(myActivity,infoMain.class);
    247             startActivity(readToJump);
    248         }
    249     }
    250 
    251     @Override
    252     public void onAnimationRepeat(Animation animation) {
    253 
    254     }
    255 
    256     /**
    257      * 初始化页面所有控件
    258      */
    259     private void initView(View view){
    260 
    261         //获取屏幕分辨率
    262         DisplayMetrics displayMetrics = new DisplayMetrics();
    263         myActivity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    264         displayWidth = displayMetrics.widthPixels;
    265         displayHeight = displayMetrics.heightPixels;
    266         //-------------下面的 ImageView 已被我 废弃,大家可以 根据自己需求,使用它 -------------
    267 
    268         views[0] = (RelativeLayout) view.findViewById(R.id.rl1);
    269         tvs[0] = (TextView) view.findViewById(R.id.tv1);
    270         ivs[0] = (ImageView) view.findViewById(R.id.img1);
    271 
    272         views[1] = (RelativeLayout) view.findViewById(R.id.rl2);
    273         tvs[1] = (TextView) view.findViewById(R.id.tv2);
    274         ivs[1] = (ImageView) view.findViewById(R.id.img2);
    275 
    276         views[2] = (RelativeLayout) view.findViewById(R.id.rl3);
    277         tvs[2] = (TextView) view.findViewById(R.id.tv3);
    278         ivs[2] = (ImageView) view.findViewById(R.id.img3);
    279 
    280         views[3] = (RelativeLayout) view.findViewById(R.id.rl4);
    281         tvs[3] = (TextView) view.findViewById(R.id.tv4);
    282         ivs[3] = (ImageView) view.findViewById(R.id.img4);
    283 
    284         views[4] = (RelativeLayout) view.findViewById(R.id.rl5);
    285         tvs[4] = (TextView) view.findViewById(R.id.tv5);
    286         ivs[4] = (ImageView) view.findViewById(R.id.img5);
    287 
    288         views[5] = (RelativeLayout) view.findViewById(R.id.rl7);//1/2大小的relativelayout
    289         tvs[5] = (TextView) view.findViewById(R.id.tv7);
    290         ivs[5] = (ImageView) view.findViewById(R.id.img7);
    291 
    292         views[6] = (RelativeLayout) view.findViewById(R.id.rl8);//1/4大小的relativelayout
    293         tvs[6] = (TextView) view.findViewById(R.id.tv8);
    294         ivs[6] = (ImageView) view.findViewById(R.id.img8);
    295 
    296         views[7] = (RelativeLayout) view.findViewById(R.id.rl9);//1/4大小的relativelayout
    297         tvs[7] = (TextView) view.findViewById(R.id.tv9);
    298         ivs[7] = (ImageView) view.findViewById(R.id.img9);
    299 
    300         //bottom_bar_up = (TextView)main.findViewById(R.id.bottom_bar_up);
    301         //bottom_bar_down = (TextView)main.findViewById(R.id.bottom_bar_down);
    302 
    303         int[] power = {0, 1, 2, 3, 4, 5, 6, 7};
    304         for(int i = 0; i < power.length; i++){
    305             switch (power[i]){
    306                 case 7:
    307                     tvs[i].setText(getText(R.string.folder));
    308                     break;
    309                 case 6:
    310                     tvs[i].setText(getText(R.string.person_info));
    311                     break;
    312                 case 5:
    313                     tvs[i].setText(getText(R.string.system_info));
    314                     break;
    315                 case 4:
    316                     tvs[i].setText(getText(R.string.hidden_danger_report));
    317                     break;
    318                 case 3:
    319                     tvs[i].setText(getText(R.string.open));
    320                     break;
    321                 case 2:
    322                     tvs[i].setText(getText(R.string.qr_code));
    323                     break;
    324                 case 1:
    325                     tvs[i].setText(getText(R.string.work_order));
    326                     break;
    327                 case 0:
    328                     tvs[i].setText(getText(R.string.routing_inspection));
    329                     break;
    330             }
    331 
    332 
    333         }
    334     }
    335     /**
    336      * 生成被拖动控件的preView
    337      */
    338     private void startDrag(Bitmap bm, int x, int y)
    339     {
    340         Log.v("startDrag>>>>>>", "startDrag");
    341         stopDrag();
    342         windowParams = new WindowManager.LayoutParams();
    343         windowParams.gravity = Gravity.TOP | Gravity.LEFT;
    344 
    345         //左上角相对于屏幕的坐标
    346         windowParams.x = x - dragPointX + dragOffsetX;
    347         windowParams.y = y - dragPointY + dragOffsetY - 40;
    348 
    349         windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
    350         windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
    351         windowParams.alpha = 0.5f;
    352 
    353         ImageView iv = new ImageView(myActivity);
    354         iv.setImageBitmap(bm);
    355         windowManager = (WindowManager) myActivity.getSystemService(Context.WINDOW_SERVICE);
    356         windowManager.addView(iv, windowParams);
    357 
    358         dragImageView = iv;
    359     }
    360 
    361     /**
    362      * 停止绘制,清空preView
    363      */
    364     private void stopDrag()
    365     {
    366         if (dragImageView != null)
    367         {
    368             windowManager.removeView(dragImageView);
    369             dragImageView = null;
    370             Log.v("StopDrag>>>>>>>", "disappear");
    371         }
    372     }
    373 
    374     /**
    375      * 拖动(Move)过程中不断调整preView的位置,以呈现拖动的效果
    376      */
    377     private void onDrag(int x, int y) {
    378         Log.v("onDrag>>>>>>", "onDrag");
    379         if (dragImageView != null) {
    380             windowParams.alpha = 0.5f;
    381 
    382             windowParams.x = x - dragPointX + dragOffsetX;
    383             windowParams.y = y - dragPointY + dragOffsetY - 40;
    384 
    385             Log.v("x", String.valueOf(x));
    386             Log.v("y", String.valueOf(y));
    387             Log.v("windowParams.x", String.valueOf(windowParams.x));
    388             Log.v("windowParams.y", String.valueOf(windowParams.y));
    389             windowManager.updateViewLayout(dragImageView, windowParams);
    390         }
    391     }
    392 
    393     /**
    394      * 返回参数View的相对屏幕的绝对坐标值
    395      * point[0]用于记录左上角的X
    396      * point[1]用于记录左上角的Y
    397      * point[2]用于记录右下角的X
    398      * point[3]用于记录右下角的Y
    399      */
    400     private int[] getPoint(View v){
    401         int point[] = new int[4];
    402         v.getLocationOnScreen(point);
    403         point[2] = point[0] + v.getWidth();
    404         point[3] = point[1] + v.getHeight();
    405         for(int i = 0; i < point.length; i++){
    406             System.out.println("point[" + i + "]::::::" + point[i]);
    407         }
    408 
    409         return point;
    410     }
    411     /**
    412      * 返回被拖动View的中心的坐标
    413      */
    414     private int[] getCentroPoint(View v){
    415         int point[] = new int[2];
    416         if(v != null){
    417             point = new int[2];
    418             v.getLocationOnScreen(point);
    419             point[0] = point[0] + v.getWidth()/2;
    420             point[1] = point[1] + v.getHeight()/2;
    421             for(int i = 0; i < point.length; i++){
    422                 System.out.println("point[" + i + "]::::::" + point[i]);
    423             }
    424         }
    425         return point;
    426     }
    427 
    428     /**
    429      * 判断被拖动的View的中心点在哪个View内部,就把哪个View隐藏
    430      */
    431     private void hide(){
    432         int point[] = getCentroPoint(dragImageView);
    433         for(int i = 0; i < views.length; i++){
    434             if(point[0] > points[i][0] && point[1] > points[i][1] && point[0] < points[i][2] && point[1] < points[i][3]){
    435                 if(views[i].isShown()){
    436                     toPoint = i;
    437                     //temp_view_img_topoint = views[i].getBackground();
    438 
    439                     onExchange();//将被拖拽的View的原始位置的数据设置为被覆盖的View的数据
    440 
    441                     views[i].setVisibility(View.INVISIBLE);
    442                 }
    443 //                if(views[i].getAnimation() == null){
    444 //                    views[i].startAnimation(blink);
    445 //                    toPoint = i;
    446 //                }
    447             }else{
    448 //                if(views[i].getAnimation() != null && !views[i].getAnimation().equals(original)){
    449 //                    views[i].startAnimation(original);
    450 //                }
    451 //                views[i].clearAnimation();
    452 //                views[i].invalidate();
    453 //                toPoint = -1;
    454                 if(toPoint == i){
    455                     toPoint = -1;
    456                     onExchange();//将被拖拽的View的原始位置的数据恢复初始数据
    457                 }
    458                 if(!views[i].isShown()){
    459                     //Toast.makeText(this,i+"--",Toast.LENGTH_LONG).show();
    460                     views[i].setVisibility(View.VISIBLE);
    461                     //views[i].setBackground(temp_view_img_topoint);
    462                 }
    463             }
    464         }
    465         //temp.requestDisallowInterceptTouchEvent(false); //恢复 viewPager 的可侧滑
    466     }
    467     /**
    468      * 拖动结束,将所有的View都显示出来
    469      */
    470     private void show(){
    471         for(int i = 0; i < views.length; i++){
    472             if(!views[i].isShown()){
    473 //                toPoint = i;
    474                 views[i].setVisibility(View.VISIBLE);
    475                 views[i].startAnimation(flash);
    476             }
    477             views[i].setVisibility(View.VISIBLE);
    478 //            if(views[i].getAnimation() != null && !views[i].getAnimation().equals(flash)){
    479 //                views[i].startAnimation(flash);
    480 //            }
    481         }
    482     }
    483     /**
    484      * 初始化所有View的坐标并存放到8行4列的二维数组point[8][4]
    485      */
    486     private void initPoints(){
    487         for(int i = 0; i < points.length; i++){
    488             points[i] = getPoint(views[i]);
    489         }
    490     }
    491     /**
    492      * 拖拽结束,将fromView和toView的内容进行交换
    493      * @return 交换返回true,不交换返回false
    494      */
    495     private boolean exchange(){
    496         if(fromPoint != -1 && toPoint != -1 && fromPoint != toPoint ){
    497             tvs[fromPoint].setText(tvs[toPoint].getText());
    498             //ivs[fromPoint].setImageDrawable(ivs[toPoint].getDrawable());
    499             views[fromPoint].setBackground(views[toPoint].getBackground());
    500 
    501             tvs[toPoint].setText(temp_str);
    502             //ivs[toPoint].setImageDrawable(temp_img);
    503             views[toPoint].setBackground(temp_view_img);
    504 
    505             views[fromPoint].startAnimation(flash);
    506 //            temp = toPoint;
    507             fromPoint = -1;
    508             toPoint = -1;
    509             return true;
    510         }else{
    511             fromPoint = -1;
    512             toPoint = -1;
    513             return false;
    514         }
    515 
    516     }
    517     /**
    518      * 用于正在被拖动View的原始位置的内容的实时交换
    519      */
    520     private void onExchange(){
    521         if(fromPoint != -1 && toPoint != -1 && fromPoint != toPoint ){
    522 
    523             //将被拖拽的View的原始位置的数据, 设置为, 被覆盖的View的数据
    524             //Toast.makeText(this,fromPoint+"--"+toPoint,Toast.LENGTH_LONG).show();
    525             tvs[fromPoint].setText(tvs[toPoint].getText());
    526             //ivs[fromPoint].setImageDrawable(ivs[toPoint].getDrawable());
    527             //views[fromPoint].setBackgroundResource(views[fromPoint].getDrawable(R.drawable.two));
    528 
    529             // 不适用 getBackground() 传入,会造成大图失真
    530             views[fromPoint].setBackground(getResources().getDrawable(relativeLayout_bgs[toPoint]));//实现拖曳过程中的实时互换
    531             //views[toPoint].setBackground(views[fromPoint].getBackground());
    532 
    533             views[fromPoint].startAnimation(blink);
    534 
    535         }else{
    536             //Toast.makeText(this,fromPoint+"--"+toPoint,Toast.LENGTH_LONG).show();
    537             //将被拖拽的View的原始位置的数据恢复初始数据
    538             tvs[fromPoint].setText(temp_str);
    539             //ivs[fromPoint].setImageDrawable(temp_img);
    540             views[fromPoint].setBackground(temp_view_img);
    541         }
    542     }
    543     /**
    544      * 用来显示更新bottom_bar中的数据
    545      */
    546     private void updateBottomBar(int sendPackageNo, int rate, double longitude, double dimension){
    547         //在这里可以设置 footer 文本
    548     }
    549 
    550 
    551 
    552     @Override
    553     public boolean onOptionsItemSelected(MenuItem item) {
    554         SharedPreferences preferences = myActivity.getSharedPreferences("system_config", myActivity.MODE_PRIVATE);
    555         boolean isChanged = preferences.getBoolean("theme_value", true);
    556         SharedPreferences.Editor editor = preferences.edit();
    557         switch (item.getItemId()) {
    558             case R.id.menu_settings: {
    559 
    560                 if (isChanged) {
    561                     isChanged = false;
    562                 } else {
    563                     isChanged = true;
    564                 }
    565                 editor.putBoolean("theme_value", isChanged);
    566                 editor.commit();
    567                 break;
    568             }
    569         }
    570         return super.onOptionsItemSelected(item);
    571     }
    572 
    573 
    574 }

    打完收工,有问题请留言。

  • 相关阅读:
    Leetcode 283. Move Zeroes
    利用脚本,一键设置java环境变量(默认安装路径)
    'wmic' 不是内部或外部命令,也不是可运行的程序 解决方法
    PowerShell因为在此系统中禁止执行脚本解决方法
    使用VBSCRIPT安装字体
    批处理基本知识以及进阶 V2.0
    Vbs 脚本编程简明教程之一
    用批处理,批量安装字体文件 (Erector.bat)
    windows 7 系统装机优化
    使用批处理替换windows系统中的hosts文件
  • 原文地址:https://www.cnblogs.com/linguanh/p/4540789.html
Copyright © 2011-2022 走看看