Android图片处理与动画.docx

上传人:b****5 文档编号:6659701 上传时间:2023-01-08 格式:DOCX 页数:34 大小:901.85KB
下载 相关 举报
Android图片处理与动画.docx_第1页
第1页 / 共34页
Android图片处理与动画.docx_第2页
第2页 / 共34页
Android图片处理与动画.docx_第3页
第3页 / 共34页
Android图片处理与动画.docx_第4页
第4页 / 共34页
Android图片处理与动画.docx_第5页
第5页 / 共34页
点击查看更多>>
下载资源
资源描述

Android图片处理与动画.docx

《Android图片处理与动画.docx》由会员分享,可在线阅读,更多相关《Android图片处理与动画.docx(34页珍藏版)》请在冰豆网上搜索。

Android图片处理与动画.docx

Android图片处理与动画

第四章图片处理

手机应用离不开图片。

图片不但可以给应用带来美观的界面,而且可以为用户提供丰富的功能和体验,在当下很难想象一个完全由文本组成的手机软件;而在开发过程中,对图片的加载,缓存,显示等处理又会直接影响整个项目的应能。

所以,在Android中对图片处理的重要性不言而喻。

本章中读者应该着重掌握如下内容:

(1)使用Matrix对图片进行变换

(2)Bitmap的操作

(3)图片异步加载框架的使用

4.1图片处理

4.1.1使用Style和Theme创建样式与主题

如果我们平时注意观察了那些成熟的Android应用,就会发现它们大都使用一种统一的风格和样式贯穿整个项目,例如统一的背景色或背景图片,统一的标题栏,统一的按钮样式,统一的字体等等。

而这种“统一”就来自于Style(样式)和Theme(主题)的使用

1.Style

Style从本质上讲就是一些属性的集合,例如:

layout_width,layout_height,textSize,textColor等等,Style将这些属性定义在xml文件中,供其他布局文件中的控件引用。

其角色类似于页面中的css,将样式单独抽离出来,方便修改和重用。

Style的定义

Style定义在styles.xml中,创建在res/values/目录下,代码如下:

xmlversion="1.0"encoding="utf-8"?

>

textSize">25sp

textStyle">bold

上面代码中定义了一个名为itemTitle的样式,它包含textSize,textStyle两个属性。

Style的使用

Style可以在布局文件中通过名字来引用,代码如下:

style="@style/itemTitle"

android:

text="测试样式"

/>

2.Theme

Theme可以说和Style是完全一样的,只不过Theme是针对Activity或整个项目的。

Theme的定义

Theme定义在theme.xml中,创建在res/values/目录下,代码如下:

xmlversion="1.0"encoding="utf-8"?

>

#FFFFFFFF

Theme.Light">

windowBackground">@color/custom_background_color

上面代码中定义了一个名为RiverTheme的主题,它包含一个windowBackgroud属性。

这里继承了系统的theme.light,一般theme是继承的,这样可以对默认的风格不必重复定义。

本例定义了一个背景色。

这里背景色要单独声明,不能在item元素中直接写颜色值,会提示语法错误。

Theme的使用

Theme可以在Manifest文件中通过名字来引用,代码如下:

android:

theme="@style/RiverTheme">

4.1.2Matrix实现图片的几何操作

在Android中,若想对图片进行缩放,旋转等操作,就需要使用Matrix类。

Matrix的操作,总共分为translate(平移),rotate(旋转),scale(缩放)和skew(倾斜)四种。

下面的代码展示了实现缩放和旋转两种操作的步骤:

//根据图片资源创建相应的Bitmap对象

myBmp=BitmapFactory.decodeResource(getResources(),R.drawable.im01);

//获取图片的原始宽高

bmpWidth=myBmp.getWidth();

bmpHeight=myBmp.getHeight();

//实例化matrix

Matrixmatrix=newMatrix();

//设定Matrix属性x,y缩放比例为1.5

matrix.postScale(1.5F,1.5F);

//顺时针旋转45度

matrix.postRotate(45.0F);

//根据Matrix的设定产生新的Bitmap对象

newBmp=Bitmap.createBitmap(myBmp,0,0,bmpWidth,bmpHeight,matrix,true);

在上面代码中,matrix的方法postScale和postRotate分别用来对图片的缩放和旋转进行设定。

缩放和旋转都围绕着一个中心点来进行,在默认情况下,中心点为(0,0),该点位于图片的物理中心。

实例BitmapDemo演示了对图片的旋转和缩放,如图4-1所示,拖动界面上方的拖动条(SeekBar),可以顺时针旋转图片;点击下方按钮,可以放大图片。

图4-1图片的旋转和缩放

布局文件main.xml内容如下:

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

layout_width="fill_parent"

android:

layout_height="fill_parent">

--拖动条-->

android:

id="@+id/seekBarId"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

layout_alignParentTop="true"/>

--图片-->

android:

id="@+id/imageview"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

src="@drawable/im01"

android:

layout_centerInParent="true"/>

--按钮-->

android:

id="@+id/big"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="放大"

android:

layout_alignParentBottom="true"

android:

layout_centerHorizontal="true"

/>

BitmapDemoActivity.java代码如下:

packagecom.spl;

importandroid.app.Activity;

importandroid.graphics.Bitmap;

importandroid.graphics.BitmapFactory;

importandroid.graphics.Matrix;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.ImageView;

importandroid.widget.SeekBar;

publicclassBitmapDemoActivityextendsActivity

{

ImageViewmyImageView;

BitmapmyBmp,newBmp;

intbmpWidth,bmpHeight;

SeekBarseekbarRotate;

Buttonbig;

floatrotAngle,scaleRate;

@Override

publicvoidonCreate(BundlesavedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

myImageView=(ImageView)findViewById(R.id.imageview);

//根据图片资源创建相应的Bitmap对象

myBmp=BitmapFactory.decodeResource(getResources(),R.drawable.im01);

//获取图片的原始宽高

bmpWidth=myBmp.getWidth();

bmpHeight=myBmp.getHeight();

scaleRate=1.2F;

//实例化matrix

Matrixmatrix=newMatrix();

//设定Matrix属性x,y缩放比例为1.5

matrix.postScale(1.5F,1.5F);

//顺时针旋转45度

matrix.postRotate(45.0F);

//根据Matrix的设定产生新的Bitmap对象

newBmp=Bitmap.createBitmap(myBmp,0,0,bmpWidth,bmpHeight,matrix,true);

seekbarRotate=(SeekBar)findViewById(R.id.seekBarId);

seekbarRotate.setOnSeekBarChangeListener(onRotate);

big=(Button)findViewById(R.id.big);

big.setOnClickListener(bigClick);

}

//按钮点击监听器

privateOnClickListenerbigClick=newOnClickListener(){

@Override

publicvoidonClick(Viewarg0){

Matrixmatrix=newMatrix();

//设定Matrix属性x,y缩放比例为1.5

matrix.postScale(scaleRate,scaleRate);

newBmp=Bitmap.createBitmap(myBmp,0,0,bmpWidth,bmpHeight,matrix,true);

myImageView.setImageBitmap(newBmp);

BitmapDemoActivity.this.setTitle("scale:

"+scaleRate);

scaleRate*=1.2F;//让放大比例持续增大

}

};

//拖动条监听器

privateSeekBar.OnSeekBarChangeListeneronRotate=newSeekBar.OnSeekBarChangeListener(){

publicvoidonStopTrackingTouch(SeekBarseekBar)

{

}

publicvoidonStartTrackingTouch(SeekBarseekBar)

{

}

publicvoidonProgressChanged(SeekBarseekBar,intprogress,

booleanfromUser)

{

//拖动过程中的事件处理

Matrixm=newMatrix();

m.postRotate((float)progress*3.6F);//产生一定角度的旋转

newBmp=Bitmap.createBitmap(myBmp,0,0,bmpWidth,bmpHeight,m,true);

myImageView.setImageBitmap(newBmp);

}

};

}

4.1.3Bitmap的使用

Bitmap称为点阵图像或位图图像,是由像素组成的,每个像素都可以看成颜色及透明度等信息的二进制编码单位,多个像素在一个平面上的二维排列就构成了Bitmap。

Bitmap是Android中处理图像最重要的类之一。

一张图片要想显示在Android应用中,必须先将图片文件的信息内容读取到Bitmap中。

Bitmap位于android.graphics包中,它不提供对外的构造方法,只能通过BitmapFactory的静态方法来实例化。

BitmapFactory提供了多个方法来获取Bitmap实例,下面给大家逐一介绍:

1)从文件获取

myBmp=BitmapFactory.decodeFile(pathName);

myBmp=BitmapFactory.decodeFile(pathName,opts);

pathName为图片的绝对路径,一般为SDCard上的路径。

2)从资源中获取

myBmp=BitmapFactory.decodeResource(res,id);

myBmp=BitmapFactory.decodeResource(res,id,opts);

res为资源对象,一般为getResources()。

id为图片的资源id,在R文件中有定义,例如:

R.drawable.img

3)从流中获取

myBmp=BitmapFactory.decodeStream(is);

myBmp=BitmapFactory.decodeStream(is,outPadding,opts);

is是InputStream对象,该输入流的来源可以是本地文件,也可以是网上资源

4)从字节数组中获取

myBmp=BitmapFactory.decodeByteArray(data,offset,length);

myBmp=BitmapFactory.decodeByteArray(data,offset,length,opts);

data为字节数组

offset为在数组中从第几个下标开始解析

length是解析的长度

以上所有方法都有一个带opts参数的重载,这个opts参数的类型是BitmapFactory.Options,通过它我们可以在图片加载的尺寸,对内存的占用等方面进行设定,从而达到防止内存溢出,提高运行速度等效果。

BitmapFactory.Options的几个常用属性和使用样例见下面代码:

BitmapFactory.Optionsopts=newBitmapFactory.Options();

opts.outWidth=200;//输入缩略图宽度为200

opts.outHeight=200;//输出缩略图高度为200

//如果设置为TRUE,不获取图片,不加载到内存。

但是会把图片的高度和宽度都获取到

opts.inJustDecodeBounds=true;

//设为2代表宽和高都是原来的1/2,则图是原来的1/4;且在内存所占空间也是原来的1/4

opts.inSampleSize=2;

从以上代码可以看出,多数Options的属性都有降低资源消耗的作用,这为应用程序的提高效率和健壮性等方面提供了很大的帮助,使我们编写的程序更加具有专业水平。

实例BitmapSampleDemo演示了上面介绍的4中加载Bitmap的方法,运行效果如图4-2所示。

图4-2实例BitmapSampleDemo运行效果

该实例界面由4个按钮,1个ImageView以及1个ProgressBar组成。

布局文件main.xml代码如下:

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

android:

orientation="vertical">

android:

id="@+id/button1"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

text="从文件加载图片"/>

android:

id="@+id/button2"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

text="从资源加载图片"/>

android:

id="@+id/button3"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

text="从流加载图片"/>

android:

id="@+id/button4"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

text="从字节数组加载图片"/>

android:

layout_width="fill_parent"

android:

layout_height="fill_parent">

--图片-->

android:

id="@+id/imageView1"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_centerInParent="true"/>

--进度条-->

android:

id="@+id/progressBar1"

style="?

android:

attr/progressBarStyleLarge"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_centerInParent="true"

android:

visibility="invisible"/>

该实例的MainActivity代码如下:

packagespl.bitmap.sample;

importjava.io.ByteArrayOutputStream;

importjava.io.InputStream;

import.HttpURLConnection;

import.URL;

importandroid.app.Activity;

importandroid.content.res.AssetManager;

importandroid.graphics.Bitmap;

importandroid.graphics.BitmapFactory;

importandroid.os.Bundle;

importandroid.os.Environment;

importandroid.os.Handler;

importandroid.os.Message;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.ImageView;

importandroid.widget.ProgressBar;

publicclassMainActivityextendsActivity{

ImageViewimageview;

ProgressBarprogressBar;

Bitmapbitmap;

Handlerhandler=newHandler(){

publicvoidhandleMessage(Messagemsg){

switch(msg.what){

case0:

imageview.setImageBitmap(bitmap);//给ImageView加载Bitmap

progressBar.setVisibility(View.INVISIBLE);//隐藏进度条

break;

}

};

};

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//初始化控件

Buttonbtn1=(Button)findViewById(R.id.button1);

Buttonbtn2=(Button)findViewById(R.id.button2);

Buttonbtn3=(Button)findViewById(R.id.button3);

Buttonbtn4=(Button)findViewById(R.id.button4);

imageview=(ImageView)findViewById(R.id.imageView1);

progressBar=(ProgressBar)findVie

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 起诉状

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1