調用系統拍照、圖庫選擇圖片並設置爲圓形—Android項目實戰—金融APP06

項目實戰:硅谷金融APPday06 調用系統拍照、圖庫選擇圖片並設置爲圓形


不否認努力,繼續加油!
學習整理重點、盲區,筆記如下:乾乾巴巴,麻麻賴賴,一點都不圓潤……

day06

內容

用戶頭像的圓形顯示

  1. 實現從本地圖庫或打開相機拍照獲取圖片,並設置爲圓形;實例已上傳GiHhub:LoadCameraMap.

  2. Bitmap 和 ImageView 相互轉化:精簡·Bitmap轉換成ImageView和ImageView轉換成Bitmap.

  3. 提供 BitmapUtil.java 實現對 bitmap 的壓縮和圓形化處理:

    public static Bitmap circleBitmap(Bitmap source) {
        int width = source.getWidth();
        Bitmap bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        canvas.drawCircle(width / 2, width / 2, width / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(source, 0, 0, paint);
        return bitmap;
    }
    //設置寬高必須使用浮點型,否則導致壓縮的比例:0
    public static Bitmap zoom(Bitmap source,float width ,float height){
        Matrix matrix = new Matrix();
        //圖片的壓縮處理;matrix 是矩陣
        matrix.postScale(width / source.getWidth(),height / source.getHeight());
        Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, false);
        return bitmap;
    }
    
  4. 調用系統拍照、圖庫選擇圖片(已上傳GitHub)

  5. 將頭像保存到本地,以便之後調用

    private void saveImage(Bitmap bitmap) {
        File filesDir;
        //判斷sd卡是否掛載
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            filesDir = this.getExternalFilesDir("");
        } else {//手機內部存儲
            filesDir = this.getFilesDir();
        }
        FileOutputStream fos = null;
        try {
            File file = new File(filesDir, "icon.png");
            fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
  6. 讀取本地保存的 頭像,注意方法寫在 onResume() 中,不過不刪除其父類的 super 方法,注意其生命週期

    private boolean readImage() {
        File filesDir;
        //判斷sd卡是否掛載
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            filesDir = this.getActivity().getExternalFilesDir("");
        }else{//手機內部存儲
            filesDir = this.getActivity().getFilesDir();
        }
        File file = new File(filesDir,"icon.png");
        if(file.exists()){
            //存儲--->內存
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            ivMeIcon.setImageBitmap(bitmap);
            return true;
        }
        return false;
    }
    

盲區

  1. 聲明:本博客根據尚硅谷項目實戰: 硅谷金融.學習整理;
  2. 關於調用相冊的 intent;Intent intent = new Intent("android.intent.action.GET_CONTENT");寫錯後,出現 BUG ,有數據,有路徑,但是設置不到 ImageView 中,排查 BUG 又花費了一個上午。
  3. 關於支付寶集成一直沒有成功;

其他筆記

金融App

  1. 金融APP01—頁面架構.
  2. 金融APP02—主頁及工具類創建
  3. 金融APP03—自定義 MyScrollView & 聯網加載數據的4種狀態的抽取及代碼優化
  4. 金融APP04—投資理財頁面實現
  5. 金融APP05—隨機飛入飛出&流式佈局自定義View的使用

商城

Android項目實戰—— 商城APP.

新聞

Android項目實戰—— 新聞APP.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章