ZoomControls

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ZoomControls;

public class ZoomControlsImageView extends Activity {
     private ImageView imageView;
     private ZoomControls zoomControls;
     private LinearLayout layout1;
     private int id = 0;
     private  int displayWidth;
     private int displayHeight;
     private int imageWidth;
     private int imageHeight;
     private float scaleWidth = 1;
     private float scaleHeight = 1;
     private Bitmap bmp;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.zoomcontrols_imageview);
         layout1 = (LinearLayout)findViewById(R.id.layout1);
        /**取得屏幕的分辨率**/
         DisplayMetrics dm = new DisplayMetrics();
         getWindowManager().getDefaultDisplay().getMetrics(dm);
         displayWidth = dm.widthPixels;
         displayHeight = dm.heightPixels;
         bmp = BitmapFactory.decodeResource(getResources(), R.drawable.han);
         imageWidth = bmp.getWidth();
         imageHeight = bmp.getHeight();
         imageView = (ImageView)findViewById(R.id.imageView);
         zoomControls =(ZoomControls) findViewById(R.id.zoomControls1);
         zoomControls.setIsZoomInEnabled(true);
         zoomControls.setIsZoomOutEnabled(true);
         /**图片放大**/
         zoomControls.setOnZoomInClickListener(new ZoomControls.OnClickListener(){
              public void onClick(View v) {
                 zoomControls.setIsZoomOutEnabled(true);
                 int bmpWidth = bmp.getWidth();
                 int bmpHeight = bmp.getHeight();
                 // 设置图片的放大比例
                 double scale = 1.25;
                 /**计算这次要放大的比例**/
                     scaleWidth = (float)(scaleWidth*scale);
                     scaleHeight = (float)(scaleHeight*scale);
                     Matrix matrix = new Matrix();
                     matrix.postScale(scaleWidth, scaleHeight);
                     Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,matrix,false);
                     if(resizeBmp.getHeight() >= displayHeight ){
                         zoomControls.setIsZoomInEnabled(false);
                     }
                     imageView.setImageBitmap(resizeBmp);
             }});
         /**图片的缩小**/
         zoomControls.setOnZoomOutClickListener(new ZoomControls.OnClickListener(){

             public void onClick(View v) {
                 zoomControls.setIsZoomInEnabled(true);
                 int bmpWidth = bmp.getWidth();
                 int bmpHeight = bmp.getHeight();
                 double scale = 0.8;1                 scaleWidth = (float)(scaleWidth*scale);
                 scaleHeight = (float)(scaleHeight*scale);
                 Matrix matrix = new Matrix();
                 matrix.postScale(scaleWidth, scaleHeight);
                 Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,matrix,true);
                 if(resizeBmp.getHeight() <= imageHeight){
                     zoomControls.setIsZoomOutEnabled(false);
                 }
                 imageView.setImageBitmap(resizeBmp);
             }});
     }
 }

你可能感兴趣的:(Zoomcontrols)