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

91 lines
3.1 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;
import android.view.ViewGroup;
import gnph.util.JSMap;
public class EleScroll extends View implements Choreographer.FrameCallback {
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 {
if(effect=='l') {
canvas.drawBitmap(img, cur, 0, null);
canvas.drawBitmap(img, cur+img.getWidth(), 0, null);
} else if(effect=='r') {
canvas.drawBitmap(img, cur, 0, null);
canvas.drawBitmap(img, cur-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);
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();
}
}
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();
}
choreographer.postFrameCallback(this);
}
}