Android开发之View双指缩放ViewGroup双指缩放视频双指缩放图片双指缩放
生活随笔
收集整理的這篇文章主要介紹了
Android开发之View双指缩放ViewGroup双指缩放视频双指缩放图片双指缩放
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
老套路先上圖
核心代碼使用view的縮放API即可
setScaleX(scale); setScaleY(scale);在看下自定義可縮放的view
package cn.xiayiye5.xiayiye5library.view;import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.RelativeLayout;/*** @author : xiayiye5* @date : 2021/3/26 15:37* 類描述 : 縮放ViewGroup*/ public class ScaleLayout extends RelativeLayout implements View.OnTouchListener {private boolean isCanTouch = false;/*** 當前觸摸的點數*/private int pointNum = 0;//最大的縮放比例public static final float SCALE_MAX = 8.0f;private static final float SCALE_MIN = 1.0f;private double oldDist = 0;private double moveDist = 0;/*** 針對控件的坐標系,即控件左上角為原點*/private double moveX = 0;private double moveY = 0;private double downX = 0;private double downY = 0;// 針對屏幕的坐標系,即屏幕左上角為原點private double moveRawX = 0;private double moveRawY = 0;public ScaleLayout(Context context) {this(context, null);}public ScaleLayout(Context context, AttributeSet attrs) {this(context, attrs, 0);}public ScaleLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);setOnTouchListener(this);init();}private void init() {setIsCanTouch(true);}public void setIsCanTouch(boolean canTouch) {isCanTouch = canTouch;}@Overridepublic boolean onTouch(View v, MotionEvent event) {if (!isCanTouch) {return false;}switch (event.getAction() & MotionEvent.ACTION_MASK) {case MotionEvent.ACTION_DOWN:pointNum = 1;downX = event.getX();downY = event.getY();break;case MotionEvent.ACTION_UP:pointNum = 0;downX = 0;downY = 0;break;case MotionEvent.ACTION_MOVE:if (pointNum == 1) {//只有一個手指的時候才有移動的操作float lessX = (float) (downX - event.getX());float lessY = (float) (downY - event.getY());moveX = event.getX();moveY = event.getY();moveRawX = event.getRawX();moveRawY = event.getRawY();setSelfPivot(lessX, lessY);//setPivot(getPivotX() + lessX, getPivotY() + lessY);} else if (pointNum == 2) {//只有2個手指的時候才有放大縮小的操作moveDist = spacing(event);double space = moveDist - oldDist;float scale = (float) (getScaleX() + space / getWidth());if (scale > SCALE_MIN && scale < SCALE_MAX) {setScale(scale);} else if (scale < SCALE_MIN) {setScale(SCALE_MIN);}}break;case MotionEvent.ACTION_POINTER_DOWN://兩點按下時的距離oldDist = spacing(event);pointNum += 1;break;case MotionEvent.ACTION_POINTER_UP:pointNum -= 1;break;default:break;}return true;}/*** 觸摸使用的移動事件** @param lessX x坐標* @param lessY y坐標*/private void setSelfPivot(float lessX, float lessY) {float setPivotX = 0;float setPivotY = 0;setPivotX = getPivotX() + lessX;setPivotY = getPivotY() + lessY;Log.e("lawwingLog", "setPivotX:" + setPivotX + " setPivotY:" + setPivotY+ " getWidth:" + getWidth() + " getHeight:" + getHeight());if (setPivotX < 0 && setPivotY < 0) {setPivotX = 0;setPivotY = 0;} else if (setPivotX > 0 && setPivotY < 0) {setPivotY = 0;if (setPivotX > getWidth()) {setPivotX = getWidth();}} else if (setPivotX < 0 && setPivotY > 0) {setPivotX = 0;if (setPivotY > getHeight()) {setPivotY = getHeight();}} else {if (setPivotX > getWidth()) {setPivotX = getWidth();}if (setPivotY > getHeight()) {setPivotY = getHeight();}}setPivot(setPivotX, setPivotY);}/*** 計算兩個點的距離** @param event 事件* @return 返回數值*/private double spacing(MotionEvent event) {if (event.getPointerCount() == 2) {float x = event.getX(0) - event.getX(1);float y = event.getY(0) - event.getY(1);return Math.sqrt(x * x + y * y);} else {return 0;}}/*** 平移畫面,當畫面的寬或高大于屏幕寬高時,調用此方法進行平移** @param x 坐標x* @param y 坐標y*/public void setPivot(float x, float y) {setPivotX(x);setPivotY(y);}/*** 設置放大縮小** @param scale 縮放值*/public void setScale(float scale) {setScaleX(scale);setScaleY(scale);}/*** 初始化比例,也就是原始比例*/public void setInitScale() {setScaleX(1.0f);setScaleY(1.0f);setPivot(getWidth() / 2f, getHeight() / 2f);} }上面代碼可使用于View,ImageView等相關控件
在看下xml布局怎么用
<cn.xiayiye5.xiayiye5library.view.ScaleLayoutandroid:id="@+id/sl"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="測試view group雙指縮放" /></cn.xiayiye5.xiayiye5library.view.ScaleLayout>再看下activity
package cn.xiayiye5.xiayiye5library.activity;import cn.xiayiye5.xiayiye5library.R; import cn.xiayiye5.xiayiye5library.view.ScaleLayout; import cn.xiayiye5.xiayiye5library.view.ScaleView;/*** @author : xiayiye5* @date : 2021/3/26 15:35* 類描述 :展示可縮放的頁面*/ public class ScaleActivity extends BaseActivity {private ScaleView sv;private ScaleLayout sl;@Overrideprotected int getLayoutView() {return R.layout.activity_scale;}@Overrideprotected void initId() {sv = findViewById(R.id.sv);sl = findViewById(R.id.sl);}@Overrideprotected void loadData() {//設置布局可以縮放sv.setIsCanTouch(true);sl.setIsCanTouch(true);} }好了代碼如上圖
在此感謝原博主:博主直達
GitHub源碼下載源碼頁查看下載
總結
以上是生活随笔為你收集整理的Android开发之View双指缩放ViewGroup双指缩放视频双指缩放图片双指缩放的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android开发之高仿百度地图底部滑出
- 下一篇: 华米新款 Amazfit Falcon