本文章分為兩部分,第一部分先講解下需要用到的知識點,第二部分提供代碼實例。文章來源:http://www.zghlxwxcb.cn/news/detail-623551.html
相關(guān)知識
MotionEvent中的動作符
-
ACTION_DOWN:單指按下動作(僅在第一根手指按下時有效)
-
ACTION_POINTER_DOWN:雙指及多指按下動作(僅在第二根手指或者大于第二根手指按下時有效)
-
ACTION_POINTER_UP:雙指及多汁抬起動作(僅在第二根手指或者大于第二根的手指抬起時有效)
-
ACTION_UP:單指抬起動作(僅在第一根手指抬起時有效)
-
ACTION_MOVE:手指移動動作(不限手指個數(shù))
-
ACTION_SCROLL:view上下或者左右滑動動作
縮放及位移方法
-
setTranslationX:控件沿x軸的位移,負(fù)數(shù)左移,正數(shù)右移
-
setTranslationY:控件沿y軸的位移,負(fù)數(shù)上移,正數(shù)下移
-
setScaleX:控件沿x軸縮放比
-
setScaleY:控件沿y軸縮放比
代碼示例
package com.example.myapplication;
import static android.content.ContentValues.TAG;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
private static final String TAG = "xiong_tag";
private float oldDistance;//剛按下時雙指之間的距離
private float newDistance;//在屏幕上滑動后雙指之間的距離
private float scalePoint;//縮放中心點
private float scale = 1f;//縮放比
private float translationX;//x軸移動量
private float translationY;//y軸位移量
private float oldCenterX;//剛按下時雙指之間的點的x坐標(biāo)
private float oldCenterY;//剛按下時雙指之間的點的y坐標(biāo)
private float newCenterX;//在屏幕上滑動后雙指之間的點的x坐標(biāo)
private float newCenterY;//在屏幕上滑動后雙指之間的點的y坐標(biāo)
private ImageView imageView;//縮放控件,可以是別的控件,如果是surfaceView那么要注意如果Android版本低于或者等于6,那么是不支持用這個方法進(jìn)行縮放的
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageview);
imageView.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_POINTER_DOWN:
Log.d(TAG, "onTouch: pointer down");
if(event.getPointerCount() == 2) {//getPointerCount返回的是手指的數(shù)量
oldDistance = calculateDistance(event);//計算距離
oldCenterX = calculateCenter(event, true);//計算兩指之間的中心點的x坐標(biāo)
oldCenterY = calculateCenter(event, false);//計算兩指之間的中心點的y坐標(biāo)
}
break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "onTouch: move");
if (event.getPointerCount() == 2) {
newDistance = calculateDistance(event);
scale += (newDistance - oldDistance) / oldDistance;
newCenterX = calculateCenter(event, true);
newCenterY = calculateCenter(event, false);
//縮放
imageView.setScaleX(scale);
imageView.setScaleY(scale);
//位移
translationX += newCenterX - oldCenterX;
translationY += newCenterY - oldCenterY;
imageView.setTranslationX(translationX);
imageView.setTranslationY(translationY);
}
break;
}
return true;
}
private float calculateDistance(MotionEvent motionEvent) {
float x1 = motionEvent.getX(0);//第一個點x坐標(biāo)
float x2 = motionEvent.getX(1);//第二個點x坐標(biāo)
float y1 = motionEvent.getY(0);//第一個點y坐標(biāo)
float y2 = motionEvent.getY(1);//第二個點y坐標(biāo)
return (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
/*
*@param isX 是否是x坐標(biāo)
*/
private float calculateCenter(MotionEvent motionEvent, boolean isX) {
return isX ? (motionEvent.getX(1) + motionEvent.getX(0)) / 2 : (motionEvent.getY(1) + motionEvent.getY(0)) / 2;
}
}
以上就是全部內(nèi)容了,謝謝觀看。文章來源地址http://www.zghlxwxcb.cn/news/detail-623551.html
到了這里,關(guān)于Android控件雙指縮放及雙指拖動的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!