溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android應用中怎么對超大的圖片進行加載

發布時間:2020-12-02 15:25:09 來源:億速云 閱讀:371 作者:Leah 欄目:移動開發

本篇文章為大家展示了Android應用中怎么對超大的圖片進行加載,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

 1.Bitmap的使用

- 2.Android手機中加載圖片的原理

  有的時候,我們加載一張不足1M的圖片,盡管手機的堆內存有16M,仍然會導致內存溢出,why?

  這就更計算機加載圖片的原理有關了:

  1).手機會解析圖片的所有像素信息,把每個像素信息都存入到內存中

  2).Android中保存圖片是用ARGB保存的,A表示阿爾法透明度,所以一個像素點占用了4個字節

例如:一張1080*720像素的24位位圖圖片,可能實際上經過壓縮后大小只有幾十K,而在android手機加載這張圖片所需要的內存大小為:

1080*720*(3+1)=3110400 byte = 3037 KB = 2.9MB

實際上,圖片中還包含一點其他的信息,例如圖片保存的格式,使用的相機名稱,以及拍攝時間等,所以總體來說要比3110400字節大一旦,大概多上幾十個字節

步驟:

1.創建一個Android項目,編寫布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="hhh.exercise.smultimedia_a_image.MainActivity" >

 <EditText
  android:id="@+id/ed"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:text="a.jpg"
  android:textColor="#00ff00"
  android:textSize="30sp" />

 <requestFocus />

 <Button
  android:onClick="see"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="點擊看片"
  android:textColor="#00ffff"
  android:textSize="30sp" />

 <ImageView
  android:id="@+id/iv"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_gravity="center"
  android:src="@drawable/ic_launcher" />
</LinearLayout>

界面如下所示:

Android應用中怎么對超大的圖片進行加載

2.在MainActivity中編寫代碼:

public class MainActivity extends Activity {
 private EditText ed;
 private ImageView iv;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ed = (EditText) findViewById(R.id.ed);
  iv = (ImageView) findViewById(R.id.iv);
 }
 public void see(View view) {
  // 確定要加載的圖片(這里為了調試方面,把所有的圖片都放在SD卡中,然后在界面上輸入圖片的名字,根據給名字拼接字符串)
  String fileName = ed.getText().toString();
  String path = Environment.getExternalStorageDirectory().getPath()+ "/" + fileName;

  // 該類為位圖工廠(BitmapFactory)的內部類,用來封裝參數對象
  Options opts = new Options();

  // 不為像素申請內存,只獲取圖片的寬、高信息
  // inJustDecodeBound該字段設置為true,那么位圖工廠構建BitMap對象時返回的是空值,但是會把圖片的一些信息返回在Options對象中(如圖片的寬、高等)
  opts.inJustDecodeBounds = true;

  // 第二個參數是解析圖片時傳入的參數,由于可能傳入的參數過多,所以直接把所有參數封裝成一個對象
  BitmapFactory.decodeFile(path, opts);

  // 獲取圖片的額寬高
  int imgWidth = opts.outWidth;
  int imgHeight = opts.outHeight;

  // 獲取當前手機屏幕的寬高
  Display dp = getWindowManager().getDefaultDisplay();
  int screenWidth = dp.getWidth();
  int screenHeight = dp.getHeight();

  // 設置默認縮放比為1
  int scale = 1;

  // 計算圖片寬高與屏幕寬高比例,即計算寬縮放比,高縮放比
  int scaleWidth = imgWidth / screenWidth;
  int scaleHeight = imgHeight / screenHeight;

  // 選擇縮放比例,如果圖片比屏幕小,就不進行縮放.如果圖片比屏幕大,但是寬高縮放比例不同,選擇縮放比大
  if (scaleWidth >= scaleHeight && scaleWidth > 1) {
   scale = scaleWidth;
  } else if (scaleWidth < scaleHeight && scaleHeight > 1) {
   scale = scaleHeight;
  }
  // 在Options的對象中設置縮放比例
  opts.inSampleSize = scale;
  // 一定要把inJustDecodeBound該字段設置為false,實際上默認值是false,
  // 但是在前面的代碼中已經改為了true,所以要更改過來。當然,也可以重新new 一個Option是對象
  opts.inJustDecodeBounds = false;
  Bitmap bm = BitmapFactory.decodeFile(path, opts);
  iv.setImageBitmap(bm);
 }
}

上述內容就是Android應用中怎么對超大的圖片進行加載,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女