<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了Android實現橡皮筋回彈和平移縮放的具體程式碼,供大家參考,具體內容如下
由於最近在做一個view的平移縮放功能以及橡皮筋效果,不過網上查到的大多數都是分開實現的,所以我這裡把這兩種功能整合到了一起
這裡我寫把效果分開來寫,最後再合併
平移、縮放
mLayout.java
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.FrameLayout; import android.widget.Scroller; /** * Created by ChenZehao * on 2019/8/4 */ public class mLayout extends FrameLayout{ // 屬性變數 private float translationX; // 移動X private float translationY; // 移動Y private float scale = 1; // 伸縮比例 // 移動過程中臨時變數 private float actionX; private float actionY; private float spacing; private int moveType; // 0=未選擇,1=拖動,2=縮放 private float firstX; private float firstY; public mLayout(Context context) { this(context, null); } public mLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public mLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: moveType = 1; actionX = event.getRawX(); actionY = event.getRawY(); firstX = actionX; firstY = actionY; break; case MotionEvent.ACTION_POINTER_DOWN: moveType = 2; spacing = getSpacing(event); break; case MotionEvent.ACTION_MOVE: if (moveType == 1) { translationX = translationX + event.getRawX() - actionX; translationY = translationY + event.getRawY() - actionY; System.out.println(); setTranslationX(translationX); setTranslationY(translationY); actionX = event.getRawX(); actionY = event.getRawY(); } else if (moveType == 2) { scale = scale * getSpacing(event) / spacing; if(scale >= 1){ setScaleX(scale); setScaleY(scale); }else{ scale = 1; } } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: moveType = 0; break; } return true; } // 觸碰兩點間距離 private float getSpacing(MotionEvent event) { //通過三角函數得到兩點間的距離 float x = event.getX(0) - event.getX(1); float y = event.getY(0) - event.getY(1); return (float) Math.sqrt(x * x + y * y); } }
橡皮筋回彈
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.FrameLayout; import android.widget.Scroller; /** * Created by ChenZehao * on 2019/8/4 */ public class mLayout extends FrameLayout{ //係數可自己更改 private static final float DEFAULT_FATOR = 0.4f; /** * 阻尼因子 */ private float mFator = DEFAULT_FATOR; private Scroller mScroller; /** * 記錄上一次觸控事件 */ private MotionEvent mLastMotionEvent; public mLayout(Context context) { this(context, null); } public mLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public mLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mScroller = new Scroller(context); } @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mLastMotionEvent = MotionEvent.obtain(event); break; case MotionEvent.ACTION_MOVE: int dx = (int) (event.getRawX() - mLastMotionEvent.getRawX()); int dy = (int) (event.getRawY() - mLastMotionEvent.getRawY()); //如果不想對四個方向增加阻尼效果,直接刪除即可 //向上平移 if ((Math.abs(dx) < Math.abs(dy)) && dy < 0){ smoothScrollBy(0, -(int) (dy * mFator)); } //向下平移 else if (Math.abs(dx) < Math.abs(dy) && dy > 0) { smoothScrollBy(0, -(int) (dy * mFator)); } //向左平移 else if (Math.abs(dx) > Math.abs(dy) && dx < 0){ smoothScrollBy(-(int) (dx * mFator), 0); } //向右平移 else if (Math.abs(dx) > Math.abs(dy) && dx > 0){ smoothScrollBy(-(int) (dx * mFator), 0); } mLastMotionEvent = MotionEvent.obtain(event); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: smoothScrollTo(0, 0); break; } return true; } private void smoothScrollBy(int dx, int dy) { mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy); invalidate(); } private void smoothScrollTo(int fx, int fy) { int dx = fx - mScroller.getFinalX(); int dy = fx - mScroller.getFinalY(); smoothScrollBy(dx, dy); } @Override public void computeScroll() { if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); postInvalidate(); } super.computeScroll(); } }
平移、縮放、阻尼效果合併
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.FrameLayout; import android.widget.Scroller; /** * Created by ChenZehao * on 2019/8/4 */ public class mLayout extends FrameLayout{ private float scale = 1; // 伸縮比例 // 移動過程中臨時變數 private float actionX; private float actionY; private float spacing; private int moveType; // 0=未選擇,1=拖動,2=縮放 private float firstX; private float firstY; //係數可自己更改 private static final float DEFAULT_FATOR = 0.4f; /** * 阻尼因子 */ private float mFator = DEFAULT_FATOR; private Scroller mScroller; /** * 記錄上一次觸控事件 */ private MotionEvent mLastMotionEvent; public mLayout(Context context) { this(context, null); } public mLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public mLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mScroller = new Scroller(context); } @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: mLastMotionEvent = MotionEvent.obtain(event); moveType = 1; actionX = event.getRawX(); actionY = event.getRawY(); firstX = actionX; firstY = actionY; break; case MotionEvent.ACTION_POINTER_DOWN: moveType = 2; spacing = getSpacing(event); break; case MotionEvent.ACTION_MOVE: if (moveType == 1) { int dx = (int) (event.getRawX() - mLastMotionEvent.getRawX()); int dy = (int) (event.getRawY() - mLastMotionEvent.getRawY()); //如果不想對四個方向增加阻尼效果,直接刪除即可 //向上平移 if ((Math.abs(dx) < Math.abs(dy)) && dy < 0){ smoothScrollBy(0, -(int) (dy * mFator)); } //向下平移 else if (Math.abs(dx) < Math.abs(dy) && dy > 0) { smoothScrollBy(0, -(int) (dy * mFator)); } //向左平移 else if (Math.abs(dx) > Math.abs(dy) && dx < 0){ smoothScrollBy(-(int) (dx * mFator), 0); } //向右平移 else if (Math.abs(dx) > Math.abs(dy) && dx > 0){ smoothScrollBy(-(int) (dx * mFator), 0); } mLastMotionEvent = MotionEvent.obtain(event); } else if (moveType == 2) { scale = scale * getSpacing(event) / spacing; if(scale >= 1){ setScaleX(scale); setScaleY(scale); }else{ scale = 1; } } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_CANCEL: moveType = 0; if(scale == 1) smoothScrollTo(0, 0); break; } return true; } private void smoothScrollBy(int dx, int dy) { mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy); invalidate(); } private void smoothScrollTo(int fx, int fy) { int dx = fx - mScroller.getFinalX(); int dy = fx - mScroller.getFinalY(); smoothScrollBy(dx, dy); } @Override public void computeScroll() { if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); postInvalidate(); } super.computeScroll(); } // 觸碰兩點間距離 private float getSpacing(MotionEvent event) { //通過三角函數得到兩點間的距離 float x = event.getX(0) - event.getX(1); float y = event.getY(0) - event.getY(1); return (float) Math.sqrt(x * x + y * y); } }
在xml檔案中新增mLayout佈局,便可對mLayout裡面的控制元件和佈局進行平移、縮放、阻尼效果的操作
功能擴充套件——在佈局中新增button
如果我們在mLayout佈局中新增button,那麼會出現獲取焦點衝突的問題,導致觸控到按鈕時無法進行平移等操作,因此我們需要重寫button的dispatchTouchEvent函數,因此要建立一個類mButton來繼承Button
點選時事件被button獲取,因此要將事件通過dispatchTouchEvent回傳給父view,再呼叫父view的onInterceptTouchEvent函數對攔截到的事件進行處理。
程式碼如下:
mButton.java
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; public class mButton extends android.support.v7.widget.AppCompatButton { public mButton(Context context) { super(context); } public mButton(Context context, AttributeSet attrs) { super(context, attrs); } public mButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: getParent().requestDisallowInterceptTouchEvent(false); break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: break; } return super.dispatchTouchEvent(ev); } }
mLayout.java
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.FrameLayout; import android.widget.Scroller; /** * Created by ChenZehao * on 2019/8/4 */ public class mLayout extends FrameLayout{ private float scale = 1; // 伸縮比例 // 移動過程中臨時變數 private float actionX; private float actionY; private float spacing; private int moveType; // 0=未選擇,1=拖動,2=縮放 private float firstX; private float firstY; //係數可自己更改 private static final float DEFAULT_FATOR = 0.4f; /** * 阻尼因子 */ private float mFator = DEFAULT_FATOR; private Scroller mScroller; /** * 記錄上一次觸控事件 */ private MotionEvent mLastMotionEvent; public mLayout(Context context) { this(context, null); } public mLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public mLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mScroller = new Scroller(context); } @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: mLastMotionEvent = MotionEvent.obtain(event); moveType = 1; actionX = event.getRawX(); actionY = event.getRawY(); firstX = actionX; firstY = actionY; break; case MotionEvent.ACTION_POINTER_DOWN: moveType = 2; spacing = getSpacing(event); break; case MotionEvent.ACTION_MOVE: if (moveType == 1) { int dx = (int) (event.getRawX() - mLastMotionEvent.getRawX()); int dy = (int) (event.getRawY() - mLastMotionEvent.getRawY()); //如果不想對四個方向增加阻尼效果,直接刪除即可 //向上平移 if ((Math.abs(dx) < Math.abs(dy)) && dy < 0){ smoothScrollBy(0, -(int) (dy * mFator)); } //向下平移 else if (Math.abs(dx) < Math.abs(dy) && dy > 0) { smoothScrollBy(0, -(int) (dy * mFator)); } //向左平移 else if (Math.abs(dx) > Math.abs(dy) && dx < 0){ smoothScrollBy(-(int) (dx * mFator), 0); } //向右平移 else if (Math.abs(dx) > Math.abs(dy) && dx > 0){ smoothScrollBy(-(int) (dx * mFator), 0); } mLastMotionEvent = MotionEvent.obtain(event); } else if (moveType == 2) { scale = scale * getSpacing(event) / spacing; if(scale >= 1){ setScaleX(scale); setScaleY(scale); }else{ scale = 1; } } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_CANCEL: moveType = 0; if(scale == 1) smoothScrollTo(0, 0); break; } return true; } //攔截子button的事件 @Override public boolean onInterceptTouchEvent(MotionEvent event) { switch (event.getAction() & MotionEvent.ACTION_MASK){ case MotionEvent.ACTION_DOWN: mLastMotionEvent = MotionEvent.obtain(event); moveType = 1; actionX = event.getRawX(); actionY = event.getRawY(); firstX = actionX; firstY = actionY; break; case MotionEvent.ACTION_POINTER_DOWN: moveType = 2; spacing = getSpacing(event); break; case MotionEvent.ACTION_MOVE: if (moveType == 1) { int dx = (int) (event.getRawX() - mLastMotionEvent.getRawX()); int dy = (int) (event.getRawY() - mLastMotionEvent.getRawY()); //如果不想對四個方向增加阻尼效果,直接刪除即可 //向上平移 if ((Math.abs(dx) < Math.abs(dy)) && dy < 0){ smoothScrollBy(0, -(int) (dy * mFator)); } //向下平移 else if (Math.abs(dx) < Math.abs(dy) && dy > 0) { smoothScrollBy(0, -(int) (dy * mFator)); } //向左平移 else if (Math.abs(dx) > Math.abs(dy) && dx < 0){ smoothScrollBy(-(int) (dx * mFator), 0); } //向右平移 else if (Math.abs(dx) > Math.abs(dy) && dx > 0){ smoothScrollBy(-(int) (dx * mFator), 0); } mLastMotionEvent = MotionEvent.obtain(event); } else if (moveType == 2) { scale = scale * getSpacing(event) / spacing; if(scale >= 1){ setScaleX(scale); setScaleY(scale); }else{ scale = 1; } } break; case MotionEvent.ACTION_UP: moveType = 0; if(scale == 1) smoothScrollTo(0, 0); if(firstX != event.getRawX() || firstY != event.getRawY()) return true; break; case MotionEvent.ACTION_POINTER_UP: moveType = 0; if(scale == 1) smoothScrollTo(0, 0); break; case MotionEvent.ACTION_CANCEL: moveType = 0; if(scale == 1) smoothScrollTo(0, 0); break; } return super.onInterceptTouchEvent(event); } private void smoothScrollBy(int dx, int dy) { mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy); invalidate(); } private void smoothScrollTo(int fx, int fy) { int dx = fx - mScroller.getFinalX(); int dy = fx - mScroller.getFinalY(); smoothScrollBy(dx, dy); } @Override public void computeScroll() { if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); postInvalidate(); } super.computeScroll(); } // 觸碰兩點間距離 private float getSpacing(MotionEvent event) { //通過三角函數得到兩點間的距離 float x = event.getX(0) - event.getX(1); float y = event.getY(0) - event.getY(1); return (float) Math.sqrt(x * x + y * y); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45