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" |
09 | android:id = "@+id/imgview" |
10 | android:layout_width = "wrap_content" |
11 | android:layout_height = "wrap_content" |
12 | android:src = "@drawable/yuanyuan" |
16 | android:id = "@+id/zoomcontrol" |
17 | android:layout_gravity = "bottom" |
18 | android:layout_width = "wrap_content" |
19 | android:layout_height = "wrap_content" |
ZoomExampleActivity.java
01 | package com.loulijun.zoomcontroltest; |
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; |
15 | public class ZoomExampleActivity extends Activity { |
16 | private LinearLayout layout1; |
17 | private ZoomControls zoom; |
18 | private ImageView img; |
20 | private int displayWidth; |
21 | private int displayHeight; |
22 | private float scaleWidth = 1 ; |
23 | private float scaleHeight = 1 ; |
26 | public void onCreate(Bundle savedInstanceState) { |
27 | super .onCreate(savedInstanceState); |
28 | setContentView(R.layout.main); |
30 | layout1 = (LinearLayout)findViewById(R.id.layout1); |
32 | DisplayMetrics dm = new DisplayMetrics(); |
33 | getWindowManager().getDefaultDisplay().getMetrics(dm); |
34 | displayWidth = dm.widthPixels; |
36 | displayHeight = dm.heightPixels; |
37 | bmp = BitmapFactory.decodeResource(getResources(), R.drawable.yuanyuan); |
38 | img = (ImageView)findViewById(R.id.imgview); |
42 | zoom = (ZoomControls)findViewById(R.id.zoomcontrol); |
43 | img = (ImageView)findViewById(R.id.imgview); |
44 | zoom.setIsZoomInEnabled( true ); |
45 | zoom.setIsZoomOutEnabled( true ); |
47 | zoom.setOnZoomInClickListener( new OnClickListener() |
49 | public void onClick(View v) |
51 | int bmpWidth = bmp.getWidth(); |
52 | int bmpHeight = bmp.getHeight(); |
56 | scaleWidth = ( float )(scaleWidth*scale); |
57 | scaleHeight = ( float )(scaleHeight*scale); |
59 | Matrix matrix = new Matrix(); |
60 | matrix.postScale(scaleWidth, scaleHeight); |
62 | Bitmap.createBitmap(bmp, 0 , 0 ,bmpWidth,bmpHeight,matrix, true ); |
63 | img.setImageBitmap(resizeBmp); |
68 | zoom.setOnZoomOutClickListener( new OnClickListener() |
71 | public void onClick(View v) { |
72 | int bmpWidth = bmp.getWidth(); |
73 | int bmpHeight = bmp.getHeight(); |
77 | scaleWidth = ( float )(scaleWidth*scale); |
78 | scaleHeight = ( float )(scaleHeight*scale); |
80 | Matrix matrix = new Matrix(); |
81 | matrix.postScale(scaleWidth, scaleHeight); |
83 | Bitmap.createBitmap(bmp, 0 , 0 ,bmpWidth,bmpHeight,matrix, true ); |
84 | img.setImageBitmap(resizeBmp); |
效果如下:
转自:
http://www.iteye.com/topic/1112742