国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android 實現加載大圖片的方法

2019-10-23 18:32:40
字體:
來源:轉載
供稿:網友

項目簡介:

該項目為加載大圖片

詳細介紹:

對于超大的圖片,如果不縮放的話,容易導致內存溢出。而經過處理后,無論多大的圖片,都能夠在手機屏幕上加載出來,不會導致內存溢出。當然,臉黑的除外

該應用涉及到的知識有:

- 1.Bitmap的使用

- 2.Android手機中android/141094.html">加載圖片的原理

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

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

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

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

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

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

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

步驟:

1.創(chuàng)建一個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); }}

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持VEVB武林網!


注:相關教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 察雅县| 无为县| 昌图县| 阿克陶县| 通山县| 江川县| 阆中市| 沛县| 道真| 茂名市| 宁安市| 静安区| 金寨县| 明星| 浪卡子县| 绥阳县| 普陀区| 大足县| 景德镇市| 金华市| 驻马店市| 永宁县| 侯马市| 江陵县| 乐至县| 萨迦县| 泽州县| 鹿泉市| 乌拉特后旗| 湘潭市| 宽甸| 顺义区| 托克逊县| 准格尔旗| 岚皋县| 扶余县| 葵青区| 鹤山市| 塘沽区| 时尚| 威宁|