何為髒矩形?比如surfaceview上繪製旋轉圖片的時候,只重新整理一個特定的矩形地區就可以了。這個矩形地區就是髒矩形。在surfaceview上 canvas = holder.lockCanvas(new Rect(0, 0, 130, 130)); //擷取畫布 像這種帶rect參數的就是髒矩形的重新整理。
請看源碼:
package yan.guoqi.rectphoto;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.PixelFormat;import android.graphics.Rect;import android.util.AttributeSet;import android.util.Log;import android.view.SurfaceHolder;import android.view.SurfaceView;public class DrawSurfaceView extends SurfaceView implements SurfaceHolder.Callback{private static final String tag2 = "DrawSV";private boolean runFlag = true;protected SurfaceHolder holder;private Bitmap rotateImg;private Thread myThread;public DrawSurfaceView(Context context, AttributeSet attrs)//建構函式{super(context, attrs);// TODO Auto-generated constructor stubrotateImg = BitmapFactory.decodeResource(getResources(), R.drawable.rotate_circle);rotateImg = Bitmap.createScaledBitmap(rotateImg, 100, 100, true);holder = this.getHolder();holder.addCallback(this);holder.setFormat(PixelFormat.TRANSPARENT); //頂層繪製SurfaceView設成透明this.setZOrderOnTop(true);myThread = new Thread(new MyThread());}public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {// TODO Auto-generated method stubLog.v(tag2, "DrawSV:surfaceChanged...");}public void surfaceCreated(SurfaceHolder arg0) {// TODO Auto-generated method stubLog.v(tag2, "DrawSV:surfaceCreated...");//啟動自訂線程myThread.start();}public void surfaceDestroyed(SurfaceHolder arg0) {// TODO Auto-generated method stubLog.v(tag2, "DrawSV:surfaceDestroyed...");//終止自訂線程runFlag = false;myThread.interrupt();}/*自訂線程*/class MyThread implements Runnable{public void run() {// TODO Auto-generated method stubCanvas canvas = null;int rotate = 0;while(runFlag){try {canvas = holder.lockCanvas(new Rect(0, 0, 130, 130)); //擷取畫布Paint paint = new Paint();//canvas.drawBitmap(rotateImg, 0, 0, paint); //繪製旋轉的背景//建立矩陣控製圖片旋轉和平移Matrix matrix = new Matrix();//設定旋轉角度matrix.postRotate((rotate += 48) % 360,rotateImg.getWidth() / 2, rotateImg.getHeight() / 2);//設定左邊距和上邊距matrix.postTranslate(0, 0);//繪製旋轉圖片canvas.drawBitmap(rotateImg, matrix, paint);//休眠控制最大幀率為每秒3繪製30次Thread.sleep(30);holder.unlockCanvasAndPost(canvas);//解鎖畫布,提交畫好的映像} catch (Exception e) {// TODO: handle exceptionLog.v(tag2, "DrawSurfaceView:繪製失敗...");}}}}}
需要注意的幾點:
第一,就是如何安全中止一個線程。這裡通過設定標誌位來實現。因為內建的stop()方法不安全,android本身都不推薦。關於如何安全中止線程可以參考這裡:http://www.iteye.com/problems/67052 http://www.dewen.org/q/1957
第二,holder.unlockCanvasAndPost(canvas);//解鎖畫布,提交畫好的映像 這句話必須和 canvas = holder.lockCanvas(new Rect(0, 0, 130, 130)); //擷取畫布 在一個{ }裡。像這裡的http://www.cnblogs.com/jqyp/archive/2012/01/01/2309692.html的做法在程式退出時會拋出異常。原因是線程中止的時候無法再解鎖畫布了。
第三,利用Matrix讓圖片旋轉的核心代碼:
//建立矩陣控製圖片旋轉和平移Matrix matrix = new Matrix();//設定旋轉角度matrix.postRotate((rotate += 48) % 360,rotateImg.getWidth() / 2, rotateImg.getHeight() / 2);//設定左邊距和上邊距matrix.postTranslate(0, 0);//繪製旋轉圖片canvas.drawBitmap(rotateImg, matrix, paint);//休眠控制最大幀率為每秒3繪製30次Thread.sleep(30);
第四,之所以將此surfaceview設成頂層透明,原因是雜家想探索在底層surfaceview預覽網路攝影機視頻的時候,在頂層的surfaceview繪製一個旋轉的圖片動畫。但遺憾的是,圖片確實是旋轉了,底層的網路攝影機預覽也正常。但就是旋轉的圖片將預覽畫面擋住了。唉,看來前文http://blog.csdn.net/yanzi1225627/article/details/8580034這裡的360掃描二維碼的介面不是通過這種思路實現的,抑或是
我旋轉的圖片不是透明的???