左右手势划屏就可以实现以上专辑图片和歌词之间视图的切换
实现步骤:
1、 自定义划屏类
package com.wwj.sb.custom;
import com.wwj.sb.activity.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.Scroller;
public class FlingGalleryView extends ViewGroup {
private static final int SNAP_VELOCITY = 1000;
// 记录当前屏幕下标,取值范围是:0 到 getChildCount()-1
private int mCurrentScreen;
private Scroller mScroller;
// 速度追踪器,主要是为了通过当前滑动速度判断当前滑动是否为fling
private VelocityTracker mVelocityTracker;
// 记录滑动时上次手指所处的位置
private float mLastMotionX;
private float mLastMotionY;
// Touch状态值 0:静止 1:滑动
private final static int TOUCH_STATE_REST = 0;
private final static int TOUCH_STATE_SCROLLING = 1;
// 记录当前touch事件状态–滑动(TOUCH_STATE_SCROLLING)、静止(TOUCH_STATE_REST 默认)
private int mTouchState = TOUCH_STATE_REST;
// 记录touch事件中被认为是滑动事件前的最大可滑动距离
private int mTouchSlop;
// 手指抛动作的最大速度px/s 每秒多少像素
private int mMaximumVelocity;
// 滚动到指定屏幕的事件
private OnScrollToScreenListener mScrollToScreenListener;
// 自定义touch事件
private OnCustomTouchListener mCustomTouchListener;
//滚动到每个屏幕时是否都要触发OnScrollToScreenListener事件
private boolean isEveryScreen=false;
public FlingGalleryView(Context context) {
super(context);
init();
mCurrentScreen = 0;
}
public FlingGalleryView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FlingGalleryView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.FlingGalleryView, defStyle, 0);
mCurrentScreen = a.getInt(R.styleable.FlingGalleryView_defaultScreen, 0);
a.recycle();
init();
}
private void init() {
mScroller = new Scroller(getContext());
final ViewConfiguration configuration = ViewConfiguration
.get(getContext());
mTouchSlop = configuration.getScaledTouchSlop();
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
}
// 保证在同一个屏幕执行一下切屏事件的一些参数
private int count = -1;
private int defaultScreen = -1;
// 当滚动条滑动时调用,startScroll()设置的是参数,实际滑动,在其里执行,
@Override
public void computeScroll() {
// mScroller.computeScrollOffset计算当前新的位置,true表示还在滑动,仍需计算
if (mScroller.computeScrollOffset()) {
// 返回true,说明scroll还没有停止
scrollTo(mScroller.getCurrX(), 0);
if(isEveryScreen)singleScrollToScreen();
postInvalidate();
}
}
// 保证在同一个屏幕执行一下切屏事件
private void singleScrollToScreen() {
final int screenWidth = getWidth();
int whichScreen = (getScrollX() + (screenWidth / 2)) / screenWidth;
if (whichScreen > (getChildCount() - 1)) {
return;
}
if (defaultScreen == -1) {
defaultScreen = whichScreen;
count = 1;
} else {
if (defaultScreen == whichScreen && count == 0) {
count = 1;
} else {
if (defaultScreen != whichScreen) {
defaultScreen = whichScreen;
count = 0;
}
}
}
if (count == 0) {
if (mScrollToScreenListener != null) {
mScrollToScreenListener.operation(whichScreen, getChildCount());
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int width = MeasureSpec.getSize(widthMeasureSpec);
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
if (widthMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException(
“Workspace can only be used in EXACTLY mode.”);
}
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException(
“Workspace can only be used in EXACTLY mode.”);
}
final int count = getChildCount();
for (int i = 0; i < count; i++) {
getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
}
scrollTo(mCurrentScreen * width, 0);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
int childLeft = 0;
// 横向平铺childView
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
child.setOnTouchListener(childTouchListener);
if (child.getVisibility() != View.GONE) {
final int childWidth = child.getMeasuredWidth();
child.layout(childLeft, 0, childLeft + childWidth,
child.getMeasuredHeight());
childLeft += childWidth;
}
}
}
// 设定childView的Touch事件返回true,这样可以在parentView中截获touch(即onInterceptTouchEvent)的move,up等事件
private OnTouchListener childTouchListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return true;
}
};
// 在系统向该ViewGroup及其各个childView触发onTouchEvent()之前对相关事件进行一次拦截
/*
-
down事件首先会传递到onInterceptTouchEvent()方法
-
如果该ViewGroup的onInterceptTouchEvent()在接收到down事件处理完成之后return
-
false,那么后续的move,
-
up等事件将继续会先传递给该ViewGroup,之后才和down事件一样传递给最终的目标view的onTouchEvent()处理。
-
如果该ViewGroup的onInterceptTouchEvent()在接收到down事件处理完成之后return
-
true,那么后续的move,
-
up等事件将不再传递给onInterceptTouchEvent(),而是和down事件一样传递给该ViewGroup的onTouchEvent
-
()处理,注意,目标view将接收不到任何事件。
-
如果最终需要处理事件的view的onTouchEvent()返回了false,那么该事件将被传递至其上一层次的view的onTouchEvent
-
()处理。 如果最终需要处理事件的view
-
的onTouchEvent()返回了true,那么后续事件将可以继续传递给该view的onTouchEvent()处理。
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (mCustomTouchListener != null) {
mCustomTouchListener.operation(ev);
}
final int action = ev.getAction();
if ((action == MotionEvent.ACTION_MOVE)
&& (mTouchState != TOUCH_STATE_REST)) {
return true;
}
final float x = ev.getX();
final float y = ev.getY();
switch (action) {
case MotionEvent.ACTION_MOVE:
// 计算X方向移动的距离
final int xDiff = (int) Math.abs(x - mLastMotionX);
final int touchSlop = mTouchSlop;
if (xDiff > touchSlop) {
// 移动方向小于45度时即X方向可以移动
if (Math.abs(mLastMotionY - y) / Math.abs(mLastMotionX - x) < 1) {
mTouchState = TOUCH_STATE_SCROLLING;
}
}
break;
case MotionEvent.ACTION_DOWN:
mLastMotionX = x;
mLastMotionY = y;
-
mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST
- TOUCH_STATE_SCROLLING;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
mTouchState = TOUCH_STATE_REST;
break;
}
return mTouchState != TOUCH_STATE_REST;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(ev);
final int action = ev.getAction();
final float x = ev.getX();
switch (action) {
case MotionEvent.ACTION_DOWN:
if (!mScroller.isFinished()) {
// 终止滚动条的滑动动画
mScroller.abortAnimation();
}
mLastMotionX = x;
count = -1;
defaultScreen = -1;
break;
case MotionEvent.ACTION_MOVE:
if (mTouchState == TOUCH_STATE_SCROLLING) {
final float t_width = (getWidth() / 4f);
// 最后一个屏幕向左移动时,不能超过屏幕的4分之一
if (getScrollX() > ((getChildCount() - 1) * getWidth() + t_width)) {
break;
}
// 第一个屏幕向右移动时,不能超过屏幕的4分之一
if (getScrollX() < ((t_width) * -1)) {
break;
}
final int deltaX = (int) (mLastMotionX - x);
mLastMotionX = x;
scrollBy(deltaX, 0);
}
break;
case MotionEvent.ACTION_UP:
if (mTouchState == TOUCH_STATE_SCROLLING) {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);// 使用pix/s为单位
int velocityX = (int) velocityTracker.getXVelocity();
if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
// 向右移动
snapToScreen(mCurrentScreen - 1, false);
} else if (velocityX < -SNAP_VELOCITY
&& mCurrentScreen < getChildCount() - 1) {
// 向左移动
snapToScreen(mCurrentScreen + 1, false);
} else {
snapToDestination();
}
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
}
mTouchState = TOUCH_STATE_REST;
break;
case MotionEvent.ACTION_CANCEL:
mTouchState = TOUCH_STATE_REST;
}
return true;
}
// 计算应该去哪个屏
private void snapToDestination() {
final int screenWidth = getWidth();
// 如果超过屏幕的一半就算是下一个屏
final int whichScreen = (getScrollX() + (screenWidth / 2))/ screenWidth;
snapToScreen(whichScreen, false);
}
// 切换屏幕
private void snapToScreen(int whichScreen, boolean isJump) {
// 判断下一个屏幕是否有效,并纠正
whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
if (getScrollX() != (whichScreen * getWidth())) {
final int delta = whichScreen * getWidth() - getScrollX();
count = -1;
defaultScreen = -1;
// 开始滚动动画
mScroller.startScroll(getScrollX(), 0, delta, 0,
Math.abs(delta) * 2);
final int t_mCurrentScreen = mCurrentScreen;
mCurrentScreen = whichScreen;
// 判断是否在同一个屏幕,不在则执行切换屏幕
if (t_mCurrentScreen != whichScreen) {
// 防止重复执行切换屏幕事件
if (Math.abs(t_mCurrentScreen - whichScreen) == 1 && !isJump) {
doOnScrollToScreen();
}
}
invalidate();
}
}
private void doOnScrollToScreen() {
if (mScrollToScreenListener != null) {
mScrollToScreenListener.operation(mCurrentScreen, getChildCount());
}
}
/**
-
设置切换到的指定下标屏幕0至getChildCount()-1
-
*/
public void setToScreen(int whichScreen, boolean isAnimation) {
if (isAnimation) {
snapToScreen(whichScreen, true);
} else {
whichScreen = Math.max(0,
Math.min(whichScreen, getChildCount() - 1));
mCurrentScreen = whichScreen;
// 直接滚动到该位置
scrollTo(whichScreen * getWidth(), 0);
if (whichScreen != mCurrentScreen) {
doOnScrollToScreen();
}
invalidate();
}
}
/**
-
设置默认屏幕的下标
-
*/
public void setDefaultScreen(int defaultScreen) {
mCurrentScreen = defaultScreen;
}
/**
-
获取当前屏幕的下标
-
*/
public int getCurrentScreen() {
return mCurrentScreen;
}
/**
-
注册滚动到指定屏幕的事件
-
*/
public void setOnScrollToScreenListener(
OnScrollToScreenListener scrollToScreenListener) {
if (scrollToScreenListener != null) {
this.mScrollToScreenListener = scrollToScreenListener;
}
}
/**
-
注册自定义Touch事件
-
*/
public void setOnCustomTouchListener(
OnCustomTouchListener customTouchListener) {
if (customTouchListener != null) {
this.mCustomTouchListener = customTouchListener;
}
}
/**
-
滚动到指定屏幕的事件(即切屏事件)
-
*/
public interface OnScrollToScreenListener {
public void operation(int currentScreen, int screenCount);
}
/**
-
自定义的一个Touch事件
-
*/
public interface OnCustomTouchListener {
public void operation(MotionEvent event);
}
/**
-
滚动到每个屏幕时是否都要触发OnScrollToScreenListener事件
-
*/
public void setEveryScreen(boolean isEveryScreen) {
this.isEveryScreen = isEveryScreen;
}
}
2、界面布局的改变
player_activity_layout.xml的布局文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:id=“@+id/RelativeLayout1”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background=“@drawable/bg_playback” >
<RelativeLayout
android:id=“@+id/header_layout”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_alignParentTop=“true” >
<Button
android:id=“@id/repeat_music”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentLeft=“true”
android:background=“@drawable/repeat_none_selector” />
<Button
android:id=“@id/shuffle_music”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentRight=“true”
android:background=“@drawable/shuffle_none_selector” />
<TextView
android:id=“@+id/musicTitle”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignBaseline=“@id/repeat_music”
android:layout_centerHorizontal=“true”
android:ellipsize=“marquee”
android:focusable=“true”
android:focusableInTouchMode=“true”
android:gravity=“center_horizontal”
android:lines=“1”
android:marqueeRepeatLimit=“marquee_forever”
android:singleLine=“true”
android:text=“@string/siger”
android:textAppearance=“?android:attr/textAppearanceLarge”
android:textColor=“@android:color/white” />
<TextView
android:id=“@+id/musicArtist”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_below=“@id/musicTitle”
android:layout_centerHorizontal=“true”
android:layout_marginTop=“15dp”
android:text=“@string/artist”
android:textColor=“#0F0”
android:textSize=“18sp” />
<FrameLayout
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_below=“@+id/header_layout” >
<RelativeLayout
android:layout_width=“match_parent”
android:layout_height=“match_parent” >
<com.wwj.sb.custom.FlingGalleryView
android:id=“@+id/fgv_player_main”
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
android:layout_centerInParent=“true” >
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
最后
一线互联网Android面试题含详解(初级到高级专题)
这些题目是今年群友去腾讯、百度、小米、乐视、美团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。并且大多数都整理了答案,熟悉这些知识点会大大增加通过前两轮技术面试的几率
如果设置门槛,很多开发者朋友会因此错过这套高级架构资料,错过提升成为架构师的可能。这就失去了我们的初衷;让更多人都能通过高效高质量的学习,提升自己的技术和格局,升职加薪。
最后送给大家一句话,望共勉,永远不要放弃自己的梦想和追求;
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
果低效又漫长,而且极易碰到天花板技术停滞不前!**
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-LKyMxqNu-1713806683710)]
[外链图片转存中…(img-ba5xYMIO-1713806683712)]
[外链图片转存中…(img-NZiGnRuV-1713806683713)]
[外链图片转存中…(img-PYFXkRfq-1713806683714)]
[外链图片转存中…(img-fIQ76sWo-1713806683715)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
[外链图片转存中…(img-EZjPVV6S-1713806683716)]
最后
一线互联网Android面试题含详解(初级到高级专题)
这些题目是今年群友去腾讯、百度、小米、乐视、美团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。并且大多数都整理了答案,熟悉这些知识点会大大增加通过前两轮技术面试的几率
[外链图片转存中…(img-VcnxjU0S-1713806683717)]
如果设置门槛,很多开发者朋友会因此错过这套高级架构资料,错过提升成为架构师的可能。这就失去了我们的初衷;让更多人都能通过高效高质量的学习,提升自己的技术和格局,升职加薪。
最后送给大家一句话,望共勉,永远不要放弃自己的梦想和追求;
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!