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 java.util.ArrayList; import gnph.util.JSMap; public class EleScroll extends View implements DrawOther, Choreographer.FrameCallback { ArrayList others; int w, h; Bitmap img; int interval, cur, end, step; char effect; public EleScroll(Context context, String dirPre, JSMap json, int w, int h) { super(context); this.w = w; this.h = h; 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 { drawOther(canvas); if(freshCnt==0 && effect!=0 && interval!=0) choreographer.postFrameCallback(this); } catch (Throwable e) { setVisibility(GONE); img = null; e.printStackTrace(); } } @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()+w; 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); } 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(); if(others!=null) for(var other : others) other.invalidate(); } choreographer.postFrameCallback(this); } }