This commit is contained in:
wangcongtao
2020-01-15 19:17:33 +08:00
parent 75f4d373a9
commit 6ddf294e7d
9 changed files with 117 additions and 40 deletions

View File

@@ -15,9 +15,16 @@ import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.mogo.utils.ThreadPoolService;
import com.mogo.utils.logger.Logger;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
public class JSurfaceView extends SurfaceView implements Runnable, SurfaceHolder.Callback {
private static final String TAG = "JSurfaceView";
private SurfaceHolder mHolder;
/**
@@ -33,6 +40,15 @@ public class JSurfaceView extends SurfaceView implements Runnable, SurfaceHolder
*/
private int[] mFrames;
// 使用 BitmapFactory.Option.inBitmap 属性复用 bitmap 对象
private Bitmap mContainerBitmap = null;
// mContainerBitmap 是否生效
private boolean mContainerBitmapStatus = true;
// mContainerBitmapStatus 为 false 时,使用该缓存方案
private Map< Integer, WeakReference< Bitmap > > mBitmapRefMap = new HashMap<>();
public JSurfaceView( Context context ) {
super( context );
init();
@@ -87,28 +103,60 @@ public class JSurfaceView extends SurfaceView implements Runnable, SurfaceHolder
private void drawBitmap() {
//获取画布并锁定
Canvas mCanvas = mHolder.lockCanvas();
if ( mCanvas == null ) {
Canvas canvas = mHolder.lockCanvas();
if ( canvas == null ) {
return;
}
//绘制透明色
mCanvas.drawColor( Color.TRANSPARENT, PorterDuff.Mode.CLEAR );
Bitmap mBitmap = BitmapFactory.decodeResource( getResources(), mFrames[mCurrentPos % mFrames.length] );
canvas.drawColor( Color.TRANSPARENT, PorterDuff.Mode.CLEAR );
int factPosition = mCurrentPos % mFrames.length;
if ( mContainerBitmapStatus ) {
if ( mContainerBitmap == null ) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
mContainerBitmap = BitmapFactory.decodeResource( getResources(), mFrames[factPosition], options );
} else {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
options.inBitmap = mContainerBitmap;
try {
BitmapFactory.decodeResource( getResources(), mFrames[factPosition], options );
} catch ( Exception e ) {
Logger.e( TAG, e, "error." );
mContainerBitmapStatus = false;
mContainerBitmap = null;
}
}
}
if ( !mContainerBitmapStatus ) {
WeakReference< Bitmap > ref = mBitmapRefMap.get( factPosition );
if ( ref != null && ref.get() != null && !ref.get().isRecycled() ) {
mContainerBitmap = ref.get();
} else {
mContainerBitmap = BitmapFactory.decodeResource( getResources(), mFrames[factPosition] );
mBitmapRefMap.put( factPosition, new WeakReference<>( mContainerBitmap ) );
}
}
if ( mContainerBitmap == null || mContainerBitmap.isRecycled() ) {
return;
}
Paint paint = new Paint();
Rect mSrcRect = new Rect( 0,
0,
mBitmap.getWidth(),
mBitmap.getHeight() ); // 图片绘制
mContainerBitmap.getWidth(),
mContainerBitmap.getHeight() ); // 图片绘制
Rect mDestRect = new Rect( 0,
0,
getWidth(),
getHeight() );// 图片绘制位置
mCanvas.drawBitmap( mBitmap, mSrcRect, mDestRect, paint );
canvas.drawBitmap( mContainerBitmap, mSrcRect, mDestRect, paint );
//解锁画布并展示bitmap到surface
mHolder.unlockCanvasAndPost( mCanvas );
mBitmap.recycle();
mHolder.unlockCanvasAndPost( canvas );
}
@Override