Android 怎么使用Bitmap+Canvas 自適應屏幕


我們可以使用Matrix 來放縮我們得到的Bitmap 從而使我們的BItmap適應我們的手機屏幕

首先我們得先獲取我們的手機屏幕的大小

WindowManager wm = (WindowManager) getContext().getSystemService(
                    Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();

然后我們構造一個新的Matrix對象,自己完成寫一個函數,如下:

public Bitmap resizeBitmap(Bitmap bitmap,int w,int h)
        {
            if(bitmap!=null)
            {
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                int newWidth = w;
                int newHeight = h;
                float scaleWight = ((float)newWidth)/width;
                float scaleHeight = ((float)newHeight)/height;
                Matrix matrix = new Matrix();
                matrix.postScale(scaleWight, scaleHeight);
                Bitmap res = Bitmap.createBitmap(bitmap, 0,0,width, height, matrix, true);
                return res;
                 
            }
            else{
                return null;
            }
        }

這樣我們通過這個函數返回的Bitmap對象就是可以適應我們手機屏幕大小的了。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM