先上图:
先来看看 主界面的文件:
activity_main.xml(为相对布局,只有两个按钮,分别用于跳转不同的界面)
<RelativeLayout 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"
tools:context=".MainActivity" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示原图"
android:onClick="unchange"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dip"
android:text="显示改变过图"
android:onClick="change"
/>
</RelativeLayout>
再来看看首界面的activity文件:
package com.baidu.bigpic;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//按钮的点击事件(进入未处理界面)
public void unchange(View v){
Intent intent=new Intent(this,Unchange.class);
startActivity(intent);
}
//按钮的点击事件(进入处理界面)
public void change(View v){
Intent intent=new Intent(this,Change.class);
startActivity(intent);
}
}
请看界面:
当然了,我们准备一张大图片在drawable资源文件夹中,并命名为c.jpg:
如图:
====================================================================================
来看看未处理界面代码:
unchange.xml
<?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" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="原图如下:"
android:textSize="30dip"
android:gravity="center_horizontal"
/>
<ImageView
android:id="@+id/iv_notchange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
看看其activity文件:
package com.baidu.bigpic;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
/**
* 没有改变界面
* @author Administrator
*
*/
public class Unchange extends Activity {
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.unchange);
img=(ImageView) findViewById(R.id.iv_notchange);
showUnchange();
}
private void showUnchange() {
// TODO Auto-generated method stub
Bitmap bt=BitmapFactory.decodeResource(this.getResources(), R.drawable.c, opts);;
img.setImageBitmap(bt);
}
}
运行会报错。因为图片太大了
===================================================================================
看看处理界面的代码:
change.xml
<?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" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="改变过的图如下:"
android:textSize="30dip"
android:gravity="center_horizontal"
/>
<ImageView
android:id="@+id/iv_change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
/>
</LinearLayout>
=========================================================================
看看activity文件:
package com.baidu.bigpic;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.ImageView;
/**
* 改变过的界面
* @author Administrator
*
*/
public class Change extends Activity {
private ImageView imgchange;
private int width;
private int height;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.change);
imgchange=(ImageView) findViewById(R.id.iv_change);
WindowManager wm= (WindowManager) getSystemService(WINDOW_SERVICE);
width=wm.getDefaultDisplay().getWidth();
height=wm.getDefaultDisplay().getHeight();
showChange();
}
private void showChange() {
BitmapFactory.Options opts=new Options();
opts.inJustDecodeBounds=true;
BitmapFactory.decodeResource(this.getResources(), R.drawable.c, opts);
int imageHeight=opts.outHeight;
int imageWidth=opts.outWidth;
int scaleY=opts.outHeight/height;//大的除以小的
int scaleX=opts.outWidth/width;
int scale=1;//如果为8,则图片为原来的1/8
if(scaleY>scaleX&&scaleX>1){
scale=scaleY;
}
if(scaleX>scaleY&&scaleY>1){
scale=scaleX;
}
opts.inJustDecodeBounds=false;
opts.inSampleSize=scale;
Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.c, opts);
imgchange.setImageBitmap(bitmap);
}
}
代码解释:
WindowManager wm= (WindowManager) getSystemService(WINDOW_SERVICE);
width=wm.getDefaultDisplay().getWidth();
height=wm.getDefaultDisplay().getHeight(); 这是获得屏幕的宽度和高度
一般来说,我们加载一张图片到内存中,平时的方式是:Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.c; 这种方式不添加任何限制,于是出现了图片过大了,而且还原封不动的被加载到内存中,这就导致出现发现内存溢出的错误.
所以我们要在这张图片加载到内存前给它点约束.即:BitmapFactory.decodeResource(this.getResources(), R.drawable.c, opts);
就是后面加了个opts.通俗点就是个约束.
好,我们来开始创造约束
BitmapFactory.Options opts=new Options();
opts.inJustDecodeBounds=true; //这句话就是,允许解析图片的边缘,以便我们取得图片的宽、高,
BitmapFactory.decodeResource(this.getResources(), R.drawable.c, opts);
int imageHeight=opts.outHeight; //图片的高
int imageWidth=opts.outWidth;//图片的宽
//既然我们要缩放图片,要确定缩放比例。大图片一般都比屏幕大。所以我们拿较大(图片的宽、高)的除以较小(屏幕的宽、高)的
int scaleY=opts.outHeight/height;//大的除以小的,如果值为8,就表示图片缩放1/8
int scaleX=opts.outWidth/width; //大的除以小的,如果值为8,就表示图片缩放1/8
if(scaleY>scaleX&&scaleX>1){
scale=scaleY;
} //我们确定这两者中较大的那个
if(scaleX>scaleY&&scaleY>1){
scale=scaleX;
}
opts.inJustDecodeBounds=false; //宽、高去结束之后,要设为false
opts.inSampleSize=scale; //确定缩放比例
Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.c, opts); //加载有缩放比例的图片
mgchange.setImageBitmap(bitmap); //设置图片
然后运行成功