android ImagView缩放方法之一(Bitmap)

  1. package com.myandroid.test;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapFactory;  
  6. import android.graphics.Matrix;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.ImageView;  
  12. import android.widget.RelativeLayout;  
  13.   
  14. public class AlterImageSize extends Activity {  
  15.     private ImageView imageView;  
  16.     private Button bt_bigger;  
  17.     private Button bt_smaller;  
  18.     private RelativeLayout layout;          //容器  
  19.     private Bitmap bmp;                     //bitmap图片对象  
  20.     private int primaryWidth;               //原图片宽  
  21.     private int primaryHeight;              //原图片高  
  22.     private double scaleWidth, scaleHeight; //高宽比例  
  23.       
  24.       
  25.     /** Called when the activity is first created. */  
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.main);  
  30.           
  31.         //初始化  
  32.         getWidget();  
  33.     }  
  34.      
  35.     /** 
  36.      * 获取控件对象并初始化 
  37.      */  
  38.     private void getWidget() {  
  39.           
  40.         imageView = (ImageView)findViewById(R.id.imageView);  
  41.         layout = (RelativeLayout)findViewById(R.id.relativeLayout);  
  42.         bt_bigger = (Button)findViewById(R.id.bt_bigger);  
  43.         bt_smaller = (Button)findViewById(R.id.bt_smaller);  
  44.           
  45.         //创建BitMap对象,用于显示图片  
  46.         bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.butterfly);  
  47.         //原始大小  
  48.         primaryWidth = bmp.getWidth();  
  49.         primaryHeight = bmp.getHeight();  
  50.         //初始比例为1  
  51.         scaleWidth = scaleHeight = 1;  
  52.           
  53.         imageView.setImageBitmap(bmp);  
  54.           
  55.         bt_bigger.setOnClickListener(new OnClickListener() {  
  56.               
  57.             @Override  
  58.             public void onClick(View v) {  
  59.                 // TODO Auto-generated method stub  
  60.                 scale(1.251.25);  
  61.             }  
  62.         });  
  63.           
  64.         bt_smaller.setOnClickListener(new OnClickListener() {  
  65.               
  66.             @Override  
  67.             public void onClick(View v) {  
  68.                 // TODO Auto-generated method stub  
  69.                 scale(0.80.8);  
  70.             }  
  71.         });  
  72.           
  73.     }  
  74.   
  75.     /** 
  76.      * 缩放 
  77.      */  
  78.     private void scale(double  scale_width, double scale_height) {  
  79.           
  80.         //这种方法,有点不好是:如果图片大小超出屏幕会报错。  
  81.         if((scale_width > 1 && scaleWidth * primaryWidth >= getWindowManager().getDefaultDisplay().getWidth())  
  82.                 || (scale_width > 1 && scale_height * primaryHeight >= getWindowManager().getDefaultDisplay().getHeight())){  
  83.             bt_bigger.setEnabled(false);  
  84.         }else {  
  85.             bt_bigger.setEnabled(true);  
  86.         }  
  87.               
  88.           
  89.           
  90.         scaleWidth = scaleWidth * scale_width;  //缩放到原来的*倍  
  91.         scaleHeight = scaleHeight * scale_height;  
  92.           
  93.         Matrix matrix = new Matrix();   //矩阵,用于图片比例缩放  
  94.         matrix.postScale((float)scaleWidth, (float)scaleHeight);    //设置高宽比例(三维矩阵)  
  95.           
  96.         //缩放后的BitMap  
  97.         Bitmap newBmp = Bitmap.createBitmap(bmp, 00, primaryWidth, primaryHeight, matrix, true);   
  98.           
  99.         //重新设置BitMap  
  100.         imageView.setImageBitmap(newBmp);  
  101.       
  102.     }  
  103. }  

xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal" >

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center" >

        <HorizontalScrollView
            android:id="@+id/hs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" >

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center" />
        </HorizontalScrollView>
    </ScrollView>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:gravity="center" >

        <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>


你可能感兴趣的:(android,layout,float,button,BT,encoding)