Android運(yùn)用onTouchEvent自定義滑動(dòng)布局
寫在自定義之前
我們也許會(huì)遇到,自定義控件的觸屏事件處理,先來了解一下View類中的,onTouch事件和onTouchEvent事件。
1、boolean onTouch(View v, MotionVent event)
觸摸事件發(fā)送到視圖時(shí)調(diào)用(v:視圖,event:觸摸事件)
返回true:事件被完全消耗(即,從down事件開始,觸發(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)聽,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)聽滑動(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)文章
Android自定義View基礎(chǔ)開發(fā)之圖片加載進(jìn)度條
這篇文章主要介紹了Android自定義View基礎(chǔ)開發(fā)之圖片加載進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android使用SqLite實(shí)現(xiàn)登錄注冊(cè)功能流程詳解
這篇文章主要介紹了使用Android Studio自帶的sqlite數(shù)據(jù)庫(kù)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的登錄注冊(cè)功能,SQLite是一個(gè)軟件庫(kù),實(shí)現(xiàn)了自給自足的、無服務(wù)器的、零配置的、事務(wù)性的SQL數(shù)據(jù)庫(kù)引擎,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
Android源碼學(xué)習(xí)之組合模式定義及應(yīng)用
將對(duì)象組合成樹形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu),使得用戶對(duì)單個(gè)對(duì)象和組合對(duì)象的使用具有一致性,需要了解的朋友可以參考下2013-01-01
Android開發(fā)中Looper.prepare()和Looper.loop()
Looper用于封裝了android線程中的消息循環(huán),默認(rèn)情況下一個(gè)線程是不存在消息循環(huán)(message loop)的,具體調(diào)用方法大家可以通過本文學(xué)習(xí)2016-11-11
Android基于widget組件實(shí)現(xiàn)物體移動(dòng)/控件拖動(dòng)功能示例
這篇文章主要介紹了Android基于widget組件實(shí)現(xiàn)物體移動(dòng)/控件拖動(dòng)功能,結(jié)合實(shí)例形式分析了widget組件在桌面應(yīng)用中的事件響應(yīng)與屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10
Android 獲取正在運(yùn)行的任務(wù)和服務(wù)的小例子
Android 獲取正在運(yùn)行的任務(wù)和服務(wù)的小例子,需要的朋友可以參考一下2013-05-05
Android的WebView與H5前端JS代碼交互的實(shí)例代碼
本篇文章主要介紹了Android的WebView與H5前端JS代碼交互的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-07-07
Android中View的炸裂特效實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Android中View的炸裂特效實(shí)現(xiàn)方法,涉及Android組件ExplosionField的相關(guān)定義與使用技巧,需要的朋友可以參考下2016-07-07

