ZoomControls放大缩小图片

hasFocus ():判断焦点

hide ():隐藏

onTouchEvent (MotionEvent event):现这个方法来处理触摸屏移动事件

setIsZoomInEnabled (boolean isEnabled):是否允许放大

setIsZoomOutEnabled (boolean isEnabled):是否允许缩小

setOnZoomInClickListener (View.OnClickListener listener):注册放大监听器

setOnZoomOutClickListener (View.OnClickListener listener):注册缩小监听器

setZoomSpeed (long speed):设置缩放速度

show ():显示

 

这里面,如果将setIsZoomInEnabled()方法设置为false,那么这个放大的按钮就变成了灰色,不能用了,其实这个控件就是两个按钮而已,只是有外观,没有功能,如果你要放大图片或者缩小图片,还是要在监听事件中实现

开始看代码

main.xml

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03     android:orientation="vertical"
04     android:layout_width="fill_parent"
05     android:layout_height="fill_parent"
06     android:id="@+id/layout1"
07     >
08 <ImageView
09     android:id="@+id/imgview"
10     android:layout_width="wrap_content"
11     android:layout_height="wrap_content"
12     android:src="@drawable/yuanyuan"
13     />
14      
15 <ZoomControls
16     android:id="@+id/zoomcontrol"
17     android:layout_gravity="bottom"
18     android:layout_width="wrap_content"
19     android:layout_height="wrap_content"
20 />
21 </LinearLayout>

ZoomExampleActivity.java
01 package com.loulijun.zoomcontroltest;
02  
03 import android.app.Activity;
04 import android.graphics.Bitmap;
05 import android.graphics.BitmapFactory;
06 import android.graphics.Matrix;
07 import android.os.Bundle;
08 import android.util.DisplayMetrics;
09 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.ImageView;
12 import android.widget.LinearLayout;
13 import android.widget.ZoomControls;
14  
15 public class ZoomExampleActivity extends Activity {
16     private LinearLayout layout1;
17     private ZoomControls zoom;
18     private ImageView img;
19     private int id=0;
20     private int displayWidth;
21     private int displayHeight;
22     private float scaleWidth = 1;
23     private float scaleHeight = 1;
24     private Bitmap bmp;
25     @Override
26     public void onCreate(Bundle savedInstanceState) {
27         super.onCreate(savedInstanceState);
28         setContentView(R.layout.main);
29          
30         layout1 = (LinearLayout)findViewById(R.id.layout1);
31         //取得屏幕分辨率大小
32         DisplayMetrics dm = new DisplayMetrics();
33         getWindowManager().getDefaultDisplay().getMetrics(dm);
34         displayWidth = dm.widthPixels;
35         //屏幕高度减去zoomControls的高度
36         displayHeight = dm.heightPixels;
37         bmp = BitmapFactory.decodeResource(getResources(), R.drawable.yuanyuan);
38         img = (ImageView)findViewById(R.id.imgview);
39         //zoom.hide();隐藏zoomControls
40         //zoom.show();显示zoomCOntrols
41          
42         zoom = (ZoomControls)findViewById(R.id.zoomcontrol);
43         img = (ImageView)findViewById(R.id.imgview);
44         zoom.setIsZoomInEnabled(true);
45         zoom.setIsZoomOutEnabled(true);
46         //图片放大
47         zoom.setOnZoomInClickListener(new OnClickListener()
48         {
49             public void onClick(View v)
50             {
51                 int bmpWidth = bmp.getWidth();
52                 int bmpHeight = bmp.getHeight();
53                 //设置图片放大但比例
54                 double scale = 1.25;
55                 //计算这次要放大的比例
56                 scaleWidth = (float)(scaleWidth*scale);
57                 scaleHeight = (float)(scaleHeight*scale);
58                 //产生新的大小但Bitmap对象
59                 Matrix matrix = new Matrix();
60                 matrix.postScale(scaleWidth, scaleHeight);
61                 Bitmap resizeBmp =
62                 Bitmap.createBitmap(bmp,0,0,bmpWidth,bmpHeight,matrix,true);
63                 img.setImageBitmap(resizeBmp);
64  
65             }
66         });
67         //图片减小
68         zoom.setOnZoomOutClickListener(new OnClickListener()
69         {
70  
71             public void onClick(View v) {
72                 int bmpWidth = bmp.getWidth();
73                 int bmpHeight = bmp.getHeight();
74                 //设置图片放大但比例
75                 double scale = 0.8;
76                 //计算这次要放大的比例
77                 scaleWidth = (float)(scaleWidth*scale);
78                 scaleHeight = (float)(scaleHeight*scale);
79                 //产生新的大小但Bitmap对象
80                 Matrix matrix = new Matrix();
81                 matrix.postScale(scaleWidth, scaleHeight);
82                 Bitmap resizeBmp =

你可能感兴趣的:(ZoomControls放大缩小图片)