Runt
2022-05-04 27f180efca808046df3f3e917ee5f98d2d54849b
滑动歌词 计算时间
4 files modified
83 ■■■■■ changed files
app/src/main/java/com/auto/lyric/data/LyricObject.java 10 ●●●●● patch | view | raw | blame | history
app/src/main/java/com/auto/lyric/data/LyricServer.java 1 ●●●● patch | view | raw | blame | history
app/src/main/java/com/auto/lyric/service/FloatingWindowService.java 31 ●●●●● patch | view | raw | blame | history
app/src/main/java/com/auto/lyric/widgets/LyricView.java 41 ●●●●● patch | view | raw | blame | history
app/src/main/java/com/auto/lyric/data/LyricObject.java
@@ -8,4 +8,14 @@
    public int endtime; // 结束时间
    public int timeline; // 单句歌词用时
    public String lrc; // 单句歌词
    @Override
    public String toString() {
        return "LyricObject{" +
                "begintime=" + begintime +
                ", endtime=" + endtime +
                ", timeline=" + timeline +
                ", lrc='" + lrc + '\'' +
                '}';
    }
}
app/src/main/java/com/auto/lyric/data/LyricServer.java
@@ -18,6 +18,7 @@
public class LyricServer {
    // key 时间戳 , obj  歌词
    private static TreeMap<Integer, LyricObject> lrc_map = new TreeMap<>();
    public static TreeMap<Integer, LyricObject> getLrc_map() {
app/src/main/java/com/auto/lyric/service/FloatingWindowService.java
@@ -22,6 +22,7 @@
import androidx.annotation.Nullable;
import com.auto.lyric.R;
import com.auto.lyric.data.LyricObject;
import com.auto.lyric.data.LyricServer;
import com.auto.lyric.databinding.FloatViewBinding;
import com.auto.lyric.util.DeviceUtil;
@@ -195,6 +196,36 @@
        Log.e(TAG,"onStartCommand flags:"+flags+" startId:"+startId+ " intent:"+intent);
        manager.addView(binding.getRoot(),params);
        binding.lyric.setTextSize();
        binding.lyric.setOnTouchListener(new View.OnTouchListener() {
            float touchY; //当触摸歌词View时,保存为当前触点的Y轴坐标
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                float tt=event.getY();
                switch(event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        touchY=tt-touchY;
                        binding.lyric.setOffsetY(binding.lyric.getOffsetY()+touchY);
                        Log.e("LyricView","offsety:"+binding.lyric.getOffsetY());
                        binding.lyric.invalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        if(binding.btnStart.getText().equals("开启") || pause) {
                            int index = binding.lyric.getIndexFromOffsetY(binding.lyric.getOffsetY());
                            binding.lyric.setIndex(index);
                            binding.lyric.setOffsetY(220 - index * (binding.lyric.getSIZEWORD() + 44));
                            LyricObject object = LyricServer.getLrc_map().get(index);
                            Log.e("LyricView", "object:" + object);
                            progress = object.begintime;
                            binding.lyric.invalidate();
                        }
                        break;
                }
                touchY=tt;
                return true;
            }
        });
        return super.onStartCommand(intent, flags, startId);
    }
app/src/main/java/com/auto/lyric/widgets/LyricView.java
@@ -6,7 +6,6 @@
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import com.auto.lyric.data.LyricObject;
import com.auto.lyric.data.LyricServer;
@@ -21,7 +20,6 @@
    private float mX;       //屏幕X轴的中点,此值固定,保持歌词在X中间显示
    private float offsetY;      //歌词在Y轴上的偏移量,此值会根据歌词的滚动变小
    private float touchY;   //当触摸歌词View时,保存为当前触点的Y轴坐标
    private int lrcIndex=0; //保存歌词TreeMap的下标
    private  int SIZEWORD=0;//显示歌词文字的大小值
    private  int INTERVAL=45;//歌词每行的间隔
@@ -70,29 +68,6 @@
            canvas.drawText("找不到歌词", mX, 310, paint);
        }
        super.onDraw(canvas);
    }
    /* (non-Javadoc)
     * @see android.view.View#onTouchEvent(android.view.MotionEvent)
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        float tt=event.getY();
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                touchY=tt-touchY;
                offsetY=offsetY+touchY;
                Log.e("LyricView","offsety:"+offsetY);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        touchY=tt;
        return true;
    }
    public void init(){
@@ -177,6 +152,9 @@
        return lrcIndex;
    }
    public void setIndex(int lrcIndex) {
        this.lrcIndex = lrcIndex;
    }
    /**
     * @return the offsetY
@@ -206,4 +184,17 @@
    public void setSIZEWORD(int sIZEWORD) {
        SIZEWORD = sIZEWORD;
    }
    public int getIndexFromOffsetY(float offsetY){
        //220 - index * (binding.lyric.getSIZEWORD() + 44)
        int index = (int) ((220 - offsetY) / (SIZEWORD + 44));
        Log.i("LyricView", "getIndexFromOffsetY index:"+index);
        if(LyricServer.getLrc_map().keySet().size()>index){
            return index;
        }else if(index < 0){
            return 0;
        }else{
            return LyricServer.getLrc_map().keySet().size() -1;
        }
    }
}