本篇文章給大家分享的是有關(guān)怎么在Android中使用ScrollView實(shí)現(xiàn)一個(gè)下拉彈回動(dòng)畫效果,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
成都創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計(jì),桓仁網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:桓仁等地區(qū)?;溉首鼍W(wǎng)站價(jià)格咨詢:18980820575
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
一.自定義View的設(shè)計(jì)代碼
package com.lwz.mathbox.weight; import android.content.Context; import android.graphics.Rect; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.animation.TranslateAnimation; import android.widget.ScrollView; /** * 實(shí)現(xiàn)了可以有下拉彈回的ScrollView的自定義View */ public class SpringScrollView extends ScrollView { private View inner;// 孩子 private float y;// 坐標(biāo) private Rect normal = new Rect();// 矩形空白 public SpringScrollView(Context context, AttributeSet attrs) { super(context, attrs); } /*** * 根據(jù) XML 生成視圖工作完成.該函數(shù)在生成視圖的最后調(diào)用,在所有子視圖添加完之后. 即使子類覆蓋了 onFinishInflate * 方法,也應(yīng)該調(diào)用父類的方法,使該方法得以執(zhí)行. */ @Override protected void onFinishInflate() { if (getChildCount() > 0) { inner = getChildAt(0);// 獲取其孩子 } } @Override public boolean onTouchEvent(MotionEvent ev) { if (inner != null) { commOnTouchEvent(ev); } return super.onTouchEvent(ev); } /*** * 觸摸事件 * * @param ev */ public void commOnTouchEvent(MotionEvent ev) { int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: y = ev.getY();// 獲取點(diǎn)擊y坐標(biāo) break; case MotionEvent.ACTION_UP: if (isNeedAnimation()) { animation(); } break; case MotionEvent.ACTION_MOVE: final float preY = y; float nowY = ev.getY(); int deltaY = (int) (preY - nowY);// 獲取滑動(dòng)距離 y = nowY; // 當(dāng)滾動(dòng)到最上或者最下時(shí)就不會(huì)再滾動(dòng),這時(shí)移動(dòng)布局 if (isNeedMove()) { if (normal.isEmpty()) { // 填充矩形,目的:就是告訴this:我現(xiàn)在已經(jīng)有了,你松開的時(shí)候記得要執(zhí)行回歸動(dòng)畫. normal.set(inner.getLeft(), inner.getTop(), inner.getRight(), inner.getBottom()); } // 移動(dòng)布局 inner.layout(inner.getLeft(), inner.getTop() - deltaY / 2, inner.getRight(), inner.getBottom() - deltaY / 2); } break; default: break; } } /*** * 開啟動(dòng)畫移動(dòng) */ public void animation() { // 開啟移動(dòng)動(dòng)畫 TranslateAnimation ta = new TranslateAnimation(0, 0, inner.getTop(), normal.top); ta.setDuration(300); inner.startAnimation(ta); // 設(shè)置回到正常的布局位置 inner.layout(normal.left, normal.top, normal.right, normal.bottom); normal.setEmpty();// 清空矩形 } /*** * 是否需要開啟動(dòng)畫 * <p> * 如果矩形不為空,返回true,否則返回false. * * @return */ public boolean isNeedAnimation() { return !normal.isEmpty(); } /*** * 是否需要移動(dòng)布局 inner.getMeasuredHeight():獲取的是控件的高度 * getHeight():獲取的是當(dāng)前控件在屏幕中顯示的高度 * * @return */ public boolean isNeedMove() { int offset = inner.getMeasuredHeight() - getHeight(); int scrollY = getScrollY(); // 0是頂部,后面那個(gè)是底部 if (scrollY == 0 || scrollY == offset) { return true; } return false; } }
二.簡(jiǎn)單調(diào)用示例
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> //包名+類型 <com.lwz.mathbox.weight.SpringScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:orientation="vertical"> <EditText android:id="@+id/et_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" android:gravity="top" android:hint="輸入文字" android:minLines="4" android:singleLine="false" android:textSize="14sp" /> <TextView android:id="@+id/tv_size" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:gravity="right" android:text="0/255" /> </LinearLayout> </com.lwz.mathbox.weight.SpringScrollView> </LinearLayout>
以上就是怎么在Android中使用ScrollView實(shí)現(xiàn)一個(gè)下拉彈回動(dòng)畫效果,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)題目:怎么在Android中使用ScrollView實(shí)現(xiàn)一個(gè)下拉彈回動(dòng)畫效果
文章轉(zhuǎn)載:http://aaarwkj.com/article38/iipdpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、網(wǎng)站導(dǎo)航、品牌網(wǎng)站建設(shè)、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)