Android 本地圖片 設定成寬高固定的背景
阿新 • • 發佈:2019-02-09
1:先獲得圖片的地址:
訪問收手機相簿:
Intent intent= new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
獲取地址並顯示在ImageView中:
效果圖:@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); selectPicturePath = cursor.getString(columnIndex); cursor.close(); // String picturePath contains the path of selected Image ImageView imageView = new ImageView(NewNoticeActivity.this); LayoutParams lp =new LinearLayout.LayoutParams(150,150); lp.rightMargin=20; imageView.setLayoutParams(lp); imageView.setBackgroundResource(R.drawable.border_grey2); imageView.setPadding(2, 2, 2, 2); imageView.setImageBitmap(Common.getImageThumbnail(selectPicturePath, 150, 150)); int index=ll_img.getChildCount(); ll_img.addView(imageView, index-1); } }
如果是在listview中顯示,先自定義介面卡,正常SimpleAdapter可以放圖片,但是隻能是drawable裡面固定圖片,如果是手機相簿裡任意一張圖片,或者網路上這些不固定地址的圖片就不行了。所以要自定義adapter繼承SimpleAdapter。重寫裡面的setViewImage(ImageView v, String value){}函式。程式碼如下:
public class NewsListAdapter extends SimpleAdapter { Context context; public NewsListAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); this.context = context; } // set the imageView using the path of image public void setViewImage(ImageView v, int value) { v.setImageResource(value); } @SuppressLint("NewApi") public void setViewImage(ImageView v, String value) { // LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 200); // v.setLayoutParams(lp); int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); v.measure(w, h); int width =v.getMeasuredWidth(); int height =v.getMeasuredHeight(); System.out.println("---tupian : "+width+" , "+ height); v.setImageBitmap(Common.getImageThumbnail(value, width, height)); // try { // Bitmap bitmap = BitmapFactory.decodeFile(value); // Bitmap newBit = Bitmap.createScaledBitmap(bitmap, // v.getLayoutParams().width, v.getLayoutParams().height, true); // v.setImageBitmap(newBit); // } catch (NumberFormatException nfe) { // v.setImageURI(Uri.parse(value)); // } } }
注:LayoutParams貌似只能設定寬高,不能獲取寬高,獲取的寬高都是0或者-2,不準確。如果是獲取寬高,還是用getMeasuredWidth()和getMeasuredHeight();
效果圖:
最後關於Common.getImageThumbnail()函式:
/** * 根據指定的影象路徑和大小來獲取縮圖 此方法有兩點好處: 1. * 使用較小的記憶體空間,第一次獲取的bitmap實際上為null,只是為了讀取寬度和高度, * 第二次讀取的bitmap是根據比例壓縮過的影象,第三次讀取的bitmap是所要的縮圖。 2. * 縮圖對於原影象來講沒有拉伸,這裡使用了2.2版本的新工具ThumbnailUtils,使 用這個工具生成的影象不會被拉伸。 * @param imagePath 影象的路徑 * @param width 指定輸出影象的寬度 * @param height 指定輸出影象的高度 * @return 生成的縮圖 */ public static Bitmap getImageThumbnail(String imagePath, int width, int height) { Bitmap bitmap = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 獲取這個圖片的寬和高,注意此處的bitmap為null try { bitmap = BitmapFactory.decodeFile(imagePath, options); } catch (Exception e) { e.printStackTrace(); } options.inJustDecodeBounds = false; // 設為 false // 計算縮放比 int h = options.outHeight; int w = options.outWidth; int beWidth = w / width; int beHeight = h / height; int be = 1; if (beWidth < beHeight) { be = beWidth; } else { be = beHeight; } if (be <= 0) { be = 1; } options.inSampleSize = be; // 重新讀入圖片,讀取縮放後的bitmap,注意這次要把options.inJustDecodeBounds 設為 false bitmap = BitmapFactory.decodeFile(imagePath, options); // 利用ThumbnailUtils來建立縮圖,這裡要指定要縮放哪個Bitmap物件 bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); return bitmap; }