本文實例講述了android開發(fā)通過Scroller實現(xiàn)過渡滑動效果。分享給大家供大家參考,具體如下:
為雞西梨樹等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及雞西梨樹網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計制作、成都網(wǎng)站設(shè)計、雞西梨樹網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
主要介紹一下Scroller這個類,它可以實現(xiàn)過渡滑動的效果,使滑動看起來不是那么生硬,當(dāng)然它用大量的重繪來實現(xiàn),invalidate();通過源碼看:
看構(gòu)造方法
/** * Create a Scroller with the default duration and interpolator. */ public Scroller(Context context) { this(context, null); } /** * Create a Scroller with the specified interpolator. If the interpolator is * null, the default (viscous) interpolator will be used. "Flywheel" behavior will * be in effect for apps targeting Honeycomb or newer. */ public Scroller(Context context, Interpolator interpolator) { this(context, interpolator, context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB); } /** * Create a Scroller with the specified interpolator. If the interpolator is * null, the default (viscous) interpolator will be used. Specify whether or * not to support progressive "flywheel" behavior in flinging. */ public Scroller(Context context, Interpolator interpolator, boolean flywheel) { mFinished = true; if (interpolator == null) { mInterpolator = new ViscousFluidInterpolator(); } else { mInterpolator = interpolator; } mPpi = context.getResources().getDisplayMetrics().density * 160.0f; mDeceleration = computeDeceleration(ViewConfiguration.getScrollFriction()); mFlywheel = flywheel; mPhysicalCoeff = computeDeceleration(0.84f); // look and feel tuning }
我們用默認(rèn)的就行,傳個context就行了,其他的什么差值器,先不管了
然后調(diào)用startScroll,傳遞我們歧視滑動位置和滑動的偏移量,還有可選的默認(rèn)持續(xù)時間,默認(rèn)為250毫秒
這個方法是用來賦值的,接下來會調(diào)用invalidate()進(jìn)行重新繪制,然后就會onDraw(),這時候會調(diào)用
computeScroll()這個方法,我們重寫這個方法,computeScrollOffset()是判斷動畫有沒有結(jié)束的一個方法,沒結(jié)束的時候,我們根據(jù)滑動的偏移位置進(jìn)行移動也就是scrollto到scroller的當(dāng)前位置,再次調(diào)用invalidate(),由此無數(shù)的重回進(jìn)行拼接形成了平滑的滑動
/** * Call this when you want to know the new location. If it returns true, * the animation is not yet finished. */ public boolean computeScrollOffset() { if (mFinished) { return false; } int timePassed = (int)(AnimationUtils.currentAnimationTimeMillis() - mStartTime); if (timePassed < mDuration) { switch (mMode) { case SCROLL_MODE: final float x = mInterpolator.getInterpolation(timePassed * mDurationReciprocal); mCurrX = mStartX + Math.round(x * mDeltaX); mCurrY = mStartY + Math.round(x * mDeltaY); break; case FLING_MODE: final float t = (float) timePassed / mDuration; final int index = (int) (NB_SAMPLES * t); float distanceCoef = 1.f; float velocityCoef = 0.f; if (index < NB_SAMPLES) { final float t_inf = (float) index / NB_SAMPLES; final float t_sup = (float) (index + 1) / NB_SAMPLES; final float d_inf = SPLINE_POSITION[index]; final float d_sup = SPLINE_POSITION[index + 1]; velocityCoef = (d_sup - d_inf) / (t_sup - t_inf); distanceCoef = d_inf + (t - t_inf) * velocityCoef; } mCurrVelocity = velocityCoef * mDistance / mDuration * 1000.0f; mCurrX = mStartX + Math.round(distanceCoef * (mFinalX - mStartX)); // Pin to mMinX <= mCurrX <= mMaxX mCurrX = Math.min(mCurrX, mMaxX); mCurrX = Math.max(mCurrX, mMinX); mCurrY = mStartY + Math.round(distanceCoef * (mFinalY - mStartY)); // Pin to mMinY <= mCurrY <= mMaxY mCurrY = Math.min(mCurrY, mMaxY); mCurrY = Math.max(mCurrY, mMinY); if (mCurrX == mFinalX && mCurrY == mFinalY) { mFinished = true; } break; } } else { mCurrX = mFinalX; mCurrY = mFinalY; mFinished = true; } return true; }
public void startScroll(int startX, int startY, int dx, int dy) { startScroll(startX, startY, dx, dy, DEFAULT_DURATION); } public void startScroll(int startX, int startY, int dx, int dy, int duration) { mMode = SCROLL_MODE; mFinished = false; mDuration = duration; mStartTime = AnimationUtils.currentAnimationTimeMillis(); mStartX = startX; mStartY = startY; mFinalX = startX + dx; mFinalY = startY + dy; mDeltaX = dx; mDeltaY = dy; mDurationReciprocal = 1.0f / (float) mDuration; }
public class MoveFreeView extends View{ private int movedX; private int movedY; private Scroller mScroller; public MoveFreeView(Context context) { super(context); } public MoveFreeView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); mScroller = new Scroller(context); } public MoveFreeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean onTouchEvent(MotionEvent event) { //獲取觸摸點到邊界坐標(biāo) int x = (int) event.getX(); int y = (int) event.getY(); switch (event.getAction()){ case MotionEvent.ACTION_DOWN: movedX = x; movedY = y; break; case MotionEvent.ACTION_MOVE: int offsetX = x-movedX; int offsetY = y-movedY; layout(getLeft()+offsetX,getTop()+offsetY,getRight()+offsetX,getBottom()+offsetY); break; } return super.onTouchEvent(event); } //供外界調(diào)用通過傳遞x,y的的滑動距離 public void smoothScrollTo(int destinyX,int destinyY){ //向右側(cè),下方滑動,請傳遞負(fù)值 int scrollX = getScrollX(); int scrollY = getScrollY(); int delta = destinyX - scrollX; int deltaY = destinyY - scrollY; mScroller.startScroll(scrollX,scrollY,delta,deltaY,5000); invalidate(); } @Override public void computeScroll() { super.computeScroll(); //true則表示滑動未結(jié)束 if (mScroller.computeScrollOffset()){ ((View) getParent()).scrollTo(mScroller.getCurrX(),mScroller.getCurrY()); invalidate(); } } }
private MoveFreeView button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (MoveFreeView) findViewById(R.id.custon); button.smoothScrollTo(-400,-300); // button.startAnimation(AnimationUtils.loadAnimation(this,R.anim.translate)); // ObjectAnimator animtor1 = ObjectAnimator.ofFloat(button, "translationX", 0, 300); // ObjectAnimator animtor2 = ObjectAnimator.ofFloat(button, "translationY", 0, 300); // ObjectAnimator animator3 = ObjectAnimator.ofFloat(button,"rotationX",0.0f,360f); // ObjectAnimator animator4 = ObjectAnimator.ofFloat(button,"scaleX",1.5f,0.5f); // AnimatorSet set= new AnimatorSet(); // set.setDuration(5000); // set.playTogether(animtor1,animtor2,animator3,animator4); // set.addListener(new Animator.AnimatorListener() { // @Override // public void onAnimationStart(Animator animator) { // // } // // @Override // public void onAnimationEnd(Animator animator) { // //動畫結(jié)束時做一些事情 // } // // @Override // public void onAnimationCancel(Animator animator) { // // } // // @Override // public void onAnimationRepeat(Animator animator) { // // } // }); // set.start(); } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
網(wǎng)站題目:android開發(fā)通過Scroller實現(xiàn)過渡滑動效果操作示例
URL分享:http://aaarwkj.com/article0/ijheoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、虛擬主機、網(wǎng)站策劃、云服務(wù)器、關(guān)鍵詞優(yōu)化、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)