package com.alleged.imageLoad;
import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
private ImageView image;
/*实现逻辑
* 第一步得到当前屏幕的分辨率
* 第二步得到要加载图片的信息
* 第三步计算缩放比例
* 第四步加载目标图片
* */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView)findViewById(R.id.showImage);
}
public void loadImage(View v){
//得到当前屏幕的分辨率
//创建WindowManger类对象
WindowManager window = (WindowManager)getSystemService(WINDOW_SERVICE);
//screenWidth代表屏幕的宽 screenHeight代表屏幕的高
int screenWidth = window.getDefaultDisplay().getWidth();
int screenHight = window.getDefaultDisplay().getHeight();
//获取图片的信息
BitmapFactory.Options options = new Options();
//只加载信息 不加载内容
options.inJustDecodeBounds = true;
//把一个图片文件 解码到bitmap中 只加载了文件信息
BitmapFactory.decodeFile("/mnt/sdcard/6.jpg",options);
//得到图片的宽 高
int imageWidth = options.outWidth;
int imageHeight = options.outHeight;
//计算缩放比
int scale = 1;
int scalex = imageWidth/screenWidth;
int scaley = imageHeight/screenHight;
if(scalex>scale&&scalex>scaley){
scale =scalex;
}else if(scaley>scale&&scaley>scalex){
scale =scaley;
}else{
scale = 1;
}
System.out.println(scale);
//正式加载图片
//设置缩放比
options.inSampleSize=scale;
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile("/mnt/sdcard/6.jpg",options);
image.setImageBitmap(bitmap);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
布局文件
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context="com.alleged.imageLoad.MainActivity" >
<Button
android:id="@+id/imageLoad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加载"
android:onClick="loadImage"/>
<ImageView
android:id="@+id/showImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>