android新浪微博圖片縮放效果怎么實(shí)現(xiàn)

這篇文章主要講解了“android新浪微博圖片縮放效果怎么實(shí)現(xiàn)”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“android新浪微博圖片縮放效果怎么實(shí)現(xiàn)”吧!

我們提供的服務(wù)有:成都網(wǎng)站制作、做網(wǎng)站、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、西固ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢(xún)和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的西固網(wǎng)站制作公司

Android開(kāi)發(fā)中有時(shí)會(huì)用到圖片縮放效果,即點(diǎn)擊圖片時(shí)顯示縮放按鈕,過(guò)一會(huì)消失。本文就根據(jù)新浪微博的圖片縮放給大家寫(xiě)一個(gè)實(shí)例,以供參考。下面直接上代碼。

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;       /**用來(lái)計(jì)數(shù)放大+1    縮小-1*/       private int    zoomNumber=0;       /**點(diǎn)擊屏幕顯示縮放按鈕,三秒消失*/       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);         //圖片是從網(wǎng)絡(luò)上獲取的話(huà),需要加入滾動(dòng)條             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("正在加載圖片請(qǐng)稍后...");           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                                     /**                                     * 在圖片上和整個(gè)view上同時(shí)添加點(diǎn)擊監(jiān)聽(tīng)捕捉屏幕                                     * 點(diǎn)擊事件,來(lái)顯示放大縮小按鈕                                        * */                    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              /**                     * 在圖片上和整個(gè)view上同時(shí)添加點(diǎn)擊監(jiān)聽(tīng)捕捉屏幕                     * 點(diǎn)擊事件,來(lái)顯示放大縮小按鈕                        * */             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);         /* 設(shè)置圖片縮小的比例 */         double scale = 0.8;         /* 計(jì)算出這次要縮小的比例 */         scaleWidth = (float) (scaleWidth * scale);         scaleHeight = (float) (scaleHeight * scale);         /* 產(chǎn)生reSize后的Bitmap對(duì)象 */         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();         /* 設(shè)置圖片放大的比例 */         double scale = 1.25;         /* 計(jì)算這次要放大的比例 */         scaleWidth = (float) (scaleWidth * scale);         scaleHeight = (float) (scaleHeight * scale);         /* 產(chǎn)生reSize后的Bitmap對(duì)象 */         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.     <?xml version="1.0" encoding="utf-8"?>         

  2.     <FrameLayout 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.          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"         

  9.                     android:layout_width="fill_parent"         

  10.                     android:layout_height="fill_parent"         

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

  12.                     >         

  13.             <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"                

  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.             <HorizontalScrollView            

  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.                     <LinearLayout     

  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.                             <ImageView        

  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.                     </LinearLayout>         

  49.             </HorizontalScrollView >         

  50.             </ScrollView>        

  51.                  <ZoomControls android:id="@+id/zoomcontrol"     

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

  53.           android:layout_centerHorizontal="true"     

  54.                 android:layout_alignParentBottom="true"     

  55.           >     

  56.         </ZoomControls>     

  57.             </RelativeLayout>         

  58.     </FrameLayout>      

感謝各位的閱讀,以上就是“android新浪微博圖片縮放效果怎么實(shí)現(xiàn)”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)android新浪微博圖片縮放效果怎么實(shí)現(xiàn)這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

新聞名稱(chēng):android新浪微博圖片縮放效果怎么實(shí)現(xiàn)
當(dāng)前鏈接:http://www.muchs.cn/article6/geecig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、網(wǎng)站收錄全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站制作網(wǎng)站策劃、動(dòng)態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作