【自定义之前】自定义之前:onTouchEvent

来源:互联网 发布:微信一键转发软件下载 编辑:程序博客网 时间:2024/06/06 14:11
写在自定义之前


我们也许会遇到,自定义控件的触屏事件处理,先来了解一下View类中的,onTouch事件和onTouchEvent事件。
1、boolean onTouch(View v, MotionVent event)
触摸事件发送到视图时调用(v:视图,event:触摸事件)
返回true:事件被完全消耗(即,从down事件开始,触发move,up所有的事件)
返回fasle:事件未被完全消耗(即,只会消耗掉down事件)
2、boolean onTouchEvent(MotionEvent event)
触摸屏幕时调用
返回值,同上

须知
1、onTouch优先级比onTouchEvent高
2、如果button设置了onTouchListener监听,onTouch方法返回了true,就不会调用这个button的Click事件



运用onTouchEvent写一个能滑动的布局


需求:
1.刚进入界面外层布局,自动下滑一段距离,露出内层布局。
2.外层布局可以上下滑动,并且带有透明度渐变效果,改变内边距效果。
需求分析:
1.显然,外层布局要默认覆盖内层布局了,这个容易。自动下滑,要用到动画,ObjectAnimator
2.外层布局要实现上下滑动,那么需要自定义,对onTouchEvent重写(核心逻辑)
也许描述的有一些模糊,看一下效果图:

效果图:


下面是源码,demo链接地址:http://download.csdn.net/detail/qq_29266921/9776565

[java] view plain copy
  1. /** 
  2.  * Author:Biligle. 
  3.  * 自定义布局 
  4.  */  
  5.   
  6. public class MyViewGroup extends ViewGroup {  
  7.   
  8.     private MyViewGroupListener listener;//接口,监听滑动事件  
  9.     private int vertical = 0;//布局距离顶端距离(默认0)  
  10.   
  11.     public MyViewGroup(Context context) {  
  12.         super(context);  
  13.     }  
  14.   
  15.     public MyViewGroup(Context context, AttributeSet attrs) {  
  16.         super(context, attrs);  
  17.     }  
  18.   
  19.     public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {  
  20.         super(context, attrs, defStyleAttr);  
  21.     }  
  22.   
  23.     @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)  
  24.     public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {  
  25.         super(context, attrs, defStyleAttr, defStyleRes);  
  26.     }  
  27.   
  28.   
  29.     private int downY = 0;//按下时的点  
  30.     private int slide = 0;//最终移动距离  
  31.     @Override  
  32.     public boolean onTouchEvent(MotionEvent event) {  
  33.         switch (event.getAction()){  
  34.             case MotionEvent.ACTION_DOWN:  
  35.                 downY = (int) event.getY();  
  36.                 break;  
  37.             case MotionEvent.ACTION_MOVE:  
  38.                 slide = downY - (int)event.getY();  
  39.                 if(slide < 0){//下滑  
  40.                     vertical = listener.marginTop(Math.abs(slide));  
  41.                 }else if(slide > 0){//上滑  
  42.                     vertical = listener.marginTop(-slide);  
  43.                 }  
  44.                 break;  
  45.             case MotionEvent.ACTION_UP:  
  46.                 if(vertical < 300){  
  47.                     //布局距离屏幕顶部小于300,就让布局充满整个屏幕  
  48.                     vertical = listener.marginTop(0);  
  49.                 }  
  50.                 break;  
  51.         }  
  52.         return true;  
  53.     }  
  54.   
  55.     /** 
  56.      * 测量子View 
  57.      * @param widthMeasureSpec 
  58.      * @param heightMeasureSpec 
  59.      */  
  60.     @Override  
  61.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  62.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  63.         for (int i = 0; i < getChildCount(); i++) {  
  64.             View child = getChildAt(i);  
  65.             //系统测量  
  66.             measureChild(child, widthMeasureSpec, heightMeasureSpec);  
  67.         }  
  68.     }  
  69.   
  70.     /** 
  71.      * 安排子View的位置 
  72.      * @param changed 
  73.      * @param l 左边距 
  74.      * @param t 上边距 
  75.      * @param r 右边距 
  76.      * @param b 下边距 
  77.      */  
  78.     @Override  
  79.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  80.         int left = 0, top = 0, right = 0, bottom = 0;  
  81.         for (int i = 0; i < getChildCount(); i++) {  
  82.             View child = getChildAt(i);  
  83.             right = left + child.getMeasuredWidth();  
  84.             bottom = top + child.getMeasuredHeight();  
  85.             child.layout(left, top, right, bottom);  
  86.         }  
  87.     }  
  88.   
  89.     public void setListener(MyViewGroupListener listener){  
  90.         this.listener = listener;  
  91.     }  
  92.   
  93.     interface MyViewGroupListener {  
  94.         /** 
  95.          * 设置topMargin,上下滑动时触发 
  96.          * @param slide 滑动距离 
  97.          * @return 当前上边距 
  98.          */  
  99.         int marginTop(int slide);  
  100.     }  
  101. }  


[java] view plain copy
  1. public class MainActivity extends AppCompatActivity implements MyViewGroup.MyViewGroupListener{  
  2.   
  3.     /** 自定义布局(外层布局)*/  
  4.     private MyViewGroup myViewGroup;  
  5.     /** 两个圆形图(在外层布局)*/  
  6.     private ImageView iv1,iv2/*,cloud*/;  
  7.     /** 包裹圆形图的布局*/  
  8.     private RelativeLayout relativeLayout;  
  9.     /** 外层布局参数类(这里用到了params.topMargin:上边距)*/  
  10.     private ViewGroup.MarginLayoutParams params;  
  11.     /** 透明值(改变两个圆图的透明值)*/  
  12.     private float f;  
  13.     /** 左右内边距(改变RelativeLayout内边距)*/  
  14.     private int p;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.         myViewGroup = (MyViewGroup) findViewById(R.id.my);  
  21.         myViewGroup.setListener(this);  
  22.         iv1 = (ImageView) findViewById(R.id.iv1);  
  23.         iv2 = (ImageView) findViewById(R.id.iv2);  
  24.         relativeLayout = (RelativeLayout) findViewById(R.id.relative);  
  25.         params = (ViewGroup.MarginLayoutParams) myViewGroup.getLayoutParams();  
  26.         //初始化动画(自动下滑一段儿距离),我这里写死了900  
  27.         ObjectAnimator animator = ObjectAnimator.ofFloat(myViewGroup,"translationY"900);  
  28.         animator.setDuration(2000);  
  29.         animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {  
  30.             @Override  
  31.             public void onAnimationUpdate(ValueAnimator animation) {  
  32.                 float y = (float)animation.getAnimatedValue();  
  33.                 f = y/800;  
  34.                 p = (int) y/3;  
  35.                 alpha(f);  
  36.                 padding(p);  
  37.             }  
  38.         });  
  39.         animator.start();  
  40. //        cloud = (ImageView) findViewById(R.id.cloud);  
  41.     }  
  42.   
  43.   
  44.     /** 
  45.      * 设置上边距 
  46.      * @param slide 滑动距离 
  47.      * @return 返回下滑布局,距离屏幕左上角的垂直距离 
  48.      */  
  49.     @Override  
  50.     public int marginTop(int slide) {  
  51.         params.topMargin += slide;  
  52.         myViewGroup.setLayoutParams(params);  
  53.         int vertical = (900 + params.topMargin);  
  54.         if(slide == 0){  
  55.             //为了隐藏两张圆图,所以把Relativelayout的高度一并减除。  
  56.             params.topMargin -= (vertical+relativeLayout.getHeight());  
  57.             myViewGroup.setLayoutParams(params);  
  58.         }  
  59.         float alpha = f + (float) params.topMargin/800;//自定义一个算法  
  60.         alpha(alpha);  
  61.         int padding = p + params.topMargin/3;//自定义一个算法  
  62.         padding(padding);  
  63.         return vertical;  
  64.     }  
  65.   
  66.     /** 
  67.      * 设置透明度 
  68.      * @param alpha 透明值 
  69.      */  
  70.     public void alpha(float alpha) {  
  71.         iv1.setAlpha(alpha);  
  72.         iv2.setAlpha(alpha);  
  73.     }  
  74.   
  75.     /** 
  76.      * 设置左右边距 
  77.      * @param padding 边距值 
  78.      */  
  79.     public void padding(int padding) {  
  80.         relativeLayout.setPadding(padding, 0, padding, 0);  
  81.     }  


[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  5.     xmlns:tools="http://schemas.android.com/tools"  
  6.     android:id="@+id/activity_main"  
  7.     android:layout_width="match_parent"  
  8.     android:layout_height="match_parent"  
  9.     tools:context="com.wgl.viewgroup1.MainActivity">  
  10.     <ImageView  
  11.         android:id="@+id/iv"  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="match_parent"  
  14.         android:src="@mipmap/pic"  
  15.         android:scaleType="fitXY"/>  
  16.         <!--<ImageView-->  
  17.             <!--android:id="@+id/cloud"-->  
  18.             <!--android:layout_width="wrap_content"-->  
  19.             <!--android:layout_height="wrap_content"-->  
  20.             <!--android:layout_centerHorizontal="true"-->  
  21.             <!--android:alpha="0.8"-->  
  22.             <!--android:src="@mipmap/cloud3"-->  
  23.             <!--android:clickable="true"/>-->  
  24.   
  25.     <com.wgl.viewgroup1.MyViewGroup  
  26.         android:id="@+id/my"  
  27.         android:layout_width="match_parent"  
  28.         android:layout_height="match_parent"  
  29.         android:alpha="0.8"  
  30.         android:layout_alignParentTop="true">  
  31.         <LinearLayout  
  32.             android:layout_width="match_parent"  
  33.             android:layout_height="match_parent"  
  34.             android:orientation="vertical">  
  35.             <RelativeLayout  
  36.                 android:id="@+id/relative"  
  37.                 android:layout_width="match_parent"  
  38.                 android:layout_height="wrap_content"  
  39.                 android:orientation="horizontal">  
  40.                 <com.wgl.viewgroup1.CircleImageView  
  41.                     android:id="@+id/iv1"  
  42.                     android:layout_width="wrap_content"  
  43.                     android:layout_height="wrap_content"  
  44.                     android:src="@mipmap/iv1"  
  45.                     app:civ_border_width="2dp"  
  46.                     app:civ_border_color="@color/colorAccent"  
  47.                     android:layout_alignParentLeft="true"/>  
  48.   
  49.                 <com.wgl.viewgroup1.CircleImageView  
  50.                     android:id="@+id/iv2"  
  51.                     android:layout_width="wrap_content"  
  52.                     android:layout_height="wrap_content"  
  53.                     android:src="@mipmap/iv2"  
  54.                     app:civ_border_width="2dp"  
  55.                     app:civ_border_color="@color/colorAccent"  
  56.                     android:layout_alignParentRight="true"/>  
  57.             </RelativeLayout>  
  58.             <LinearLayout  
  59.                 android:layout_width="match_parent"  
  60.                 android:layout_height="match_parent"  
  61.                 android:alpha="0.8"  
  62.                 android:background="@color/colorPrimary">  
  63.   
  64.             </LinearLayout>  
  65.         </LinearLayout>  
  66.   
  67.     </com.wgl.viewgroup1.MyViewGroup>  
  68. </RelativeLayout>  
阅读全文
1 0
原创粉丝点击