我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

android新浪微博图片缩放效果怎么实现

这篇文章主要讲解了“android新浪微博图片缩放效果怎么实现”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“android新浪微博图片缩放效果怎么实现”吧!

我们提供的服务有:成都网站制作、做网站、外贸营销网站建设、微信公众号开发、网站优化、网站认证、西固ssl等。为上1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的西固网站制作公司

Android开发中有时会用到图片缩放效果,即点击图片时显示缩放按钮,过一会消失。本文就根据新浪微博的图片缩放给大家写一个实例,以供参考。下面直接上代码。

package com.Johnson.image.zoom;     import android.app.Activity;     import android.app.Dialog;     import android.app.ProgressDialog;     import android.content.DialogInterface;     import android.content.DialogInterface.OnKeyListener;     import android.graphics.Bitmap;     import android.graphics.BitmapFactory;     import android.graphics.Matrix;     import android.os.Bundle;     import android.os.Handler;     import android.util.DisplayMetrics;     import android.util.Log;     import android.view.KeyEvent;     import android.view.MotionEvent;     import android.view.View;     import android.view.View.OnClickListener;     import android.widget.ImageView;     import android.widget.LinearLayout;     import android.widget.RelativeLayout;     import android.widget.ZoomControls;     public class MainActivity extends Activity {             /** Called when the activity is first created. */       private final int LOADING_IMAGE = 1;       public static String KEY_IMAGEURI = "ImageUri";       private ZoomControls zoom;       private ImageView mImageView;       private LinearLayout layoutImage;       private int displayWidth;       private int displayHeight;       /**图片资源*/       private Bitmap bmp;       /**宽的缩放比例*/       private float scaleWidth = 1;       /**高的缩放比例*/       private float scaleHeight = 1;       /**用来计数放大+1    缩小-1*/       private int    zoomNumber=0;       /**点击屏幕显示缩放按钮,三秒消失*/       private int showTime=3000;       RelativeLayout rl;       Handler mHandler = new Handler();       private Runnable task = new Runnable() {         public void run() {           zoom.setVisibility(View.INVISIBLE);                 }       };             @Override             public void onCreate(Bundle savedInstanceState) {                     super.onCreate(savedInstanceState);                     setContentView(R.layout.main);         //showDialog(LOADING_IMAGE);         //图片是从网络上获取的话,需要加入滚动条             bmp=BitmapFactory.decodeResource(getResources(), R.drawable.image);         //removeDialog(LOADING_IMAGE);              initZoom();     }       @Override       protected Dialog onCreateDialog(int id) {         switch (id) {         case LOADING_IMAGE: {           final ProgressDialog dialog = new ProgressDialog(this);           dialog.setOnKeyListener(new OnKeyListener() {             @Override             public boolean onKey(DialogInterface dialog, int keyCode,                 KeyEvent event) {               if (keyCode == KeyEvent.KEYCODE_BACK) {                 finish();               }               return false;             }           });           dialog.setMessage("正在加载图片请稍后...");           dialog.setIndeterminate(true);           dialog.setCancelable(true);           return dialog;         }         }         return null;       }       public void initZoom() {         /* 取得屏幕分辨率大小 */         DisplayMetrics dm = new DisplayMetrics();         getWindowManager().getDefaultDisplay().getMetrics(dm);         displayWidth = dm.widthPixels;         displayHeight = dm.heightPixels;         mImageView = (ImageView) findViewById(R.id.myImageView);         mImageView.setImageBitmap(bmp);         layoutImage = (LinearLayout) findViewById(R.id.layoutImage);         mImageView.setOnClickListener(new OnClickListener() {           @Override           public void onClick(View v) {             // TODO Auto-generated method stub                                     /**                                     * 在图片上和整个view上同时添加点击监听捕捉屏幕                                     * 点击事件,来显示放大缩小按钮                                        * */                    zoom.setVisibility(View.VISIBLE);             mHandler.removeCallbacks(task);             mHandler.postDelayed(task, showTime);           }         });         layoutImage.setOnClickListener(new OnClickListener() {           @Override           public void onClick(View v) {             // TODO Auto-generated method stub                         zoom.setVisibility(View.VISIBLE);             mHandler.removeCallbacks(task);             mHandler.postDelayed(task, showTime);           }         });         zoom = (ZoomControls) findViewById(R.id.zoomcontrol);         zoom.setIsZoomInEnabled(true);         zoom.setIsZoomOutEnabled(true);         // 图片放大         zoom.setOnZoomInClickListener(new OnClickListener() {           public void onClick(View v) {             big();           }         });         // 图片减小         zoom.setOnZoomOutClickListener(new OnClickListener() {           public void onClick(View v) {             small();           }         });         zoom.setVisibility(View.VISIBLE);         mHandler.postDelayed(task, showTime);       }       @Override       public boolean onTouchEvent(MotionEvent event) {         // TODO Auto-generated method stub              /**                     * 在图片上和整个view上同时添加点击监听捕捉屏幕                     * 点击事件,来显示放大缩小按钮                        * */             zoom.setVisibility(View.VISIBLE);         mHandler.removeCallbacks(task);         mHandler.postDelayed(task, showTime);         return false;       }       @Override       public boolean onKeyDown(int keyCode, KeyEvent event) {         // TODO Auto-generated method stub         super.onKeyDown(keyCode, event);         return true;       }       /* 图片缩小的method */       private void small() {         --zoomNumber;         int bmpWidth = bmp.getWidth();         int bmpHeight = bmp.getHeight();         Log.i("","bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);         /* 设置图片缩小的比例 */         double scale = 0.8;         /* 计算出这次要缩小的比例 */         scaleWidth = (float) (scaleWidth * scale);         scaleHeight = (float) (scaleHeight * scale);         /* 产生reSize后的Bitmap对象 */         Matrix matrix = new Matrix();         matrix.postScale(scaleWidth, scaleHeight);         Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,             matrix, true);         mImageView.setImageBitmap(resizeBmp);         /* 限制缩小尺寸 */         if ((scaleWidth * scale * bmpWidth < bmpWidth / 4             || scaleHeight * scale * bmpHeight > bmpWidth /4             || scaleWidth * scale * bmpWidth > displayWidth / 5             || scaleHeight * scale * bmpHeight > displayHeight / 5)&&(zoomNumber==-1) ){         zoom.setIsZoomOutEnabled(false);         } else {         zoom.setIsZoomOutEnabled(true);         }         zoom.setIsZoomInEnabled(true);         System.gc();       }       /* 图片放大的method */       private void big() {         ++zoomNumber;         int bmpWidth = bmp.getWidth();         int bmpHeight = bmp.getHeight();         /* 设置图片放大的比例 */         double scale = 1.25;         /* 计算这次要放大的比例 */         scaleWidth = (float) (scaleWidth * scale);         scaleHeight = (float) (scaleHeight * scale);         /* 产生reSize后的Bitmap对象 */         Matrix matrix = new Matrix();         matrix.postScale(scaleWidth, scaleHeight);         Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,             matrix, true);         mImageView.setImageBitmap(resizeBmp);         /* 限制放大尺寸 */         if (scaleWidth * scale * bmpWidth > bmpWidth * 4             || scaleHeight * scale * bmpHeight > bmpWidth * 4             || scaleWidth * scale * bmpWidth > displayWidth * 5             || scaleHeight * scale * bmpHeight > displayHeight * 5) {           zoom.setIsZoomInEnabled(false);         } else {           zoom.setIsZoomInEnabled(true);         }         zoom.setIsZoomOutEnabled(true);       System.gc();       }     }

布局文件如下:

  1.              

  2.     

  3.             android:orientation="vertical"         

  4.             android:layout_width="fill_parent"         

  5.             android:layout_height="fill_parent"         

  6.             android:id="@+id/layout1"         

  7.                 >         

  8.          

  9.                     android:layout_width="fill_parent"         

  10.                     android:layout_height="fill_parent"         

  11.                  android:id="@+id/rl"        

  12.                     >         

  13.             

  14.                     android:layout_width="fill_parent"         

  15.                     android:layout_height="fill_parent"            

  16.                     android:layout_weight="19"         

  17.                     android:scrollbars="none"         

  18.                     android:fadingEdge="vertical"     

  19.                     android:layout_gravity="center"        

  20.                     android:gravity="center"         

  21.                  >         

  22.             

  23.                     android:layout_height="fill_parent"         

  24.                     android:layout_width="fill_parent"     

  25.                     android:scrollbars="none"     

  26.                      android:layout_gravity="center"        

  27.                     android:gravity="center"        

  28.                     android:id="@+id/hs"     

  29.                         >         

  30.                     

  31. android:orientation="horizontal"         

  32. android:layout_width="fill_parent"         

  33. android:layout_height="fill_parent"         

  34.                             android:id="@+id/layoutImage"        

  35.                             android:layout_gravity="center"        

  36.                             android:gravity="center"         

  37.                             >         

  38.                             

  39. android:layout_gravity="center"        

  40.                                 android:gravity="center"         

  41. android:id="@+id/myImageView"         

  42. android:layout_width="fill_parent"         

  43. android:layout_height="fill_parent"         

  44. android:layout_weight="19"         

  45. android:paddingTop="5dip"         

  46. android:paddingBottom="5dip"        

  47.                                     />         

  48.                              

  49.                      

  50.                     

  51.                  

  52.           android:layout_width="wrap_content" android:layout_height="wrap_content"     

  53.           android:layout_centerHorizontal="true"     

  54.                 android:layout_alignParentBottom="true"     

  55.           >     

  56.              

  57.                      

  58.           

感谢各位的阅读,以上就是“android新浪微博图片缩放效果怎么实现”的内容了,经过本文的学习后,相信大家对android新浪微博图片缩放效果怎么实现这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


网站标题:android新浪微博图片缩放效果怎么实现
浏览地址:http://shouzuofang.com/article/geecig.html

其他资讯