Android編程實(shí)現(xiàn)壓縮圖片并加載顯示的方法
本文實(shí)例講述了Android編程實(shí)現(xiàn)壓縮圖片并加載顯示的方法。分享給大家供大家參考,具體如下:
解析:
圖片壓縮的關(guān)鍵就是
options.inSampleSize = scale;
如果scale > 0,表示圖片進(jìn)行了壓縮
/**
* 壓縮圖片
* @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;
//得到圖片的真實(shí)寬高
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ā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android圖片壓縮的3種方法實(shí)例
- Android拍照得到全尺寸圖片并進(jìn)行壓縮
- android 將圖片壓縮到指定的大小的示例
- Android實(shí)現(xiàn)圖片壓縮(bitmap的六種壓縮方式)
- Android圖片壓縮以及優(yōu)化實(shí)例
- android異步加載圖片并緩存到本地實(shí)現(xiàn)方法
- Android中Glide加載庫的圖片緩存配置究極指南
- Android實(shí)現(xiàn)加載廣告圖片和倒計(jì)時(shí)的開屏布局
- Android 異步加載圖片分析總結(jié)
- Android開發(fā)之加載圖片的方法
- Android Glide圖片加載(加載監(jiān)聽、加載動(dòng)畫)
相關(guān)文章
Android實(shí)現(xiàn)大圖滾動(dòng)顯示效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)大圖滾動(dòng)顯示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android創(chuàng)建簡單發(fā)送和接收短信應(yīng)用
收發(fā)短信應(yīng)該是每個(gè)手機(jī)最基本的功能之一了,即使是許多年前的老手機(jī)也都會(huì)具備這項(xiàng)功能,而Android 作為出色的智能手機(jī)操作系統(tǒng),自然也少不了在這方面的支持。今天我們開始自己創(chuàng)建一個(gè)簡單的發(fā)送和接收短信的應(yīng)用,需要的朋友可以參考下2016-04-04
Android動(dòng)態(tài)自定義圓形進(jìn)度條
這篇文章主要介紹了Android動(dòng)態(tài)自定義圓形進(jìn)度條,需要的朋友可以參考下2017-03-03
Android布局中g(shù)ravity與layout_gravity屬性說明
這篇文章主要介紹了Android布局中g(shù)ravity與layout_gravity屬性說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
android 無須root截圖方案的實(shí)現(xiàn)
這篇文章主要介紹了android 無須root截圖方案的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03
詳解Retrofit 動(dòng)態(tài)參數(shù)(非固定參數(shù)、非必須參數(shù))(Get、Post請(qǐng)求)
這篇文章主要介紹了詳解Retrofit 動(dòng)態(tài)參數(shù)(非固定參數(shù)、非必須參數(shù))(Get、Post請(qǐng)求),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
Android WebView實(shí)現(xiàn)文件下載功能
這篇文章主要為大家詳細(xì)介紹了Android WebView實(shí)現(xiàn)文件下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
淺談AnDroidDraw+DroidDraw實(shí)現(xiàn)Android程序UI設(shè)計(jì)的分析說明
本篇文章是對(duì)AnDroidDraw+DroidDraw實(shí)現(xiàn)Android程序UI設(shè)計(jì)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android仿今日頭條APP實(shí)現(xiàn)下拉導(dǎo)航選擇菜單效果
這篇文章主要為大家詳細(xì)介紹了Android仿今日頭條APP實(shí)現(xiàn)下拉導(dǎo)航選擇菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06

