ImageView使用(適屏、縮放功能)

來源:互聯網
上載者:User

標籤:des   android   style   blog   color   使用   io   for   

1、適屏

提取手機的圖片庫,並且進行選擇圖片的功能:

Button onClick:Intent intent = new Intent(                    Intent.ACTION_PICK,                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// 開啟手機的圖片庫            startActivityForResult(intent, IMAGE_SELECT);

處理圖片,按照手機的螢幕大小顯示:

if (requestCode == IMAGE_SELECT) {                Uri url = data.getData();// 獲得圖片的路徑                int dw = getWindowManager().getDefaultDisplay().getWidth();// 獲得螢幕的寬度                int dh = getWindowManager().getDefaultDisplay().getWidth() / 2;// 獲得螢幕的高度/2                try {                    /**                     * BitampFactory :可以建立一個Bitmap位元影像對象                     * BitampFactory.Options為匿名內部類                     */                    // 實現對圖片的裁剪的類,是一個匿名內部類                    BitmapFactory.Options factory = new BitmapFactory.Options();                    factory.inJustDecodeBounds = true;// 如果設定為true,允許查詢圖片不是按照像素分配給記憶體                    // 將一個流轉換為Bitmap位元影像對象                    Bitmap bmp = BitmapFactory.decodeStream(                            getContentResolver().openInputStream(url), null,                            factory);                    // 對圖片的寬度和高度對應手機的螢幕進行匹配                    int hRatio = (int) Math                            .ceil(factory.outHeight / (float) dh);                    // 如果大於1 表示圖片的高度大於手機螢幕的高度                    int wRatio = (int) Math.ceil(factory.outWidth / (float) dw);                    // 如果大於1表示圖片的寬度大於手機螢幕的寬度                    // 縮放到1/radio的尺寸 和1/radio^2像素                    if (hRatio > 1 || wRatio > 1) {                        if (hRatio > wRatio) {                            factory.inSampleSize = hRatio;                        } else {                            factory.inSampleSize = wRatio;                        }                    }                    factory.inJustDecodeBounds = false;                    // 使用BitmapFactory對圖片進行適屏的操作                    bmp = BitmapFactory.decodeStream(getContentResolver()                            .openInputStream(url), null, factory);                    imageView.setImageBitmap(bmp);                } catch (Exception e) {                    // TODO: handle exception                }            }

2、縮放:

    Intent intent2 = getImageClipIntent();            startActivityForResult(intent2, IMAGE_CUT);
 if (requestCode == IMAGE_CUT) {                Bitmap bitmap = data.getParcelableExtra("data");                imageView.setImageBitmap(bitmap);            }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.