亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android運(yùn)用onTouchEvent自定義滑動(dòng)布局

 更新時(shí)間:2017年03月21日 14:22:47   作者:Biligle  
這篇文章主要為大家詳細(xì)介紹了Android運(yùn)用onTouchEvent寫一個(gè)上下滑動(dòng)的布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

寫在自定義之前

我們也許會(huì)遇到,自定義控件的觸屏事件處理,先來(lái)了解一下View類中的,onTouch事件和onTouchEvent事件。

1、boolean onTouch(View v, MotionVent event)

觸摸事件發(fā)送到視圖時(shí)調(diào)用(v:視圖,event:觸摸事件)
返回true:事件被完全消耗(即,從down事件開(kāi)始,觸發(fā)move,up所有的事件)
返回fasle:事件未被完全消耗(即,只會(huì)消耗掉down事件)

2、boolean onTouchEvent(MotionEvent event)

觸摸屏幕時(shí)調(diào)用
返回值,同上

須知

1、onTouch優(yōu)先級(jí)比onTouchEvent高
2、如果button設(shè)置了onTouchListener監(jiān)聽(tīng),onTouch方法返回了true,就不會(huì)調(diào)用這個(gè)button的Click事件

運(yùn)用onTouchEvent寫一個(gè)能滑動(dòng)的布局

需求:

1.剛進(jìn)入界面外層布局,自動(dòng)下滑一段距離,露出內(nèi)層布局。
2.外層布局可以上下滑動(dòng),并且?guī)в型该鞫葷u變效果,改變內(nèi)邊距效果。

需求分析:

1.顯然,外層布局要默認(rèn)覆蓋內(nèi)層布局了,這個(gè)容易。自動(dòng)下滑,要用到動(dòng)畫,ObjectAnimator
2.外層布局要實(shí)現(xiàn)上下滑動(dòng),那么需要自定義,對(duì)onTouchEvent重寫(核心邏輯)
也許描述的有一些模糊,看一下效果圖:

效果圖:

下面是源碼,demo鏈接地址

/** 
 * Author:Biligle. 
 * 自定義布局 
 */ 
 
public class MyViewGroup extends ViewGroup { 
 
 private MyViewGroupListener listener;//接口,監(jiān)聽(tīng)滑動(dòng)事件 
 private int vertical = 0;//布局距離頂端距離(默認(rèn)0) 
 
 public MyViewGroup(Context context) { 
 super(context); 
 } 
 
 public MyViewGroup(Context context, AttributeSet attrs) { 
 super(context, attrs); 
 } 
 
 public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { 
 super(context, attrs, defStyleAttr); 
 } 
 
 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 
 public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
 super(context, attrs, defStyleAttr, defStyleRes); 
 } 
 
 
 private int downY = 0;//按下時(shí)的點(diǎn) 
 private int slide = 0;//最終移動(dòng)距離 
 @Override 
 public boolean onTouchEvent(MotionEvent event) { 
 switch (event.getAction()){ 
  case MotionEvent.ACTION_DOWN: 
  downY = (int) event.getY(); 
  break; 
  case MotionEvent.ACTION_MOVE: 
  slide = downY - (int)event.getY(); 
  if(slide < 0){//下滑 
   vertical = listener.marginTop(Math.abs(slide)); 
  }else if(slide > 0){//上滑 
   vertical = listener.marginTop(-slide); 
  } 
  break; 
  case MotionEvent.ACTION_UP: 
  if(vertical < 300){ 
   //布局距離屏幕頂部小于300,就讓布局充滿整個(gè)屏幕 
   vertical = listener.marginTop(0); 
  } 
  break; 
 } 
 return true; 
 } 
 
 /** 
 * 測(cè)量子View 
 * @param widthMeasureSpec 
 * @param heightMeasureSpec 
 */ 
 @Override 
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
 for (int i = 0; i < getChildCount(); i++) { 
  View child = getChildAt(i); 
  //系統(tǒng)測(cè)量 
  measureChild(child, widthMeasureSpec, heightMeasureSpec); 
 } 
 } 
 
 /** 
 * 安排子View的位置 
 * @param changed 
 * @param l 左邊距 
 * @param t 上邊距 
 * @param r 右邊距 
 * @param b 下邊距 
 */ 
 @Override 
 protected void onLayout(boolean changed, int l, int t, int r, int b) { 
 int left = 0, top = 0, right = 0, bottom = 0; 
 for (int i = 0; i < getChildCount(); i++) { 
  View child = getChildAt(i); 
  right = left + child.getMeasuredWidth(); 
  bottom = top + child.getMeasuredHeight(); 
  child.layout(left, top, right, bottom); 
 } 
 } 
 
 public void setListener(MyViewGroupListener listener){ 
 this.listener = listener; 
 } 
 
 interface MyViewGroupListener { 
 /** 
  * 設(shè)置topMargin,上下滑動(dòng)時(shí)觸發(fā) 
  * @param slide 滑動(dòng)距離 
  * @return 當(dāng)前上邊距 
  */ 
 int marginTop(int slide); 
 } 
}
public class MainActivity extends AppCompatActivity implements MyViewGroup.MyViewGroupListener{ 
 
 /** 自定義布局(外層布局)*/ 
 private MyViewGroup myViewGroup; 
 /** 兩個(gè)圓形圖(在外層布局)*/ 
 private ImageView iv1,iv2/*,cloud*/; 
 /** 包裹圓形圖的布局*/ 
 private RelativeLayout relativeLayout; 
 /** 外層布局參數(shù)類(這里用到了params.topMargin:上邊距)*/ 
 private ViewGroup.MarginLayoutParams params; 
 /** 透明值(改變兩個(gè)圓圖的透明值)*/ 
 private float f; 
 /** 左右內(nèi)邊距(改變RelativeLayout內(nèi)邊距)*/ 
 private int p; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 myViewGroup = (MyViewGroup) findViewById(R.id.my); 
 myViewGroup.setListener(this); 
 iv1 = (ImageView) findViewById(R.id.iv1); 
 iv2 = (ImageView) findViewById(R.id.iv2); 
 relativeLayout = (RelativeLayout) findViewById(R.id.relative); 
 params = (ViewGroup.MarginLayoutParams) myViewGroup.getLayoutParams(); 
 //初始化動(dòng)畫(自動(dòng)下滑一段兒距離),我這里寫死了900 
 ObjectAnimator animator = ObjectAnimator.ofFloat(myViewGroup,"translationY", 900); 
 animator.setDuration(2000); 
 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
  @Override 
  public void onAnimationUpdate(ValueAnimator animation) { 
  float y = (float)animation.getAnimatedValue(); 
  f = y/800; 
  p = (int) y/3; 
  alpha(f); 
  padding(p); 
  } 
 }); 
 animator.start(); 
// cloud = (ImageView) findViewById(R.id.cloud); 
 } 
 
 
 /** 
 * 設(shè)置上邊距 
 * @param slide 滑動(dòng)距離 
 * @return 返回下滑布局,距離屏幕左上角的垂直距離 
 */ 
 @Override 
 public int marginTop(int slide) { 
 params.topMargin += slide; 
 myViewGroup.setLayoutParams(params); 
 int vertical = (900 + params.topMargin); 
 if(slide == 0){ 
  //為了隱藏兩張圓圖,所以把Relativelayout的高度一并減除。 
  params.topMargin -= (vertical+relativeLayout.getHeight()); 
  myViewGroup.setLayoutParams(params); 
 } 
 float alpha = f + (float) params.topMargin/800;//自定義一個(gè)算法 
 alpha(alpha); 
 int padding = p + params.topMargin/3;//自定義一個(gè)算法 
 padding(padding); 
 return vertical; 
 } 
 
 /** 
 * 設(shè)置透明度 
 * @param alpha 透明值 
 */ 
 public void alpha(float alpha) { 
 iv1.setAlpha(alpha); 
 iv2.setAlpha(alpha); 
 } 
 
 /** 
 * 設(shè)置左右邊距 
 * @param padding 邊距值 
 */ 
 public void padding(int padding) { 
 relativeLayout.setPadding(padding, 0, padding, 0); 
 } 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:app="http://schemas.android.com/apk/res-auto" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:id="@+id/activity_main" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 tools:context="com.wgl.viewgroup1.MainActivity"> 
 <ImageView 
 android:id="@+id/iv" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:src="@mipmap/pic" 
 android:scaleType="fitXY"/> 
 <!--<ImageView--> 
  <!--android:id="@+id/cloud"--> 
  <!--android:layout_width="wrap_content"--> 
  <!--android:layout_height="wrap_content"--> 
  <!--android:layout_centerHorizontal="true"--> 
  <!--android:alpha="0.8"--> 
  <!--android:src="@mipmap/cloud3"--> 
  <!--android:clickable="true"/>--> 
 
 <com.wgl.viewgroup1.MyViewGroup 
 android:id="@+id/my" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:alpha="0.8" 
 android:layout_alignParentTop="true"> 
 <LinearLayout 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical"> 
  <RelativeLayout 
  android:id="@+id/relative" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:orientation="horizontal"> 
  <com.wgl.viewgroup1.CircleImageView 
   android:id="@+id/iv1" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:src="@mipmap/iv1" 
   app:civ_border_width="2dp" 
   app:civ_border_color="@color/colorAccent" 
   android:layout_alignParentLeft="true"/> 
 
  <com.wgl.viewgroup1.CircleImageView 
   android:id="@+id/iv2" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:src="@mipmap/iv2" 
   app:civ_border_width="2dp" 
   app:civ_border_color="@color/colorAccent" 
   android:layout_alignParentRight="true"/> 
  </RelativeLayout> 
  <LinearLayout 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:alpha="0.8" 
  android:background="@color/colorPrimary"> 
 
  </LinearLayout> 
 </LinearLayout> 
 
 </com.wgl.viewgroup1.MyViewGroup> 
</RelativeLayout> 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論