您的位置:首页 > 移动开发 > Android开发

Android缩放图片和旋转图片示例 .

2013-01-11 16:40 435 查看
一、完成该功能要具备以下知识点

得到位图Bitmap的方式我总结了四种方式,不管哪种方式得到的图片相关的位图,
都可以用ImageView对象的setImageBitmap(Bitmap bitmap)函数,把与位图相关的图片设置在ImageView中显示出来
(1)从sdcard中图片路径去取,本例就是采取这种方式 ,如下面的代码,
filename代表图片的路径
Bitmap bm = BitmapFactory.decodeFile(fileName);
(2)从项目的/res/drawable中去取
Bitmap bm = ((BitmapDrawable)getResources().getDrawable(R.drawable.icon)).getBitmap();

java.io.InputStream is = getResources().openRawResource(R.drawable.icon);
BitmapFactory.Option opts = new BitmapFactory.Options();
opts.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeStream(is,null,opts);
(3)从布局文件中的ImageView中去取 (以下代码中imageView是一个ImageView对象,该对象中已经设置了图片)
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
(4)从socket中去取
socket读取出的图片内容保存到byte数组中(假设byte数组名是 b,并且b的长度与图片内容长度相等)
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);

二、Android改变位图(图片)大小

如果想改变图片的大小,可以先得到图片的位图对象后,改变位图大小,再设置到ImageView中去,
本编讲的是怎样改变位图的大小示例,该示例把位图放大了1/4
(假设bm是位图Bitmap对象, iv是ImageView对象,int curDegrees = 0)
int bmpW = bm.getWidth();
int bmpH = bm.getHeight();
//设置图片放大比例
double scale = 1.25;
//计算出缩放比例的比例
int scaleW = 1;
int scaleH = 1;
scaleW = (float)(scaleW*scale);
scaleH = (float)(scaleH*scale);
//产生reSize后的Bitmap对象
android.graphics.Matrix mt = new android.graphics.Matrix();
mt.postScale(scaleW, scaleH);
Bitmap resizeBmp = Bitmap.createBitmap(bm, 0, 0, bmpW, bmpH, mt, true);

三、旋转图片

(以下示例是把图片放大四分之一再旋转,假设bm是位图Bitmap对象, iv是ImageView对象,int curDegrees = 0)
int bmpW = bm.getWidth();
int bmpH = bm.getHeight();
//设置图片放大比例
double scale = 1.25;
//计算出这次要放大的比例
int scaleW = 1;
int scaleH = 1;
scaleW = (float)(scaleW*scale);
scaleH = (float)(scaleH*scale);
//产生reSize后的Bitmap对象
android.graphics.Matrix mt = new android.graphics.Matrix();
//设置位图缩放比例
mt.postScale(scaleW, scaleH);
//设置位图旋转程度
mt.setRotate(curDegrees = curDegrees + 5);
//设置例设置好的位图缩放比例与旋转程度改变位图
Bitmap resizeBmp = Bitmap.createBitmap(bm, 0, 0, bmpW, bmpH, mt, true);
//把位图显示到ImageView中去
iv.setImageBitmap(resizeBmp);

四、下面做一个完整的示例,功能是可以缩放图片也可以旋转图片?

1、第一步:
布局文件/res/layout/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="缩小"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="200px"></Button>

<Button android:id="@+id/Button2"
android:text="放大"
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="左旋"
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="右旋"
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>

2、第二步
java代码
package com.fs.com;

import java.io.File;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
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 MainActivity extends Activity {

private ImageView iv;

//图片自己在相应目录下加上就行了
private String fileName = "/sdcard/qq.JPG";//
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(){
ImageView imageView = null;
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐