Andorid ZoomControls的用法

 1 package com.turboradio.googlesdk;
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.util.DisplayMetrics;
9 import android.view.View;
10 import android.widget.ImageView;
11 import android.widget.LinearLayout;
12 import android.widget.ZoomControls;
13
14 public class ZoomControlsImageView extends Activity {
15 private ImageView imageView;
16 private ZoomControls zoomControls;
17 private LinearLayout layout1;
18 private int id = 0;
19 private int displayWidth;
20 private int displayHeight;
21 private int imageWidth;
22 private int imageHeight;
23 private float scaleWidth = 1;
24 private float scaleHeight = 1;
25 private Bitmap bmp;
26 @Override
27 protected void onCreate(Bundle savedInstanceState) {
28 super.onCreate(savedInstanceState);
29 setContentView(R.layout.zoomcontrols_imageview);
30 layout1 = (LinearLayout)findViewById(R.id.layout1);
31 /**取得屏幕的分辨率**/
32 DisplayMetrics dm = new DisplayMetrics();
33 getWindowManager().getDefaultDisplay().getMetrics(dm);
34 displayWidth = dm.widthPixels;
35 displayHeight = dm.heightPixels;
36 bmp = BitmapFactory.decodeResource(getResources(), R.drawable.han);
37 imageWidth = bmp.getWidth();
38 imageHeight = bmp.getHeight();
39 imageView = (ImageView)findViewById(R.id.imageView);
40 zoomControls =(ZoomControls) findViewById(R.id.zoomControls1);
41 zoomControls.setIsZoomInEnabled(true);
42 zoomControls.setIsZoomOutEnabled(true);
43 /**图片放大**/
44 zoomControls.setOnZoomInClickListener(new ZoomControls.OnClickListener(){
45
46 public void onClick(View v) {
47 zoomControls.setIsZoomOutEnabled(true);
48 int bmpWidth = bmp.getWidth();
49 int bmpHeight = bmp.getHeight();
50 // 设置图片的放大比例
51 double scale = 1.25;
52 /**计算这次要放大的比例**/
53 scaleWidth = (float)(scaleWidth*scale);
54 scaleHeight = (float)(scaleHeight*scale);
55 Matrix matrix = new Matrix();
56 matrix.postScale(scaleWidth, scaleHeight);
57 Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,matrix,false);
58 if(resizeBmp.getHeight() >= displayHeight ){
59 zoomControls.setIsZoomInEnabled(false);
60 }
61 imageView.setImageBitmap(resizeBmp);
62 }});
63 /**图片的缩小**/
64 zoomControls.setOnZoomOutClickListener(new ZoomControls.OnClickListener(){
65
66 public void onClick(View v) {
67 zoomControls.setIsZoomInEnabled(true);
68 int bmpWidth = bmp.getWidth();
69 int bmpHeight = bmp.getHeight();
70 double scale = 0.8;
71 scaleWidth = (float)(scaleWidth*scale);
72 scaleHeight = (float)(scaleHeight*scale);
73 Matrix matrix = new Matrix();
74 matrix.postScale(scaleWidth, scaleHeight);
75 Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,matrix,true);
76 if(resizeBmp.getHeight() <= imageHeight){
77 zoomControls.setIsZoomOutEnabled(false);
78 }
79 imageView.setImageBitmap(resizeBmp);
80 }});
81 }
82 }
 1 <?xml version="1.0" encoding="utf-8"?>
2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 >
6 <ImageView
7 android:id="@+id/imageView"
8 android:layout_width="wrap_content"
9 android:layout_height="wrap_content"
10 android:src="@drawable/han"
11 android:layout_gravity="top"
12 />
13 <ZoomControls
14 android:id="@+id/zoomControls1"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:layout_gravity="bottom|right"
18 />
19 </FrameLayout>



posted @ 2011-12-07 11:13  疯子FK  阅读(2353)  评论(1编辑  收藏  举报