篇一: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/目录下,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="itemTitle">
<item name="android:textSize">25sp</item>
<item name="android:textStyle">bold</item> </style>
</resources>
上面代码中定义了一个名为itemTitle的样式,它包含textSize,textStyle两个属性。
Style的使用
Style可以在布局文件中通过名字来引用,代码如下:
<TextView
style="@style/itemTitle"
android:text="测试样式"
/>
2.Theme
Theme可以说和Style是完全一样的,只不过Theme是针对Activity或整个项目的。
Theme的定义
Theme定义在theme.xml中,创建在res/values/目录下,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="custom_background_color">#FFFFFFFF</color>
<style name="RiverTheme" parent="android:Theme.Light">
<item name="android:windowBackground">@color/custom_background_color</item> </style>
</resources>
上面代码中定义了一个名为RiverTheme的主题,它包含一个windowBackgroud属性。这里继承了系统的theme.light,一般theme是继承的,这样可以对默认的风格不必重复定义。本例定义了一个背景色。这里背景色要单独声明,不能在item元素中直接写颜色值,会提示语法错误。
Theme的使用
Theme可以在Manifest文件中通过名字来引用,代码如下:
<activity .............
android:theme="@style/RiverTheme">
4.1.2 Matrix实现图片的几何操作
在Android中,若想对图片进行缩放,旋转等操作,就需要使用Matrix类。Matrix的操作,总共分为translate(平移),rotate(旋转),scale(缩放)和skew(倾斜)四种。下面的代码展示了实现缩放和旋转两种操作的步骤:
//根据图片资源创建相应的Bitmap对象
myBmp = BitmapFactory.decodeResource(getResources(), R.drawable.im01);
//获取图片的原始宽高
bmpWidth = myBmp.getWidth();
bmpHeight = myBmp.getHeight();
//实例化matrix
Matrix matrix = new Matrix();
//设定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内容如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- 拖动条 -->
<SeekBar
android:id="@+id/seekBarId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<!-- 图片 -->
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/im01"
android:layout_centerInParent="true" />
<!-- 按钮 -->
<Button
android:id="@+id/big"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="放大"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
BitmapDemoActivity.java代码如下:
package com.spl;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
public class BitmapDemoActivity extends Activity
{
ImageView myImageView;
Bitmap myBmp, newBmp;
int bmpWidth, bmpHeight;
SeekBar seekbarRotate;
Button big;
float rotAngle, scaleRate;
@Override
public void onCreate(Bundle savedInstanceState)
{
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
Matrix matrix = new Matrix();
//设定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);
}
//按钮点击监听器
private OnClickListener bigClick = new OnClickListener() {
@Override
{public void onClick(View arg0) { Matrix matrix = new Matrix();//设定Matrix属性 x,y缩放比例为1.5matrix.postScale(scaleRate, scaleRate);newBmp = Bitmap.createBitmap(myBmp, 0, 0, bmpWidth, bmpHeight, matrix, true);myImageView.setImageBitmap(newBmp);BitmapDemoActivity.this.setTitle("scale:"+scaleRate);scaleRate *= 1.2F;// 让放大比例持续增大 }; // 拖动条监听器 } private SeekBar.OnSeekBarChangeListener onRotate=new SeekBar.OnSeekBarChangeListener()
public void onStopTrackingTouch(SeekBar seekBar)
{
}
public void onStartTrackingTouch(SeekBar seekBar)
{
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser)
{
//拖动过程中的事件处理
Matrix m = new Matrix();
m.postRotate((float)progress*3.6F);//产生一定角度的旋转
newBmp=Bitmap.createBitmap(myBmp, 0, 0, bmpWidth, bmpHeight, m, true); myImageView.setImageBitmap(newBmp);
}
};
}
4.1.3 Bitmap的使用
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);
篇二:我的图片
240x320 102k jpg
...浪漫的可爱卡通情侣图片240x...
290x400 154k gif
500x353 18k jpg
...漂亮的韩国卡通情侣 - ps实...
715x450 166k jpg
...-韩国漂亮卡通情侣大图模块 ...
140x109 2k jpg
qq非主流卡通情侣图相关图片...
1280x960 186k jpg
动漫情侣签素材
185x187 10k jpg
95x95 4k jpg
240x400 40k gif
爱情左右卡通情侣手机图片 -...
300x340 74k jpg
可爱的动漫情侣
184x415 36k gif
...片 ·甜蜜卡通情侣情人节闪...
155x400 38k jpg
非主流卡通情侣动态图片-qq...
176x220 38k gif
好看的卡通动漫情侣闪图_简...
240x320 92k gif
100x120 46k gif
非主流动态卡通情侣头像_此...
100x100 14k jpg
qq卡通情侣可爱头像_最新非...
240x320 56k gif
240x320 42k gif
可爱卡通情侣成对图片_简单...
1024x768 298k jpg
240x320 44k gif
...材 ·甜蜜卡通情侣情人节闪...
100x100 2k jpg
卡通情侣可爱头像_
240x320 44k gif
240x320 80k gif
甜蜜卡通情侣情人节闪图素材...
240x319 40k jpg
...浪漫的可爱卡通情侣图片240x...
240x320 98k jpg
可爱卡通情侣__手机图片,非...
篇三:海贼王动漫旗帜
《动漫手机壁纸》出自:百味书屋
链接地址:http://www.850500.com/news/134464.html
转载请保留,谢谢!