Skip to content

Commit

Permalink
Merge pull request #9 from censhengde/dev
Browse files Browse the repository at this point in the history
新增 app:located_view_layout 属性。
  • Loading branch information
censhengde authored Oct 13, 2023
2 parents eb4dc2e + 59ff7d5 commit a32f3e6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 29 deletions.
26 changes: 18 additions & 8 deletions app/src/main/java/com/bytedance/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,25 @@ class MainActivity : AppCompatActivity(), KrcView.onDraggingListener {
}

})
LocatedViewBinding.inflate(layoutInflater, vb.krcView, false).run {
// 点击跳转指定进度。
btnSeekTo.setOnClickListener {
progress = skipProgress
vb.krcView.setProgress(skipProgress)
vb.seekBar.progress = skipProgress.toInt()
// LocatedViewBinding.inflate(layoutInflater, vb.krcView, false).run {
// // 点击跳转指定进度。
// btnSeekTo.setOnClickListener {
// progress = skipProgress
// vb.krcView.setProgress(skipProgress)
// vb.seekBar.progress = skipProgress.toInt()
// }
// // 设置 located view
// vb.krcView.locatedView = this.root
// }
vb.krcView.locatedView?.let {
LocatedViewBinding.bind(it).run {
// 点击跳转指定进度。
btnSeekTo.setOnClickListener {
progress = skipProgress
vb.krcView.setProgress(skipProgress)
vb.seekBar.progress = skipProgress.toInt()
}
}
// 设置 located view
vb.krcView.locatedView = this.root
}
// 设置拖拽歌词监听器
vb.krcView.setOnDraggingListener(this)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
app:max_text_size="18sp"
app:min_text_size="15sp"
app:maxWordsPerLine="15"
app:located_view_layout="@layout/located_view"
app:current_line_top_offset="70dp"
android:layout_marginBottom="10dp" />

Expand Down
52 changes: 31 additions & 21 deletions krcview/src/main/java/com/bytedance/krcview/KrcView.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -34,6 +35,7 @@
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.LayoutInflaterCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;
Expand Down Expand Up @@ -127,7 +129,7 @@ public KrcView(@NonNull Context context, @Nullable AttributeSet attrs) {

public KrcView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
recyclerView = new RecyclerView(context){
recyclerView = new RecyclerView(context) {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent e) {
Expand Down Expand Up @@ -205,6 +207,11 @@ protected int getExtraLayoutSpace(State state) {
currentLineTextColor = readAttrColor(a, R.styleable.KrcView_current_line_text_color, normalTextColor);
currentLineHLTextColor = readAttrColor(a, R.styleable.KrcView_current_line_highLight_text_color, Color.GREEN);
isUserInputEnabled = a.getBoolean(R.styleable.KrcView_isUserInputEnabled, true);
final int locatedViewLayoutId = a.getResourceId(R.styleable.KrcView_located_view_layout, View.NO_ID);
if (locatedViewLayoutId != View.NO_ID) {
locatedView = LayoutInflater.from(getContext()).inflate(locatedViewLayoutId, this, false);
setLocatedView(locatedView);
}
a.recycle();
if (lineSpace > 0f) {
recyclerView.addItemDecoration(new ItemDecoration() {
Expand Down Expand Up @@ -365,6 +372,7 @@ public void setLocatedView(View view) {
locatedView = view;
view.setVisibility(View.INVISIBLE);
addView(view);
post(this::updateLocateViewTopOffset);
}


Expand All @@ -389,6 +397,7 @@ public void setKrcData(List<KrcLineInfo> data) {
}
}
recyclerView.setAdapter(new AdapterImpl());
post(this::updateLocateViewTopOffset);
}


Expand Down Expand Up @@ -544,8 +553,7 @@ void setLineProgress(final long lineProgress) {
return;
}
this.lineProgress = lineProgress;
// KrcLineView.this.invalidate();
invalidate();
KrcLineView.this.invalidate();
}


Expand Down Expand Up @@ -792,27 +800,13 @@ public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newStat

private void tryToShowLocatedView() {
removeCallbacks(hideLocatedViewTask);
if (locatedView == null || curLineIndex < 0 || locatedView.getVisibility() == View.VISIBLE) {
if (locatedView == null || locatedView.getVisibility() == View.VISIBLE) {
return;
}
locatedView.setVisibility(View.VISIBLE);
}

private void updateLocateViewTopOffset() {
if (locatedView == null || curLineIndex < 0 || locatedView.getVisibility() == View.VISIBLE) {
return;
}
final ViewHolder curVH = recyclerView.findViewHolderForAdapterPosition(curLineIndex);
if (curVH == null) {
return;
}
final int newLocateViewTopOffset = curVH.itemView.getBottom() - (locatedView.getHeight() >> 1);
if (newLocateViewTopOffset != locateViewTopOffset) {
locateViewTopOffset = newLocateViewTopOffset;
logI("===>updateLocateViewTopOffset: " + locateViewTopOffset);
requestLayout();
}
}


private void tryToHideLocatedViewDelay() {
if (locatedView == null || locatedView.getVisibility() != VISIBLE) {
Expand All @@ -822,10 +816,10 @@ private void tryToHideLocatedViewDelay() {
}

private void notifyStartDragging() {
if (onDraggingListener == null || curLineIndex < 0) {
if (onDraggingListener == null) {
return;
}
final ViewHolder cur = recyclerView.findViewHolderForAdapterPosition(curLineIndex);
final ViewHolder cur = recyclerView.findViewHolderForAdapterPosition(curLineIndex == -1 ? 0 : curLineIndex);
if (cur != null) {
locatedItemView = (KrcLineView) cur.itemView;
onDraggingListener.onStartDragging(KrcView.this, locatedItemView.krcLineInfo,
Expand Down Expand Up @@ -873,6 +867,22 @@ private void notifyDragging() {
}
};

private void updateLocateViewTopOffset() {
if (locatedView == null || locatedView.getVisibility() == View.VISIBLE) {
return;
}
final ViewHolder curVH = recyclerView.findViewHolderForAdapterPosition(
curLineIndex == -1 ? 0 : curLineIndex);
if (curVH == null) {
return;
}
final int newLocateViewTopOffset = curVH.itemView.getBottom() - (locatedView.getHeight() >> 1);
if (newLocateViewTopOffset != locateViewTopOffset) {
locateViewTopOffset = newLocateViewTopOffset;
logI("===>updateLocateViewTopOffset: " + locateViewTopOffset);
requestLayout();
}
}
public interface onDraggingListener {

/**
Expand Down
1 change: 1 addition & 0 deletions krcview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<attr name="current_line_top_offset" format="dimension" />
<attr name="maxWordsPerLine" format="integer" />
<attr name="isUserInputEnabled" format="boolean" />
<attr name="located_view_layout" format="reference" />
</declare-styleable>

</resources>

0 comments on commit a32f3e6

Please sign in to comment.