1. 程式人生 > >Android Bitmap和Canvas

Android Bitmap和Canvas

 

點陣圖是我們開發中最常用的資源,畢竟一個漂亮的介面對使用者是最有吸引力的。

1. 從資源中獲取點陣圖

可以使用BitmapDrawable或者BitmapFactory來獲取資源中的點陣圖。

當然,首先需要獲取資源:

        Resources res=getResources();

使用BitmapDrawable獲取點陣圖

  1. 使用BitmapDrawable (InputStream is)構造一個BitmapDrawable;
  2. 使用BitmapDrawable類的getBitmap()獲取得到點陣圖;
// 讀取InputStream並得到點陣圖 InputStream is=res.openRawResource(R.drawable.pic180); BitmapDrawable bmpDraw=new
BitmapDrawable(is); Bitmap bmp=bmpDraw.getBitmap();

或者採用下面的方式:

BitmapDrawable bmpDraw=(BitmapDrawable)res.getDrawable(R.drawable.pic180); Bitmap bmp=bmpDraw.getBitmap();

使用BitmapFactory獲取點陣圖

(Creates Bitmap objects from various sources, including files, streams, and byte-arrays.)

使用BitmapFactory類decodeStream(InputStream is)解碼點陣圖資源,獲取點陣圖。

     Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180);

BitmapFactory的所有函式都是static,這個輔助類可以通過資源ID、路徑、檔案、資料流等方式來獲取點陣圖。

以上方法在程式設計的時候可以自由選擇,在Android SDK中說明可以支援的圖片格式如下:png (preferred), jpg (acceptable), gif (discouraged),和bmp(Android SDK Support Media Format)。

2. 獲取點陣圖的資訊

要獲取點陣圖資訊,比如點陣圖大小、畫素、density、透明度、顏色格式等,獲取得到Bitmap就迎刃而解了,這些資訊在

Bitmap的手冊中,這裡只是輔助說明以下2點:

  • 在Bitmap中對RGB顏色格式使用Bitmap.Config定義,僅包括ALPHA_8、ARGB_4444、ARGB_8888、RGB_565,缺少了一些其他的,比如說RGB_555,在開發中可能需要注意這個小問題;
  • Bitmap還提供了compress()介面來壓縮圖片,不過AndroidSAK只支援PNG、JPG格式的壓縮;其他格式的需要Android開發人員自己補充了。
3. 顯示點陣圖

顯示點陣圖可以使用核心類Canvas,通過Canvas類的drawBirmap()顯示點陣圖,或者藉助於BitmapDrawable來將Bitmap繪製到Canvas。當然,也可以通過BitmapDrawable將點陣圖顯示到View中。

轉換為BitmapDrawable物件顯示點陣圖

        // 獲取點陣圖
        Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180);
        // 轉換為BitmapDrawable物件
        BitmapDrawable bmpDraw=new BitmapDrawable(bmp);
        // 顯示點陣圖
        ImageView iv2 = (ImageView)findViewById(R.id.ImageView02);
       iv2.setImageDrawable(bmpDraw);

使用Canvas類顯示點陣圖

這兒採用一個繼承自View的子類Panel,在子類的OnDraw中顯示

public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new Panel(this)); } class Panel extends View{ public Panel(Context context) { super(context); } public void onDraw(Canvas canvas){ Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180); canvas.drawColor(Color.BLACK); canvas.drawBitmap(bmp, 10, 10, null); } } }
4. 點陣圖縮放

(1)將一個位圖按照需求重畫一遍,畫後的點陣圖就是我們需要的了,與點陣圖的顯示幾乎一樣:drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)。

(2)在原有點陣圖的基礎上,縮放原點陣圖,建立一個新的點陣圖:CreateBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)

(3)藉助Canvas的scale(float sx, float sy) (Preconcat the current matrix with the specified scale.),不過要注意此時整個畫布都縮放了。

(4)藉助Matrix:

 Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180); Matrix matrix=new Matrix(); matrix.postScale(0.2f, 0.2f); Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(), bmp.getHeight(),matrix,true); canvas.drawColor(Color.BLACK); canvas.drawBitmap(dstbmp, 10, 10, null);
5. 點陣圖旋轉

同樣,點陣圖的旋轉也可以藉助Matrix或者Canvas來實現。

 Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180); Matrix matrix=new Matrix(); matrix.postScale(0.8f, 0.8f); matrix.postRotate(45); Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(), bmp.getHeight(),matrix,true); canvas.drawColor(Color.BLACK); 
            canvas.drawBitmap(dstbmp, 10, 10, null);

旋轉效果:

image

image

6.圖片水印的生成方法

生成水印的過程。其實分為三個環節:第一,載入原始圖片;第二,載入水印圖片;第三,儲存新的圖片。

  • /**
  •     * create the bitmap from a byte array
  •     *
  •     * @param src the bitmap object you want proecss
  •     * @param watermark the water mark above the src
  •     * @return return a bitmap object ,if paramter's length is 0,return null
  •     */
  • private Bitmap createBitmap( Bitmap src, Bitmap watermark )  
  •    {  
  •        String tag = "createBitmap";  
  •        Log.d( tag, "create a new bitmap" );  
  • if( src == null )  
  •        {  
  • returnnull;  
  •        }  
  • int w = src.getWidth();  
  • int h = src.getHeight();  
  • int ww = watermark.getWidth();  
  • int wh = watermark.getHeight();  
  • //create the new blank bitmap
  •        Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );//建立一個新的和SRC長度寬度一樣的點陣圖 
  •        Canvas cv = new Canvas( newb );  
  • //draw src into 
  •        cv.drawBitmap( src, null );//在 0,0座標開始畫入src 
  • //draw watermark into 
  •        cv.drawBitmap( watermark, w - ww + 5, h - wh + 5null );//在src的右下角畫入水印 
  • //save all clip 
  •        cv.save( Canvas.ALL_SAVE_FLAG );//儲存 
  • //store 
  •        cv.restore();//儲存 
  • return newb;  
  •    } 
7.Canvas的save和restore

onDraw方法會傳入一個Canvas物件,它是你用來繪製控制元件視覺介面的畫布。

在onDraw方法裡,我們經常會看到呼叫save和restore方法,它們到底是幹什麼用的呢?

❑ save:用來儲存Canvas的狀態。save之後,可以呼叫Canvas的平移、放縮、旋轉、錯切、裁剪等操作。

❑ restore:用來恢復Canvas之前儲存的狀態。防止save後對Canvas執行的操作對後續的繪製有影響。

save和restore要配對使用(restore可以比save少,但不能多),如果restore呼叫次數比save多,會引發Error。save和restore之間,往往夾雜的是對Canvas的特殊操作。

例如:我們先想在畫布上繪製一個右向的三角箭頭,當然,我們可以直接繪製,另外,我們也可以先把畫布旋轉90°,畫一個向上的箭頭,然後再旋轉回來(這種旋轉操作對於畫圓周上的標記非常有用)。然後,我們想在右下角有個20畫素的圓,那麼,onDraw中的核心程式碼是:

int px = getMeasuredWidth();

int py = getMeasuredWidth();

// Draw background

canvas.drawRect(0, 0, px, py, backgroundPaint);

canvas.save();

canvas.rotate(90, px/2, py/2);                

// Draw up arrow

canvas.drawLine(px / 2, 0, 0, py / 2, linePaint);                

canvas.drawLine(px / 2, 0, px, py / 2, linePaint);

canvas.drawLine(px / 2, 0, px / 2, py, linePaint);

canvas.restore();

// Draw circle

canvas.drawCircle(px - 10, py - 10, 10, linePaint);

效果如圖1所示:

如果我們不呼叫save和restore會是什麼樣子呢?如圖2所示:

從這兩個圖中,我們就能看到圓圈位置的明顯差異。不進行Canvas的save和restore操作的話,所有的影象都是在畫布旋轉90°後的畫布上繪製的。當執行完onDraw方法,系統自動將畫布恢復回來。save和restore操作執行的時機不同,就能造成繪製的圖形不同。