android中圖片加載到內(nèi)存的實(shí)例代碼
本文演示android中圖片加載到內(nèi)存
首先設(shè)計(jì)界面:
代碼如下:
<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=".MainActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="load" android:text="加載圖片到內(nèi)存" /> <ImageView android:id="@+id/iv" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
往mnt/sdcard中上傳測(cè)試圖片
添加邏輯部分代碼:
public class MainActivity extends Activity { private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); } public void load() { Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/a.jpg"); iv.setImageBitmap(bitmap); } }
運(yùn)行代碼,產(chǎn)生錯(cuò)誤,原因是圖片太大(選取的是大照片)
可以使用BitmapFactory中包含的靜態(tài)類(lèi)Options在不解析圖片信息的前提下得到圖片的寬高信息:
BitmapFactory.Options opts = new Options(); // 不去解析圖片信息,只是得到圖片的頭部信息 寬高 opts.inJustDecodeBounds = true; BitmapFactory.decodeFile("/sdcard/a.jpg", opts); int imageHeight = opts.outHeight; int imagewidth = opts.outWidth; System.out.println("圖片寬:" + imagewidth); System.out.println("圖片高" + imageHeight);
運(yùn)行一下:
09-04 06:09:10.519: I/System.out(1812): 圖片寬:2560
09-04 06:09:10.519: I/System.out(1812): 圖片高1920
接下來(lái)得到手機(jī)屏幕的寬高:
//得到手機(jī)屏幕的寬高 WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE); int height = wm.getDefaultDisplay().getHeight(); int width = wm.getDefaultDisplay().getWidth();
可以看到getHeight與getWidth方法均已過(guò)時(shí),使用下面的方法替代:
// 得到手機(jī)屏幕的寬高 WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); Point outSize = new Point(); wm.getDefaultDisplay().getSize(outSize); // 3.0以后的版本才能使用
完整代碼如下:
package com.wuyudong.loadimage; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.graphics.Point; import android.view.Menu; import android.view.View; import android.view.WindowManager; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView iv; private int windowHeight; private int windowWidth; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); // 得到手機(jī)屏幕的寬高 WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); windowHeight = wm.getDefaultDisplay().getHeight(); windowWidth = wm.getDefaultDisplay().getWidth(); //Point outSize = new Point(); //wm.getDefaultDisplay().getSize(outSize); // 3.0以后的版本才能使用 //windowHeight = outSize.y; //windowWidth = outSize.x; } public void load(View view) { // Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/a.jpg"); // iv.setImageBitmap(bitmap); // 圖片解析的配置 BitmapFactory.Options opts = new Options(); // 不去解析圖片信息,只是得到圖片的頭部信息 寬高 opts.inJustDecodeBounds = true; BitmapFactory.decodeFile("/sdcard/a.jpg", opts); int imageHeight = opts.outHeight; int imagewidth = opts.outWidth; System.out.println("圖片寬:" + imagewidth); System.out.println("圖片高" + imageHeight); // 計(jì)算縮放比例 int scaleX = imagewidth / windowWidth; int scaleY = imageHeight / windowHeight; int scale = 1; if (scaleX > scaleY & scaleY >= 1) { scale = scaleX; } if (scaleY > scaleX & scaleX >= 1) { scale = scaleY; } //解析圖片 opts.inJustDecodeBounds = false; //采樣率 opts.inSampleSize = scale; Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/a.jpg", opts); iv.setImageBitmap(bitmap); } }
以上所述是小編給大家介紹的android中圖片加載到內(nèi)存的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android Volley圖片加載功能詳解
- Android自定義View基礎(chǔ)開(kāi)發(fā)之圖片加載進(jìn)度條
- 從源碼分析Android的Glide庫(kù)的圖片加載流程及特點(diǎn)
- 一起動(dòng)手編寫(xiě)Android圖片加載框架
- Android開(kāi)發(fā)中ImageLoder進(jìn)行圖片加載和緩存
- 深入剖析Android的Volley庫(kù)中的圖片加載功能
- Android圖片加載緩存框架Glide
- Android圖片加載的緩存類(lèi)
- Android程序開(kāi)發(fā)ListView+Json+異步網(wǎng)絡(luò)圖片加載+滾動(dòng)翻頁(yè)的例子(圖片能緩存,圖片不錯(cuò)亂)
- 設(shè)計(jì)簡(jiǎn)單的Android圖片加載框架
相關(guān)文章
Kotlin?Flow數(shù)據(jù)流的3種使用場(chǎng)景詳解
這篇文章主要為大家詳細(xì)介紹了Kotlin中Flow數(shù)據(jù)流的幾種使用場(chǎng)景,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2023-04-04Android項(xiàng)目實(shí)戰(zhàn)之Glide 高斯模糊效果的實(shí)例代碼
這篇文章主要介紹了Android項(xiàng)目實(shí)戰(zhàn)之Glide 高斯模糊效果的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06Android跳轉(zhuǎn)系統(tǒng)設(shè)置Settings的各個(gè)界面詳解
系統(tǒng)設(shè)置Settings中定義的一些常用的各界面ACTION常量,下面這篇文章主要給大家介紹了關(guān)于Android跳轉(zhuǎn)系統(tǒng)設(shè)置Settings的各個(gè)界面,文中介紹非常詳細(xì),需要的朋友可以參考下2023-01-01Android通過(guò)overScrollBy實(shí)現(xiàn)下拉視差特效
這篇文章主要為大家詳細(xì)介紹了Android通過(guò)overScrollBy實(shí)現(xiàn)下拉視差特效,實(shí)現(xiàn)精彩的阻尼效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android滾動(dòng)條廣告實(shí)現(xiàn)代碼示例
本篇文章主要介紹了Android滾動(dòng)條廣告實(shí)現(xiàn)代碼示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09Flutter 控制屏幕旋轉(zhuǎn)的實(shí)現(xiàn)
這篇文章主要介紹了Flutter 控制屏幕旋轉(zhuǎn)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Android ActionBar完全解析使用官方推薦的最佳導(dǎo)航欄(上)
Action Bar是一種新増的導(dǎo)航欄功能,在Android 3.0之后加入到系統(tǒng)的API當(dāng)中,它標(biāo)識(shí)了用戶當(dāng)前操作界面的位置,并提供了額外的用戶動(dòng)作、界面導(dǎo)航等功能2017-04-04Android編程調(diào)節(jié)屏幕亮度(背景燈)及保持背景燈常亮的方法
這篇文章主要介紹了Android編程調(diào)節(jié)屏幕亮度(背景燈)及保持背景燈常亮的方法,涉及Android屏幕相關(guān)屬性涉及技巧,需要的朋友可以參考下2016-01-01