Android運(yùn)用onTouchEvent自定義滑動布局
寫在自定義之前
我們也許會遇到,自定義控件的觸屏事件處理,先來了解一下View類中的,onTouch事件和onTouchEvent事件。
1、boolean onTouch(View v, MotionVent event)
觸摸事件發(fā)送到視圖時調(diào)用(v:視圖,event:觸摸事件)
返回true:事件被完全消耗(即,從down事件開始,觸發(fā)move,up所有的事件)
返回fasle:事件未被完全消耗(即,只會消耗掉down事件)
2、boolean onTouchEvent(MotionEvent event)
觸摸屏幕時調(diào)用
返回值,同上
須知
1、onTouch優(yōu)先級比onTouchEvent高
2、如果button設(shè)置了onTouchListener監(jiān)聽,onTouch方法返回了true,就不會調(diào)用這個button的Click事件
運(yùn)用onTouchEvent寫一個能滑動的布局
需求:
1.剛進(jìn)入界面外層布局,自動下滑一段距離,露出內(nèi)層布局。
2.外層布局可以上下滑動,并且?guī)в型该鞫葷u變效果,改變內(nèi)邊距效果。
需求分析:
1.顯然,外層布局要默認(rèn)覆蓋內(nèi)層布局了,這個容易。自動下滑,要用到動畫,ObjectAnimator
2.外層布局要實(shí)現(xiàn)上下滑動,那么需要自定義,對onTouchEvent重寫(核心邏輯)
也許描述的有一些模糊,看一下效果圖:
效果圖:
下面是源碼,demo鏈接地址
/** * Author:Biligle. * 自定義布局 */ public class MyViewGroup extends ViewGroup { private MyViewGroupListener listener;//接口,監(jiān)聽滑動事件 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;//按下時的點(diǎn) private int slide = 0;//最終移動距離 @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,就讓布局充滿整個屏幕 vertical = listener.marginTop(0); } break; } return true; } /** * 測量子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)測量 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,上下滑動時觸發(fā) * @param slide 滑動距離 * @return 當(dāng)前上邊距 */ int marginTop(int slide); } }
public class MainActivity extends AppCompatActivity implements MyViewGroup.MyViewGroupListener{ /** 自定義布局(外層布局)*/ private MyViewGroup myViewGroup; /** 兩個圓形圖(在外層布局)*/ private ImageView iv1,iv2/*,cloud*/; /** 包裹圓形圖的布局*/ private RelativeLayout relativeLayout; /** 外層布局參數(shù)類(這里用到了params.topMargin:上邊距)*/ private ViewGroup.MarginLayoutParams params; /** 透明值(改變兩個圓圖的透明值)*/ 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(); //初始化動畫(自動下滑一段兒距離),我這里寫死了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 滑動距離 * @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;//自定義一個算法 alpha(alpha); int padding = p + params.topMargin/3;//自定義一個算法 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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Andriod 讀取網(wǎng)絡(luò)圖片實(shí)例代碼解析
Android手機(jī)上,我們經(jīng)常用imageview顯示圖片,通過本文學(xué)習(xí)獲取網(wǎng)絡(luò)圖片并顯示在imageview中,對android讀取網(wǎng)絡(luò)圖片相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-02-02Android 個人理財(cái)工具五:顯示賬單明細(xì) 上
本文主要介紹 Android 個人理財(cái)工具顯示賬單明細(xì),這里提供了示例代碼,和實(shí)現(xiàn)效果圖,幫助大家學(xué)習(xí)理解ListView的用法,有興趣的小伙伴可以參考下2016-08-08Android 實(shí)現(xiàn)定時任務(wù)的過程詳解
這篇文章主要介紹了Android 定時任務(wù)過程詳解的相關(guān)資料,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11Android實(shí)現(xiàn)調(diào)取支付寶健康碼
大家好,本篇文章主要講的是Android實(shí)現(xiàn)調(diào)取支付寶健康碼,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01Android編程之九宮格實(shí)現(xiàn)方法實(shí)例分析
這篇文章主要介紹了Android編程之九宮格實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android九宮格的實(shí)現(xiàn)方法與具體步驟,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-01-01Android AndBase框架實(shí)現(xiàn)多功能標(biāo)題欄(一)
這篇文章主要整理了Android AndBase框架學(xué)習(xí)筆記,本文主要使用AndBase實(shí)現(xiàn)多功能標(biāo)題欄,感興趣的小伙伴們可以參考一下2016-03-03Android開發(fā)中如何去掉app標(biāo)題欄的實(shí)現(xiàn)
這篇文章主要介紹了Android開發(fā)中如何去掉app標(biāo)題欄的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Android自定義View實(shí)現(xiàn)兩種二維碼的掃描效果
這篇文章主要為大家詳細(xì)介紹了Android如何自定義View實(shí)現(xiàn)兩種二維碼的掃描效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01Android 調(diào)用系統(tǒng)應(yīng)用的方法總結(jié)
這篇文章主要介紹了Android 調(diào)用系統(tǒng)應(yīng)用的方法總結(jié)的相關(guān)資料,這里提供調(diào)用錄像,錄音,拍照等功能,需要的朋友可以參考下2017-08-08