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

100 lines
3.3 KiB
Java
Raw Normal View History

2023-11-09 08:37:59 +08:00
package com.xixun.xixunplayer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.Choreographer;
import android.view.View;
2023-11-10 20:44:11 +08:00
import java.util.ArrayList;
2023-11-09 08:37:59 +08:00
import gnph.util.JSMap;
2023-11-10 20:44:11 +08:00
public class EleScroll extends View implements DrawOther, Choreographer.FrameCallback {
2023-11-09 08:37:59 +08:00
2023-11-10 20:44:11 +08:00
ArrayList<EleOtherPart> others;
2023-11-09 08:37:59 +08:00
Bitmap img;
int interval, cur, end, step;
char effect;
public EleScroll(String dirPre, JSMap json, Context context) {
super(context);
img = BitmapFactory.decodeFile(dirPre + json.stnn("id"));
var effStr = json.str("effect");
if(effStr==null || effStr.equals("no")) return;
var scrollSpeed = json.dbl("scrollSpeed");
if(scrollSpeed==0) {
var scrollDur = json.dbl("effectSpeed");
if(scrollDur==0) return;
scrollSpeed = 1000 / scrollDur;
}
interval = step = 1;
if(scrollSpeed > 60) step = (int) Math.round(scrollSpeed/60);
else if(scrollSpeed < 60) interval = (int) Math.round(60/scrollSpeed);
int idx = effStr.lastIndexOf(' ');
if(idx > -1) {
effect = effStr.charAt(idx+1);
if(effect=='l') end = -(img.getWidth()-step);
else if(effect=='r') end = img.getWidth()-step;
else if(effect=='t') end = -(img.getHeight()-step);
else if(effect=='b') end = img.getHeight()-step;
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(img==null) return;
try {
2023-11-10 20:44:11 +08:00
drawOther(canvas);
2023-11-09 08:37:59 +08:00
if(freshCnt==0 && effect!=0 && interval!=0) choreographer.postFrameCallback(this);
2023-11-10 09:47:38 +08:00
} catch (Throwable e) {
setVisibility(GONE);
img = null;
2023-11-09 08:37:59 +08:00
e.printStackTrace();
}
}
2023-11-10 20:44:11 +08:00
@Override
public void drawOther(Canvas canvas) {
if(img==null) return;
if(effect=='l') {
canvas.drawBitmap(img, cur, 0, null);
canvas.drawBitmap(img, cur+img.getWidth(), 0, null);
} else if(effect=='r') {
var x = cur-img.getWidth()+getWidth();
canvas.drawBitmap(img, x, 0, null);
canvas.drawBitmap(img, x-img.getWidth(), 0, null);
} else if(effect=='t') {
canvas.drawBitmap(img, 0, cur, null);
canvas.drawBitmap(img, 0, cur+img.getHeight(), null);
} else if(effect=='b') {
canvas.drawBitmap(img, 0, cur, null);
canvas.drawBitmap(img, 0, cur-img.getHeight(), null);
} else canvas.drawBitmap(img, 0, 0, null);
}
2023-11-09 08:37:59 +08:00
Choreographer choreographer = Choreographer.getInstance();
int freshCnt;
@Override
public void doFrame(long frameTimeNanos) {
if(! isShown()) {
freshCnt = cur = 0;
return;
}
if(freshCnt < interval) freshCnt++;
else {
freshCnt = 1;
if(effect=='t' || effect=='l') {
if(cur <= end) cur -= end;
else cur -= step;
} else if(effect=='b' || effect=='r') {
if(cur >= end) cur -= end;
else cur += step;
}
invalidate();
2023-11-10 20:44:11 +08:00
if(others!=null) for(var other : others) other.invalidate();
2023-11-09 08:37:59 +08:00
}
choreographer.postFrameCallback(this);
}
}