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

首頁 > 系統 > Android > 正文

Android編程實現壓縮圖片并加載顯示的方法

2019-10-22 18:25:42
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程實現壓縮圖片并加載顯示的方法。分享給大家供大家參考,具體如下:

解析:

圖片壓縮的關鍵就是

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倍,像數=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>

 

希望本文所述對大家Android程序設計有所幫助。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 分宜县| 微博| 祁东县| 台中县| 岫岩| 合阳县| 承德县| 葵青区| 军事| 南开区| 建昌县| 德庆县| 祥云县| 卢龙县| 青神县| 怀化市| 宁波市| 正定县| 浦北县| 富裕县| 都江堰市| 湄潭县| 福建省| 保德县| 双城市| 宜黄县| 邛崃市| 乌海市| 晋中市| 尉犁县| 张北县| 石林| 仁寿县| 东山县| 深泽县| 嘉兴市| 通江县| 靖州| 富民县| 白城市| 藁城市|