本篇文章給大家分享的是有關怎么在Android應用中利用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重寫(核心邏輯)
代碼如下:
/**
* Author:Biligle.
* 自定義布局
*/
public class MyViewGroup extends ViewGroup {
private MyViewGroupListener listener;//接口,監聽滑動事件
private int vertical = 0;//布局距離頂端距離(默認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;//按下時的點
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);
//系統測量
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 {
/**
* 設置topMargin,上下滑動時觸發
* @param slide 滑動距離
* @return 當前上邊距
*/
int marginTop(int slide);
}
}public class MainActivity extends AppCompatActivity implements MyViewGroup.MyViewGroupListener{
/** 自定義布局(外層布局)*/
private MyViewGroup myViewGroup;
/** 兩個圓形圖(在外層布局)*/
private ImageView iv1,iv2/*,cloud*/;
/** 包裹圓形圖的布局*/
private RelativeLayout relativeLayout;
/** 外層布局參數類(這里用到了params.topMargin:上邊距)*/
private ViewGroup.MarginLayoutParams params;
/** 透明值(改變兩個圓圖的透明值)*/
private float f;
/** 左右內邊距(改變RelativeLayout內邊距)*/
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);
}
/**
* 設置上邊距
* @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;
}
/**
* 設置透明度
* @param alpha 透明值
*/
public void alpha(float alpha) {
iv1.setAlpha(alpha);
iv2.setAlpha(alpha);
}
/**
* 設置左右邊距
* @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>
以上就是怎么在Android應用中利用onTouchEvent實現一個滑動布局,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。