职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 970|回复: 5

玩转Android---UI篇---ZoomControls放大缩小图片

[复制链接]
shmilyyu 发表于 2011-8-7 10:43 | 显示全部楼层 |阅读模式
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
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:id="@+id/layout1"  
  7.     >   
  8. <ImageView   
  9.     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>  

  22. <?xml version="1.0" encoding="utf-8"?>
  23. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  24.     android:orientation="vertical"
  25.     android:layout_width="fill_parent"
  26.     android:layout_height="fill_parent"
  27.     android:id="@+id/layout1"
  28.     >
  29. <ImageView
  30.         android:id="@+id/imgview"
  31.     android:layout_width="wrap_content"
  32.     android:layout_height="wrap_content"
  33.     android:src="@drawable/yuanyuan"
  34.     />
  35.    
  36. <ZoomControls
  37.         android:id="@+id/zoomcontrol"
  38.         android:layout_gravity="bottom"
  39.         android:layout_width="wrap_content"
  40.         android:layout_height="wrap_content"
  41. />
  42. </LinearLayout>
复制代码
ZoomExampleActivity.java
  1. package com.loulijun.zoomcontroltest;   
  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.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 = Bitmap.createBitmap(bmp,0,0,bmpWidth,bmpHeight,matrix,true);   
  62.                 img.setImageBitmap(resizeBmp);   
  63.   
  64.             }   
  65.         });   
  66.         //图片减小   
  67.         zoom.setOnZoomOutClickListener(new OnClickListener()   
  68.         {   
  69.   
  70.             public void onClick(View v) {   
  71.                 int bmpWidth = bmp.getWidth();   
  72.                 int bmpHeight = bmp.getHeight();   
  73.                 //设置图片放大但比例   
  74.                 double scale = 0.8;   
  75.                 //计算这次要放大的比例   
  76.                 scaleWidth = (float)(scaleWidth*scale);   
  77.                 scaleHeight = (float)(scaleHeight*scale);   
  78.                 //产生新的大小但Bitmap对象   
  79.                 Matrix matrix = new Matrix();   
  80.                 matrix.postScale(scaleWidth, scaleHeight);   
  81.                 Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,bmpHeight,matrix,true);   
  82.                 img.setImageBitmap(resizeBmp);   
  83.             }   
  84.                
  85.         });   
  86.     }   
  87. }  

  88. package com.loulijun.zoomcontroltest;

  89. import android.app.Activity;
  90. import android.graphics.Bitmap;
  91. import android.graphics.BitmapFactory;
  92. import android.graphics.Matrix;
  93. import android.os.Bundle;
  94. import android.util.DisplayMetrics;
  95. import android.view.View;
  96. import android.view.View.OnClickListener;
  97. import android.widget.ImageView;
  98. import android.widget.LinearLayout;
  99. import android.widget.ZoomControls;

  100. public class ZoomExampleActivity extends Activity {
  101.         private LinearLayout layout1;
  102.         private ZoomControls zoom;
  103.         private ImageView img;
  104.         private int id=0;
  105.         private int displayWidth;
  106.         private int displayHeight;
  107.         private float scaleWidth = 1;
  108.         private float scaleHeight = 1;
  109.         private Bitmap bmp;
  110.     @Override
  111.     public void onCreate(Bundle savedInstanceState) {
  112.         super.onCreate(savedInstanceState);
  113.         setContentView(R.layout.main);
  114.         
  115.         layout1 = (LinearLayout)findViewById(R.id.layout1);
  116.         //取得屏幕分辨率大小
  117.         DisplayMetrics dm = new DisplayMetrics();
  118.         getWindowManager().getDefaultDisplay().getMetrics(dm);
  119.         displayWidth = dm.widthPixels;
  120.         //屏幕高度减去zoomControls的高度
  121.         displayHeight = dm.heightPixels;
  122.         bmp = BitmapFactory.decodeResource(getResources(), R.drawable.yuanyuan);
  123.         img = (ImageView)findViewById(R.id.imgview);
  124.         //zoom.hide();隐藏zoomControls
  125.         //zoom.show();显示zoomCOntrols
  126.         
  127.         zoom = (ZoomControls)findViewById(R.id.zoomcontrol);
  128.         img = (ImageView)findViewById(R.id.imgview);
  129.         zoom.setIsZoomInEnabled(true);
  130.         zoom.setIsZoomOutEnabled(true);
  131.         //图片放大
  132.         zoom.setOnZoomInClickListener(new OnClickListener()
  133.         {
  134.                 public void onClick(View v)
  135.                 {
  136.                         int bmpWidth = bmp.getWidth();
  137.                                 int bmpHeight = bmp.getHeight();
  138.                                 //设置图片放大但比例
  139.                                 double scale = 1.25;
  140.                                 //计算这次要放大的比例
  141.                                 scaleWidth = (float)(scaleWidth*scale);
  142.                                 scaleHeight = (float)(scaleHeight*scale);
  143.                                 //产生新的大小但Bitmap对象
  144.                                 Matrix matrix = new Matrix();
  145.                                 matrix.postScale(scaleWidth, scaleHeight);
  146.                                 Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,bmpHeight,matrix,true);
  147.                                 img.setImageBitmap(resizeBmp);

  148.                 }
  149.         });
  150.         //图片减小
  151.         zoom.setOnZoomOutClickListener(new OnClickListener()
  152.         {

  153.                         public void onClick(View v) {
  154.                                 int bmpWidth = bmp.getWidth();
  155.                                 int bmpHeight = bmp.getHeight();
  156.                                 //设置图片放大但比例
  157.                                 double scale = 0.8;
  158.                                 //计算这次要放大的比例
  159.                                 scaleWidth = (float)(scaleWidth*scale);
  160.                                 scaleHeight = (float)(scaleHeight*scale);
  161.                                 //产生新的大小但Bitmap对象
  162.                                 Matrix matrix = new Matrix();
  163.                                 matrix.postScale(scaleWidth, scaleHeight);
  164.                                 Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,bmpHeight,matrix,true);
  165.                                 img.setImageBitmap(resizeBmp);
  166.                         }
  167.                
  168.         });
  169.     }
  170. }
复制代码
效果如下:

656b439f-7fb6-3878-8118-2891f605c0e7.png

77800003-eb75-303a-b136-6b0a8c5baa65.png

92205dde-b69b-34d8-b831-a7aca4442f8b.png

能文能武 发表于 2011-8-7 10:43 | 显示全部楼层
恩,不错,正准备写一个图片查看界面,用上了

已经来了吗 发表于 2011-8-7 10:44 | 显示全部楼层
借鉴一下你的

有烟没火 发表于 2011-8-7 10:44 | 显示全部楼层
相当不错了
Jethro 发表于 2011-8-13 11:34 | 显示全部楼层
原来...发神经是这样的啊...
 楼主| shmilyyu 发表于 2011-8-13 11:34 | 显示全部楼层
楼主,支持!
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-4-25 12:25 , Processed in 0.143673 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表