安卓大图片处理的入门
来源:互联网 发布:福运来彩票源码 编辑:程序博客网 时间:2024/04/30 00:13
先上图:
先来看看 主界面的文件:
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); //设置图片
然后运行成功
- 安卓大图片处理的入门
- 大图片的压缩处理
- 本地大图片的处理
- 安卓读取本机大图片
- 安卓解决大图片内存溢出
- Android的Bitmap处理大图片解决方法
- Android的Btimap处理大图片解决方法
- Android的Btimap处理大图片解决方案
- Android处理大图片
- iOS处理大图片
- Android大图片处理
- Android 处理大图片
- Android大图片处理
- Android大图片处理
- android_62_大图片处理
- 安卓大图片不失真拉拽放大缩小
- 深入安卓大图片处理机制,本地及网络图片不加载到内存预压缩
- android 加载大图片处理
- flask cache with memcache
- 原因为什么我不能访问Facebook
- sql 生成拼音简码函数
- POJ 1006 ——Biorhythms 中国剩余定理模板
- 树形DP
- 安卓大图片处理的入门
- hibernate数据类型,标准sql数据类型之间的对应表
- 解析DBR操作系统引导记录数据
- JAVA Drp项目实战—— Unable to compile class for JSP 一波三折
- poj2352 Stars 树状数组
- C++容器类对象函数参数问题
- 原因为什么它不值得做你自己的网站
- C语言详解 - 枚举类型
- 印度花岗岩砖 - 以帮助您通过自然生活