欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)通過手勢控制圖片大小縮放的方法

 更新時(shí)間:2016年10月21日 11:22:45   作者:pku_android  
這篇文章主要介紹了Android實(shí)現(xiàn)通過手勢控制圖片大小縮放的方法,結(jié)合實(shí)例形式分析了Android控制圖片縮放的原理、實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android實(shí)現(xiàn)通過手勢控制圖片大小縮放的方法。分享給大家供大家參考,具體如下:

該程序?qū)崿F(xiàn)的是通過手勢來縮放圖片,從左向右揮動(dòng)圖片時(shí)圖片被放大,從右向左揮動(dòng)圖片時(shí)圖片被縮小,揮動(dòng)速度越快,縮放比越大。程序思路如下:在界面中定義一個(gè)ImageView來顯示圖片,使用一個(gè)GestureDetector來檢測用戶的手勢,并根據(jù)用戶的手勢在橫向的速度來縮放圖片。

在介紹這個(gè)實(shí)例前,先介紹一下Android中處理手勢觸摸事件的大概框架。

一、添加語句實(shí)現(xiàn)OnGestureListener手勢監(jiān)聽器,代碼如下:

public classGestureZoom extends Activity implements OnGestureListener

二、定義一個(gè)手勢監(jiān)聽器的全局實(shí)例,并在onCreate函數(shù)中對其進(jìn)行初始化,代碼如下:

GestureDetector detector;
@Override
public void onCreate(Bundle savedInstanceState)
{
  ... ...
  detector = new GestureDetector(this);
}

三、重寫onTouchEvent函數(shù),把本Activity的觸摸事件交給GestureDetector處理,代碼如下:

@Override
public boolean onTouchEvent(MotionEvent me)
{
  return detector.onTouchEvent(me);
}

四、重寫你需要監(jiān)聽的手勢的函數(shù),默認(rèn)包括如下幾種手勢:

BooleanonDown(MotionEvent e):按下。
BooleanonFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY):拖過、滑動(dòng)。
abstract voidonLongPress(MotionEvent e):長按。
BooleanonScroll(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY):滾動(dòng)。
voidonShowPress(MotionEvent e):按下且未移動(dòng)和松開。
BooleanonSingleTapUp(MotionEvent e):輕擊。

這幾種手勢是系統(tǒng)默認(rèn)提供的,根據(jù)描述大家可能還是不太明確這幾種手勢,最好的方法就是大家可以實(shí)現(xiàn)一個(gè)簡單的程序?qū)嶒?yàn)一下就明白了。當(dāng)然,除了這些默認(rèn)的手勢,也可以自行添加手勢,篇幅有限就不再贅述了。

接下來給出通過滑動(dòng)來實(shí)現(xiàn)圖片縮放的實(shí)例,對比上面給出的基本框架,其實(shí)就是重寫了onFling函數(shù),在其中定義了如何處理滑動(dòng)事件。

首先定義除了手勢監(jiān)聽器外一些全局對象,并在onCreate函數(shù)中做相應(yīng)的初始化:

GestureDetectordetector;
ImageViewimageView;
Bitmap bitmap;//保存圖片資源
int width,height;// 記錄圖片的寬、高
floatcurrentScale = 1;// 記錄當(dāng)前的縮放比
Matrix matrix;//控制圖片縮放的Matrix對象
@Override
public voidonCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    detector = new GestureDetector(this);
    imageView = (ImageView)findViewById(R.id.show);
    matrix = new Matrix();
    bitmap =BitmapFactory.decodeResource(this.getResources(), <你的圖片資源>);//獲取被縮放的源圖片,因?yàn)椴荒軐υ袌D片進(jìn)行修改,所以必須轉(zhuǎn)化為位圖
    width = bitmap.getWidth();
    height = bitmap.getHeight();
    imageView.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), <你的圖片資源>));//設(shè)置ImageView初始化時(shí)顯示的圖片
}

一、觸摸時(shí)間綁定手勢監(jiān)聽器,和前面是一樣的,就不再貼代碼了。

二、重寫onFling函數(shù):

@Override
publicboolean onFling(MotionEvent event1, MotionEvent event2
, float velocityX, float velocityY)
{
   velocityX = velocityX > 4000 ? 4000 :velocityX;
   velocityX = velocityX < -4000 ? -4000: velocityX;
   //根據(jù)手勢的速度來計(jì)算縮放比,如果velocityX>0,放大圖像,否則縮小圖像。
   currentScale += currentScale * velocityX/ 4000.0f;
   //保證currentScale不會(huì)等于0
   currentScale = currentScale > 0.01 ?currentScale : 0.01f;
   // 重置Matrix
   matrix.reset();
   // 縮放Matrix
   matrix.setScale(currentScale,currentScale , 160 , 200);
   BitmapDrawable tmp = (BitmapDrawable)imageView.getDrawable();
   //如果圖片還未回收,先強(qiáng)制回收該圖片
   if (!tmp.getBitmap().isRecycled())
   {
       tmp.getBitmap().recycle();
   }
   // 根據(jù)原始位圖和Matrix創(chuàng)建新圖片
   Bitmap bitmap2 =Bitmap.createBitmap(bitmap
       ,0, 0, width, height, matrix, true);
   // 顯示新的位圖
   imageView.setImageBitmap(bitmap2);
   return true;
}

布局文件僅僅添加了一個(gè)ImageView控件,大家自己畫一下。在這里沒有截圖,因?yàn)榻貓D也看不出效果,大家就自己試試吧。好了,至此就實(shí)現(xiàn)了通過手勢滑動(dòng)來實(shí)現(xiàn)圖片縮放,以上內(nèi)容學(xué)習(xí)自瘋狂Android一書。

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論