亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實(shí)現(xiàn)多點(diǎn)觸控,自由縮放圖片的實(shí)例代碼

 更新時(shí)間:2016年12月26日 16:46:39   作者:xiaohuanqi  
本篇文章主要介紹了Android實(shí)現(xiàn)多點(diǎn)觸控,自由縮放圖片的實(shí)例代碼,可以自由地對(duì)圖片進(jìn)行縮放和移動(dòng),非常具有實(shí)用價(jià)值,需要的朋友可以參考下。

Android多點(diǎn)觸控涉及到的知識(shí)點(diǎn)

1、ScaleGestureDetector

2、OnScaleGestureListener

3、Matrix

4、OnTouchListener

四個(gè)知識(shí)點(diǎn)需要了解一下,需要注意的是Matrix在內(nèi)存中是一個(gè)一維數(shù)組,操控圖片的Matrxi是一個(gè)3X3的矩陣,在內(nèi)存中也就是一個(gè)大小為9的一維數(shù)組。

實(shí)現(xiàn)多點(diǎn)觸控,自由變化圖片

1、 ImageView的基礎(chǔ)上繼承

2、因?yàn)橐趫D片加載完成就獲取到相關(guān)的屬性,所以實(shí)現(xiàn)OnGlobalLayoutListener接口,并實(shí)現(xiàn)方法onGlobalLayout

注冊(cè)O(shè)nGlobalLayoutListener接口:

 @Override
protected void onAttachedToWindow() {
 super.onAttachedToWindow();
 //注冊(cè) OnGlobalLayoutListener
 getViewTreeObserver().addOnGlobalLayoutListener(this);
}

@Override
protected void onDetachedFromWindow() {
 super.onDetachedFromWindow();
 //注銷 OnGlobalLayoutListener
 getViewTreeObserver().removeOnGlobalLayoutListener(this);
}

實(shí)現(xiàn)onGlobalLayout方法

 @Override
public void onGlobalLayout() {
 //因?yàn)橐诩虞d完成的時(shí)候就獲取到圖片的寬高 然后讓圖片的寬高去適應(yīng)控件的寬高大小 isOnce只在第一次加載到時(shí)候處理
 if (isOnce) {
  //下一步3 獲取相關(guān)屬性 并做處理
  isOnce = false;
 }
}

3、

 

//獲取控件的寬高
 int width = getWidth();
  int height = getHeight();
  //獲取圖片
  Drawable drawable = getDrawable();
  if (null == drawable) {
   return;
  }
  //獲取到圖片的寬高 **根據(jù)drawable的這兩個(gè)方法獲取
  int dw = drawable.getIntrinsicWidth();
  int dh = drawable.getIntrinsicHeight();

//定義一個(gè)圖片縮放值
 float scale = 1.0f;

接下來(lái)就是根據(jù)圖片的寬和高 控件的寬和高 去設(shè)置這個(gè)scale值

 //當(dāng)圖片的寬大于了控件的寬 圖片的高小于控件的高
if (dw > width && dh < height) {
  scale = width * 1.0f / dw;
 }
 //當(dāng)圖片的寬小于了控件的寬 圖片的高大于控件的高
if (dw < width && dh > height) {
  scale = height * 1.0f / dh;
 }

if ((dw > width && dh > height) || (dw < width && dh < height)) {
 scale = Math.min((width * 1.0f / dw), (height * 1.0f / dh));
}

//初始化三個(gè)縮放的值
  mInitScale = scale;//正常情況下的 縮放值
  mMidScale = scale * 2; //
  mMaxScale = scale * 4;//最大的縮放值 


//將圖片初始化加載到控件的正中心位置
  //計(jì)算橫縱需要移動(dòng)的偏移值
  float dx = getWidth() / 2f - dw / 2f;
  float dy = getHeight() / 2f - dh / 2f;
  //使用矩陣控制圖片的平移和縮放
  mMatrix.postTranslate(dx, dy);
  //縮放的時(shí)候要指定縮放基準(zhǔn)點(diǎn)
  mMatrix.postScale(mInitScale, mInitScale, getWidth() / 2f, getHeight() / 2f);
  //通過(guò)設(shè)置Matrix改變ImageView
  setImageMatrix(mMatrix);

4、接下來(lái)就是ScaleGestureDetector

//初始化 this是OnScaleGestureListener 對(duì)象
 mScaleGestureDetector = new ScaleGestureDetector(context, this);
  //要通過(guò)ScaleGestureDetector去操控觸摸事件,那還要實(shí)現(xiàn)OnTouchListener接口并實(shí)現(xiàn)onTouch方法,在該方法中將觸摸事件傳遞給mScaleGestureDetector 對(duì)象。

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
 //將觸摸事件傳遞給ScaleGesture
 mScaleGestureDetector.onTouchEvent(motionEvent);
 return true;
}
 //設(shè)置監(jiān)聽(tīng)
 setOnTouchListener(this);

5、OnScaleGestureListener 中的重要方法了

 //使用ScaleGestureListener去實(shí)現(xiàn)多點(diǎn)觸控
@Override
public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
 if (null == getDrawable()) {
  return true;
 }
//下一步6 處理

return true;
}

6、

 //縮放中
 //獲取當(dāng)前圖片縮放scale
 float scale = getCurrentScale();

 //獲取縮放因子
 float scaleFactor = scaleGestureDetector.getScaleFactor();
 //縮放值達(dá)到最大和最小的情況 scaleFactor>1表示正在放大 <1表示正在縮小
 if ((scale < mMaxScale && scaleFactor > 1.0f) || scale > mInitScale && scaleFactor < 1.0f) {
  if (scale * scaleFactor < mInitScale) {
   scaleFactor = mInitScale / scale;
  } else if (scale * scaleFactor > mMaxScale) {
   scaleFactor = mMaxScale / scale;
  }
 }

 //根據(jù)縮放因子去設(shè)置圖片的縮放 根據(jù)多點(diǎn)的中心去縮放 scaleGestureDetector.getFocusX(), scaleGestureDetector.getFocusY()縮放中心點(diǎn)一定是手指觸摸的中心點(diǎn)

  mMatrix.postScale(scaleFactor, scaleFactor, scaleGestureDetector.getFocusX(), scaleGestureDetector.getFocusY());

 //因?yàn)榭s放的中心點(diǎn)會(huì)改變 所以要控制圖片的邊界處理*** 如果不處理,中心點(diǎn)會(huì)根據(jù)你手指位置的不同發(fā)生改變,那么圖片位置會(huì)錯(cuò)亂
 checkoutBounds(); //下一步 7
 setImageMatrix(mMatrix);

7、checkoutBounds()

 private void checkoutBounds() {
 //通過(guò)矩陣要獲取到縮放后圖片的大小和坐標(biāo)
 Drawable drawable = getDrawable();
 if (null != drawable) {
  RectF rectF = getScaleMatrix(drawable); //下一步 8

  //獲取控件的寬高
  int width = getWidth();
  int height = getHeight();
  //聲明 x y偏移值 如果偏離了控件需要移動(dòng)回去
  float detalX = 0;
  float detalY = 0;

  if (rectF.width() >= width) {
   //圖片的寬大于等于了控件的寬,為了讓寬留白邊,計(jì)算出應(yīng)該左右移動(dòng)的偏移值
   if (0 < rectF.left) {
    //左邊留空白了 那就應(yīng)該像左移動(dòng)
    detalX = -rectF.left;
   } else if (rectF.right < width) {
    detalX = width - rectF.right;
   }
  }
  //高度控制
  if (rectF.height() >= height) {
   if (0 < rectF.top) {
    detalY = -rectF.top;
   } else if (rectF.bottom < height) {
    detalY = height - rectF.bottom;
   }
  }

  //圖片寬和高小于控件寬高的情況,讓圖片居中顯示
  if (rectF.width() < width) {
   //計(jì)算偏移值
   detalX = width / 2f - rectF.right + rectF.width() / 2f;
  }

  if (rectF.height() < height) {
   detalY = height / 2f - rectF.bottom + rectF.height() / 2f;
  }
  mMatrix.postTranslate(detalX, detalY);
}

8、getScaleMatrix(drawable) 該方法其他地方也可以效仿

//通過(guò)矩陣 去獲取到縮放后的圖片的四個(gè)頂點(diǎn)坐標(biāo)
public RectF getScaleMatrix(Drawable drawable) {
 Matrix matrix = mMatrix;
 //圖片的四個(gè)點(diǎn)坐標(biāo)
 RectF rectF = new RectF(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
 matrix.mapRect(rectF);
 return rectF;
}

通過(guò)該控件可以熟悉一下多點(diǎn)觸控的實(shí)現(xiàn) 和圖形矩陣的知識(shí)

Demo地址:ZoomImageView

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論