這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)Android應(yīng)用中利用SurfaceView截屏?xí)r出現(xiàn)黑屏如何解決,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、微信平臺(tái)小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了澗西免費(fèi)建站歡迎大家使用!
1、首先看我們一般是怎么用SurfaceView的
public class SuperSurfaceView extends SurfaceView implements SurfaceHolder.Callback { SurfaceHolder surfaceHolder; public SuperSurfaceView(Context context) { super(context); surfaceHolder = this.getHolder(); surfaceHolder.addCallback(this); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder) { new Thread(new MyThread()).start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { } class MyThread implements Runnable { @Override public void run() { Canvas canvas = surfaceHolder.lockCanvas(null);//獲取畫布 Paint mPaint = new Paint(); mPaint.setColor(Color.BLUE); canvas.drawRect(new RectF(100, 100, 1000, 550), mPaint); surfaceHolder.unlockCanvasAndPost(canvas);//解鎖畫布,提交畫好的圖像 } } }
從以上代碼中,可以看到在MyThread的run方法中創(chuàng)建了一塊畫布,進(jìn)行SurfaceView的繪制,但是我們知道SurfaceView是雙緩沖機(jī)制,這個(gè)Canvas上的內(nèi)容我們拿不到,所以截屏出來是黑屏。所以,要拿到這個(gè)畫布上的內(nèi)容,就自己創(chuàng)建一個(gè)畫布,將這些內(nèi)容畫到自己的Canvas上,就可以了。
2、拿到SurfaceView畫布上的bitmap,看修改過后的代碼
public abstract class SuperSurfaceView extends SurfaceView implements SurfaceHolder.Callback { SurfaceHolder surfaceHolder; public SuperSurfaceView(Context context) { super(context); surfaceHolder = this.getHolder(); surfaceHolder.addCallback(this); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder) { new Thread(new MyThread()).start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { } class MyThread implements Runnable { @Override public void run() { Canvas canvas = surfaceHolder.lockCanvas(null);//獲取畫布 doDraw(canvas); surfaceHolder.unlockCanvasAndPost(canvas);//解鎖畫布,提交畫好的圖像 } } //將繪制圖案的方法抽象出來,讓子類實(shí)現(xiàn),調(diào)用getBitmap方法時(shí)就會(huì)調(diào)用此方法 protected abstract void doDraw(Canvas canvas); //調(diào)用該方法將doDraw繪制的圖案繪制在自己的canvas上 public Bitmap getBitmap() { Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); doDraw(canvas); return bitmap; } }
3、下面看MainActivity中如何使用
Button bt = (Button) findViewById(R.id.bt); FrameLayout fl = (FrameLayout) findViewById(R.id.fl); //用到surfaceview需繼承SuperSurfaceView才能截到屏,否則截出來是黑屏 SuperSurfaceView surfaceView = new SuperSurfaceView(this) { @Override protected void doDraw(Canvas canvas) { Paint mPaint = new Paint(); mPaint.setColor(Color.BLUE); canvas.drawRect(new RectF(100, 100, 1000, 550), mPaint); } }; fl.addView(surfaceView); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //調(diào)用該方法即可得到surfaceview上繪制的圖案 Bitmap bitmap = surfaceView.getBitmap(); } });
上述就是小編為大家分享的Android應(yīng)用中利用SurfaceView截屏?xí)r出現(xiàn)黑屏如何解決了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。