編輯:Android 開發實例
圖片瀏覽器幾乎是每支智慧型手機的標配:能放大利圖、能產生縮圖,還能把直拍的照片轉成橫式觀看。這些效果背後都需要對圖片做幾何變換,而 Android 中負責這件事的類別就是 Matrix。
1. 載入圖片
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fengfly);
2. 取得原始寬高
int height = bitmap.getHeight(); int width = bitmap.getWidth();
3. 決定目標尺寸
int nHeight = 150; int nWidth = 180;
4. 計算縮放比例
float scaleWidth = ((float) nWidth) / width; float scaleHeight = ((float) nHeight) / height;
5. 建立 Matrix 物件
Matrix matrix = new Matrix();
6. 套用縮放
matrix.postScale(scaleWidth, scaleHeight);
7. 產生新圖片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
旋轉只需要改動 Matrix 的角度,最後產生圖片的方式與縮放完全相同:
matrix.postRotate(60);
得到新的 Bitmap 之後,把它設定給 ImageView 或任何繪圖元件即可顯示。
postScale 與 postRotate 可以串在這個 Matrix 上,變換會依呼叫順序累加;若想先旋轉再縮放,注意順序會影響最終結果。bitmap.recycle(),並善用 BitmapFactory.Options 的 inSampleSize 在解碼階段就先降採樣。
Android 多語系支援:從 values-it 談字串本地化
一支上架的應用程式,使用者可能來自任何國家。要讓不同語言環境的人都讀得懂,就必須把介面文字、數字格式、日期寫法都調整成當地的習慣,這項工作稱為本地化。 Android 的
用 ActivityGroup 與 GridView 打造 Tab 分頁框架
翻開任何一款 Android 瀏覽器或客戶端軟體,幾乎都是 Tab 分頁的界面框架。很多人直覺會想到 TabHost 加 TabActivity 的組合,但在實際專案中最
Android記事本範例剖析(一):入口、清單與 Intent 匹配機制
這是 Android 記事本範例系列的第一篇。與其直接背誦 API,不如從一個完整的小程式開始,觀察入口、列表、Intent 與 ContentProvider 如何串起
Android 中顯示 GIF 動畫的實作程式碼詳解
本文為大家分享 Android 中顯示 GIF 動畫的實作程式碼,值得參考,具體如下:GIF 動畫在 Android 中還算常見,像新浪微博裡就有大量 GIF 圖片,而且