Android之ImageView的实例_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Android之ImageView的实例

Android之ImageView的实例

 2011/1/1 9:07:56  aceaddi  http://aceaddi.javaeye.com  我要评论(0)
  • 摘要:推荐高焕堂的一本Android电子书籍,在瀚宇通开发网http://www.henii.com可以下载!这可是一本很好的书,想学习的就下,不想学习的路过...................下面是一个使用ImageView的实例,实例实现了图片的缩放和旋转功能:strings.xml:<?xmlversion="1.0"encoding="utf-8"?><resources><stringname="hello">HelloWorld
  • 标签:android view imageView 实例

推荐高焕堂 的一本Android电子书籍,在瀚宇通开发网 http://www.henii.com 可以下载!

这可是一本很好的书,想学习的就下,不想学习的路过...................

?

?

下面是一个使用ImageView的实例,实例实现了图片的缩放和旋转功能:

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
??? <string name="hello">Hello World, ImageViewActivity!</string>
??? <string name="app_name">ImageView实例</string>
???
??? <string name="ZoomOut">缩小</string>
??? <string name="ZoomIn">放大</string>
???
??? <string name="RotateLeft">左转</string>
??? <string name="RotateRight">右转</string>
</resources>

?

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? android:orientation="vertical" android:layout_width="fill_parent"
??? android:layout_height="fill_parent">
???
??? <TextView android:layout_width="fill_parent"
??? ??? android:layout_height="wrap_content" android:text="" />
??? ???
??? <ImageView android:id="@+id/ImageView1"
??? ??? android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
??? ???
??? <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? android:orientation="vertical" android:layout_width="fill_parent"
??? android:layout_height="wrap_content">
??? ???
??? ??? <Button android:id="@+id/Button1"
??? ??? android:text="@string/ZoomOut"
??? ??? android:layout_width="wrap_content" android:layout_height="wrap_content"
??? ??? android:layout_marginTop="200px"></Button>
??? ???
??? ??? <Button android:id="@+id/Button2"
??? ??? android:text="@string/ZoomIn"
??? ??? android:layout_width="wrap_content" android:layout_height="wrap_content"
??? ??? android:layout_toRightOf="@id/Button1"
??? ??? android:layout_marginLeft="30px"
??? ??? android:layout_alignTop="@id/Button1"></Button>
??? ???
??? ??? <Button android:id="@+id/Button3"
??? ??? android:text="@string/RotateLeft"
??? ??? android:layout_width="wrap_content" android:layout_height="wrap_content"
??? ??? android:layout_toRightOf="@id/Button2"
??? ??? android:layout_marginLeft="30px"
??? ??? android:layout_alignTop="@id/Button1"></Button>
??? ???
??? ??? <Button android:id="@+id/Button4"
??? ??? android:text="@string/RotateRight"
??? ??? android:layout_width="wrap_content" android:layout_height="wrap_content"
??? ??? android:layout_toRightOf="@id/Button3"
??? ??? android:layout_marginLeft="30px"
??? ??? android:layout_alignTop="@id/Button1"></Button>
???
??? </RelativeLayout>
</LinearLayout>

?

ImageViewActivity:

package com.henii.android;

import java.io.File;

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.Toast;

//ImageView、Bitmap、BitmapFactory以及Matrix的使用
public class ImageViewActivity extends Activity {
??? /** Called when the activity is first created. */
??? private ImageView iv;

?? //图片自己在相应目录下加上就行了
??? private String fileName = "/data/data/com.henii.android/??.png";//中文乱码了,变成??了!
???
??? private Button zoomOutBtn;//缩小按钮
??? private Button zoomInBtn;//放大按钮
???
??? private Button rotateLeftBtn;//左转按钮
??? private Button rotateRightBtn;//右转按钮
???
??? private Bitmap bm;//图片资源Bitmap
???
??? private float scaleW = 1;//横向缩放系数,1表示不变
??? private float scaleH = 1;//纵向缩放系数,1表示不变
???
??? private float curDegrees = 0;//当前旋转度数
???
??? @Override
??? public void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.main);
???????
??????? iv = (ImageView)findViewById(R.id.ImageView1);
??????? zoomOutBtn = (Button)findViewById(R.id.Button1);
??????? zoomInBtn = (Button)findViewById(R.id.Button2);
???????
??????? rotateLeftBtn = (Button)findViewById(R.id.Button3);
??????? rotateRightBtn = (Button)findViewById(R.id.Button4);
???????
??????? File f = new File(fileName);
??????? if(f.exists()){
??????? ??? //此外,你还可以使用BitmapFactory的decodeResource方法获得一个Bitmap对象
??????? ??? //使用decodeResource方法时传入的是一个drawable的资源id
??????? ??? //还有一个decodeStream方法,这个方法传入一个图片文件的输入流即可!
??????? ??? bm = BitmapFactory.decodeFile(fileName);
??????? ??? //设置ImageView的显示图片
??????? ??? iv.setImageBitmap(bm);
??????? }else{
??????? ??? Toast.makeText(this, "文件不存在!", Toast.LENGTH_SHORT).show();
??????? }
???????
??????? //缩小按钮事件处理
??????? zoomOutBtn.setOnClickListener(new OnClickListener() {
??? ??? ??? @Override
??? ??? ??? public void onClick(View v) {
??? ??? ??? ??? small();
??? ??? ??? }
??? ??? });
???????
??????? //放大按钮事件处理
??????? zoomInBtn.setOnClickListener(new OnClickListener() {
??? ??? ??? @Override
??? ??? ??? public void onClick(View v) {
??? ??? ??? ??? big();
??? ??? ??? }
??? ??? });
???????
??????? //左转按钮事件处理
??????? rotateLeftBtn.setOnClickListener(new OnClickListener() {
??? ??? ??? @Override
??? ??? ??? public void onClick(View v) {
??? ??? ??? ??? left();
??? ??? ??? }
??? ??? });
???????
??????? //右转按钮事件处理
??????? rotateRightBtn.setOnClickListener(new OnClickListener() {
??? ??? ??? @Override
??? ??? ??? public void onClick(View v) {
??? ??? ??? ??? right();
??? ??? ??? }
??? ??? });
??? }
???
? //缩小事件处理
??? private void small(){
??? ??? //根据getWidth和getHeight方法获取Bitmap资源的宽和高
??? ??? int bmpW = bm.getWidth();
??? ??? int bmpH = bm.getHeight();
??? ??? //设置图片缩小比例
??? ??? double scale = 0.8;
??? ??? //计算出这次要缩小的比例
??? ??? scaleW = (float)(scaleW*scale);
??? ??? scaleH = (float)(scaleH*scale);
??? ???
??? ??? //产生reSize后的Bitmap对象
??? ??? //注意这个Matirx是android.graphics底下的那个
??? ??? Matrix mt = new Matrix();
??? ??? //设置缩放系数,分别为原来的0.8和0.8
??? ??? //如果设置为1,1,则是原来的尺寸
??? ??? mt.postScale(scaleW, scaleH);
??? ???
??? ??? //根据缩放系数把原来的Bitmap资源bm进行缩放
??? ??? //参数一为原来的Bitmap资源bm
??? ??? //参数二和三为Bitmap所处的左上角坐标位置
??? ??? //参数四和五为原来Bitmap资源bm的宽和高
??? ??? //参数六为缩放系数参数
??? ??? //参数七为是否过滤
??? ??? //得到缩放后的Bitmap位图资源
??? ??? Bitmap resizeBmp = Bitmap.createBitmap(bm, 0, 0, bmpW, bmpH, mt, true);
??? ???
??? ??? //重新设置ImageView显示的Bitmap位图资源图片
??? ??? iv.setImageBitmap(resizeBmp);
??? }
???
??? //放大事件处理
??? private void big(){
??? ??? int bmpW = bm.getWidth();
??? ??? int bmpH = bm.getHeight();
??? ??? //设置图片放大比例
??? ??? double scale = 1.25;
??? ??? //计算出这次要放大的比例
??? ??? scaleW = (float)(scaleW*scale);
??? ??? scaleH = (float)(scaleH*scale);
??? ???
??? ??? //产生reSize后的Bitmap对象
??? ??? //注意这个Matirx是android.graphics底下的那个
??? ??? Matrix mt = new Matrix();
??? ??? mt.postScale(scaleW, scaleH);
??? ??? Bitmap resizeBmp = Bitmap.createBitmap(bm, 0, 0, bmpW, bmpH, mt, true);
??? ???
??? ??? iv.setImageBitmap(resizeBmp);
??? }
???
??? //左转事件处理
??? private void left(){
??? ??? int bmpW = bm.getWidth();
??? ??? int bmpH = bm.getHeight();
??? ??? //设置图片放大比例
??? ??? double scale = 1;
??? ??? //计算出这次要放大的比例
??? ??? scaleW = (float)(scaleW*scale);
??? ??? scaleH = (float)(scaleH*scale);
??? ???
??? ??? //产生reSize后的Bitmap对象
??? ??? //注意这个Matirx是android.graphics底下的那个
??? ??? Matrix mt = new Matrix();
??? ??? mt.postScale(scaleW, scaleH);
??? ??? //设置旋转角度
??? ??? //如果是设置为0则表示不旋转
??? ??? //设置的数是负数则向左转
??? ??? //设置的数是正数则向右转
??? ??? mt.setRotate(curDegrees = curDegrees-5);
??? ??? Bitmap resizeBmp = Bitmap.createBitmap(bm, 0, 0, bmpW, bmpH, mt, true);
??? ???
??? ??? iv.setImageBitmap(resizeBmp);
??? }
???
??? //右转事件处理
??? private void right(){
??? ??? int bmpW = bm.getWidth();
??? ??? int bmpH = bm.getHeight();
??? ??? //设置图片放大比例
??? ??? double scale = 1;
??? ??? //计算出这次要放大的比例
??? ??? scaleW = (float)(scaleW*scale);
??? ??? scaleH = (float)(scaleH*scale);
??? ???
??? ??? //产生reSize后的Bitmap对象
??? ??? //注意这个Matirx是android.graphics底下的那个
??? ??? Matrix mt = new Matrix();
??? ??? mt.postScale(scaleW, scaleH);
??? ??? mt.setRotate(curDegrees = curDegrees + 5);
??? ??? Bitmap resizeBmp = Bitmap.createBitmap(bm, 0, 0, bmpW, bmpH, mt, true);
??? ???
??? ??? iv.setImageBitmap(resizeBmp);
??? }
}

发表评论
用户名: 匿名