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” >
<include
android:id=“@+id/player_main_album”
layout=“@layout/music_album” />
<include
android:id=“@+id/player_main_lyric”
layout=“@layout/music_lyric” />
</com.wwj.sb.custom.FlingGalleryView>
<RelativeLayout
android:id=“@+id/ll_player_voice”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:background=“@drawable/player_progresslayout_bg”
android:visibility=“gone” >
<ImageView
android:id=“@+id/iv_player_min_voice”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentLeft=“true”
android:layout_centerVertical=“true”
android:background=“@drawable/volume_min_icon” />
<ImageView
android:id=“@+id/iv_player_max_voice”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentRight=“true”
android:layout_centerVertical=“true”
android:background=“@drawable/volume_max_icon” />
<SeekBar
android:id=“@+id/sb_player_voice”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:layout_centerVertical=“true”
android:layout_toLeftOf=“@id/iv_player_max_voice”
android:layout_toRightOf=“@id/iv_player_min_voice”
android:background=“@drawable/voice_seekbar_bg”
android:paddingLeft=“5dp”
android:paddingRight=“5dp”
android:progressDrawable=“@drawable/voice_seekbar_progress”
android:thumb=“@drawable/voice_seekbar_thumb” />
<RelativeLayout
android:id=“@+id/footer_layout”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_alignParentBottom=“true” >
<RelativeLayout
android:id=“@+id/seekbarLayout”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:background=“@drawable/player_progresslayout_bg” >
<SeekBar
android:id=“@+id/audioTrack”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_centerVertical=“true”
android:background=“@drawable/player_progress_bg”
android:progressDrawable=“@drawable/seekbar_img”
android:thumb=“@drawable/media_player_progress_button” />
<TextView
android:id=“@+id/current_progress”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_below=“@id/audioTrack” />
<TextView
android:id=“@+id/final_progress”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后
分享一份NDK基础开发资料
分享内容包括不限于高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter等全方面的Android进阶实践技术;希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-JdygegFT-1712421284959)]
[外链图片转存中…(img-LDqsjSkQ-1712421284960)]
[外链图片转存中…(img-7ZNpLG5b-1712421284960)]
[外链图片转存中…(img-sctMVlrW-1712421284961)]
[外链图片转存中…(img-WRHNNX88-1712421284961)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后
分享一份NDK基础开发资料
[外链图片转存中…(img-ubxwPBQ5-1712421284961)]
分享内容包括不限于高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter等全方面的Android进阶实践技术;希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!