Android編程實現(xiàn)壓縮圖片并加載顯示的方法
本文實例講述了Android編程實現(xiàn)壓縮圖片并加載顯示的方法。分享給大家供大家參考,具體如下:
解析:
圖片壓縮的關(guān)鍵就是
options.inSampleSize = scale;
如果scale > 0,表示圖片進行了壓縮
/** * 壓縮圖片 * @author chen.lin * */ public class LoadImageActivity extends Activity implements OnClickListener { private Button mBtnLoad; private ImageView mImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_load); initViews(); } private void initViews() { mBtnLoad = (Button) findViewById(R.id.btnLoadImage); mImageView = (ImageView) findViewById(R.id.imageView); mBtnLoad.setOnClickListener(this); } @Override public void onClick(View v) { if (v == mBtnLoad) { Options options = new Options(); BitmapFactory.decodeFile("/sdcard/images/1.jpg", options); //不去真的解析圖片,只是獲取圖片的頭部信息,寬高 options.inJustDecodeBounds = true; //得到圖片的真實寬高 int imageHeight = options.outHeight; int imageWidth = options.outWidth; //得到屏幕的寬高 WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); int screenHeight = wm.getDefaultDisplay().getHeight(); int screenWidth = wm.getDefaultDisplay().getWidth(); //得到縮放比例 int scale = 1; int scaleX = imageWidth / screenWidth; int scaleY = imageHeight / screenHeight; if (scaleX > scaleY & scaleX >=1) {//表示如果寬的縮放比例大于高的,并且scaleX>=1都為true scale = scaleX; } if (scaleY > scaleX & scaleY >=1) {//表示如果高的縮放比例大于寬的,并且scaleY>=1都為true scale = scaleY; } //解析圖片 options.inJustDecodeBounds = false; //修改圖片的縮放比例,如果scale=4說明圖片縮小4倍,像數(shù)=1/16 options.inSampleSize = scale; Bitmap bm = BitmapFactory.decodeFile("/sdcard/images/1.jpg", options); mImageView.setImageBitmap(bm); } } }
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/btnLoadImage" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="loadImage" android:text="加載圖片" /> </LinearLayout>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android中監(jiān)聽系統(tǒng)網(wǎng)絡(luò)連接打開或者關(guān)閉的實現(xiàn)代碼
本篇文章對Android中監(jiān)聽系統(tǒng)網(wǎng)絡(luò)連接打開或者關(guān)閉的實現(xiàn)用實例進行了介紹。需要的朋友參考下2013-05-05Afianl框架里面的FinalBitmap加載網(wǎng)絡(luò)圖片
這篇文章主要介紹了Afianl框架里面的FinalBitmap加載網(wǎng)絡(luò)圖片的相關(guān)資料,需要的朋友可以參考下2015-07-07OpenHarmony實現(xiàn)屏幕亮度動態(tài)調(diào)節(jié)方法詳解
大家在拿到dayu之后,都吐槽說,會經(jīng)常熄屏,不利于調(diào)試,那么有沒有一種辦法,可以讓app不熄屏呢,答案是有的,今天我們就來揭秘一下,如何控制屏幕亮度2022-11-11解決Android Studio 格式化 Format代碼快捷鍵問題
這篇文章主要介紹了解決Android Studio 格式化 Format代碼快捷鍵問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android給自定義按鍵添加廣播和通過廣播給當前焦點輸入框賦值
這篇文章主要介紹了Android給自定義按鍵添加廣播和通過廣播給當前焦點輸入框賦值的相關(guān)資料,需要的朋友可以參考下2016-10-10內(nèi)存泄露導致Android?中setVisibility()?失效原理
這篇文章主要介紹了內(nèi)存泄露導致Android?中setVisibility()?失效原理,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-07-07