在Android里面图片的缩放,有很多方法,这里介绍一种比较简单的。它主要采用BitMap对象的切换,通过BitMap的比例缩放改变来实现图片的放大放小。下面是效果图:
程序代码:
(1)布局:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout android:id="@+id/relativeLayout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center_horizontal"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <ImageView android:id="@+id/imageView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- >
- </ImageView>
- <LinearLayout android:id="@+id/linearLayout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true">
- <Button android:id="@+id/bt_smaller"
- android:layout_width="100px"
- android:layout_height="50px"
- android:text="缩小" >
- </Button>
- <Button android:id="@+id/bt_bigger"
- android:layout_width="100px"
- android:layout_height="50px"
- android:text="放大" >
- </Button>
- </LinearLayout>
- </RelativeLayout>
(2)代码:
- package com.myandroid.test;
- 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.RelativeLayout;
- public class AlterImageSize extends Activity {
- private ImageView imageView;
- private Button bt_bigger;
- private Button bt_smaller;
- private RelativeLayout layout; //容器
- private Bitmap bmp; //bitmap图片对象
- private int primaryWidth; //原图片宽
- private int primaryHeight; //原图片高
- private double scaleWidth, scaleHeight; //高宽比例
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //初始化
- getWidget();
- }
- /**
- * 获取控件对象并初始化
- */
- private void getWidget() {
- imageView = (ImageView)findViewById(R.id.imageView);
- layout = (RelativeLayout)findViewById(R.id.relativeLayout);
- bt_bigger = (Button)findViewById(R.id.bt_bigger);
- bt_smaller = (Button)findViewById(R.id.bt_smaller);
- //创建BitMap对象,用于显示图片
- bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.butterfly);
- //原始大小
- primaryWidth = bmp.getWidth();
- primaryHeight = bmp.getHeight();
- //初始比例为1
- scaleWidth = scaleHeight = 1;
- imageView.setImageBitmap(bmp);
- bt_bigger.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- scale(1.25, 1.25);
- }
- });
- bt_smaller.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- scale(0.8, 0.8);
- }
- });
- }
- /**
- * 缩放
- */
- private void scale(double scale_width, double scale_height) {
- //这种方法,有点不好是:如果图片大小超出屏幕会报错。
- if((scale_width > 1 && scaleWidth * primaryWidth >= getWindowManager().getDefaultDisplay().getWidth())
- || (scale_width > 1 && scale_height * primaryHeight >= getWindowManager().getDefaultDisplay().getHeight())){
- bt_bigger.setEnabled(false);
- }else {
- bt_bigger.setEnabled(true);
- }
- scaleWidth = scaleWidth * scale_width; //缩放到原来的*倍
- scaleHeight = scaleHeight * scale_height;
- Matrix matrix = new Matrix(); //矩阵,用于图片比例缩放
- matrix.postScale((float)scaleWidth, (float)scaleHeight); //设置高宽比例(三维矩阵)
- //缩放后的BitMap
- Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, primaryWidth, primaryHeight, matrix, true);
- //重新设置BitMap
- imageView.setImageBitmap(newBmp);
- }
- }
原文地址:http://blog.csdn.net/kkfdsa132/article/details/6368409