Android/XixunPlayer/app/src/main/java/com/xixun/xixunplayer/SrcTimer.java

152 lines
4.7 KiB
Java
Raw Normal View History

2023-11-09 08:37:59 +08:00
package com.xixun.xixunplayer;
2024-02-02 21:51:16 +08:00
import android.annotation.SuppressLint;
2023-11-09 08:37:59 +08:00
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Choreographer;
import android.view.View;
import java.util.ArrayList;
import java.util.HashMap;
import gnph.util.Dates;
import gnph.util.JSMap;
import gnph.util.NumFmts;
2024-02-02 21:51:16 +08:00
@SuppressLint("ViewConstructor")
2024-01-24 20:17:59 +08:00
public class SrcTimer extends View implements Choreographer.FrameCallback {
2023-11-09 08:37:59 +08:00
HashMap<String, Bitmap> imgMap = new HashMap<>();
ArrayList<Bitmap> imgs = new ArrayList<>();
Bitmap text, day, hour, min, sec;
int spaceWidth, len;
Paint paint = new Paint();
long targetTime;
boolean isDown;
boolean isMultiline;
2024-01-24 20:17:59 +08:00
boolean hasDay, hasHour, hasMin, hasSec;
2023-11-09 08:37:59 +08:00
2024-02-02 21:51:16 +08:00
public SrcTimer(Prog prog, JSMap json) {
super(prog.getContext());
2023-11-09 08:37:59 +08:00
var imgEntrys = json.jsmap("imgs").entrySet();
2024-01-24 20:17:59 +08:00
for(var imgEntry : imgEntrys) imgMap.put(imgEntry.getKey(), BitmapFactory.decodeFile(Util.programDir+"/"+imgEntry.getValue()));
2023-11-09 08:37:59 +08:00
text = imgMap.get("text");
day = imgMap.get("day");
hour = imgMap.get("hour");
min = imgMap.get("min");
sec = imgMap.get("sec");
spaceWidth = (int) Math.round(json.dbl("spaceWidth"));
isDown = json.bool("isDown");
targetTime = Dates.milli(json.stnn("targetTime")) / 1000;
hasDay = json.bool("hasDay");
hasHour = json.bool("hasHour");
hasMin = json.bool("hasMin");
hasSec = json.bool("hasSec");
isMultiline = json.bool("isMultiline");
paint.setTextAlign(Paint.Align.CENTER);
try {
setBackgroundColor(Color.parseColor(json.stnn("backColor")));
} catch (Exception ignored) {}
2024-02-02 21:51:16 +08:00
prog.calls.add(this);
2023-11-09 08:37:59 +08:00
}
void cal() {
2024-02-02 21:51:16 +08:00
var secs = isDown ? targetTime - lastSec : lastSec - targetTime;
2023-11-09 08:37:59 +08:00
if(secs < 0) secs = 0;
len = 0;
imgs.clear();
if(text!=null && ! isMultiline) {
imgs.add(text);
imgs.add(null);
len += text.getWidth();
len += spaceWidth;
}
if(hasDay) {
var str = Long.toString(secs/86400);
for(int cc=0; cc<str.length(); cc++) {
var img = imgMap.get(str.substring(cc, cc+1));
imgs.add(img);
len += img.getWidth();
}
imgs.add(null);
imgs.add(day);
imgs.add(null);
len += day.getWidth();
len += spaceWidth * 2;
secs %= 86400;
}
if(hasHour) {
var str = NumFmts.zz().format(secs/3600);
for(int cc=0; cc<str.length(); cc++) {
var img = imgMap.get(str.substring(cc, cc+1));
imgs.add(img);
len += img.getWidth();
}
imgs.add(hour);
imgs.add(null);
len += hour.getWidth();
len += spaceWidth;
secs %= 3600;
}
if(hasMin) {
var str = NumFmts.zz().format(secs/60);
for(int cc=0; cc<str.length(); cc++) {
var img = imgMap.get(str.substring(cc, cc+1));
imgs.add(img);
len += img.getWidth();
}
imgs.add(min);
imgs.add(null);
len += min.getWidth();
len += spaceWidth;
secs %= 60;
}
if(hasSec) {
var str = NumFmts.zz().format(secs);
for(int cc=0; cc<str.length(); cc++) {
var img = imgMap.get(str.substring(cc, cc+1));
imgs.add(img);
len += img.getWidth();
}
imgs.add(sec);
imgs.add(null);
len += sec.getWidth();
len += spaceWidth;
}
imgs.remove(imgs.size()-1);
len -= spaceWidth;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int x = (getWidth()-len) / 2, y;
if(text!=null && isMultiline) {
y = getHeight() / 2;
canvas.drawBitmap(text, (getWidth()-text.getWidth()) / 2, y-text.getHeight(), null);
} else y = (getHeight() - imgs.get(0).getHeight()) / 2;
for(var img : imgs) {
if(img==null) x += spaceWidth;
else {
canvas.drawBitmap(img, x, y, null);
x += img.getWidth();
}
}
}
2024-01-24 20:17:59 +08:00
long lastSec;
2023-11-09 08:37:59 +08:00
@Override
2024-02-02 21:51:16 +08:00
public void doFrame(long ms) {
if(! isShown()) return;
var sec = ms / 1000;
2023-11-09 08:37:59 +08:00
if(sec != lastSec) {
lastSec = sec;
cal();
invalidate();
}
}
}