欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義實(shí)現(xiàn)可回彈的ScollView

 更新時(shí)間:2022年04月18日 14:13:07   作者:清風(fēng)一杯酒  
這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)可回彈的ScollView,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

  • 仿IOS回彈效果
  • 為了增強(qiáng)用戶體驗(yàn),自定義一個(gè)可回彈的ScrollView是一個(gè)不錯(cuò)的選擇,而且這種效果還是很簡(jiǎn)單的

把原來(lái)的ScollView標(biāo)簽替換一下就好了

<?xml version="1.0" encoding="utf-8"?>
<com.mycompany.myapp.MyScrollView
? ?xmlns:android="http://schemas.android.com/apk/res/android"
? ?android:layout_height="match_parent"
? ?android:layout_width="match_parent"
? ?android:fillViewport="true">

? ?<LinearLayout
? ? ? android:layout_height="match_parent"
? ? ? android:layout_width="match_parent"
? ? ? android:gravity="center"
? ? ? android:background="#FFABE346"
? ? ? android:elevation="1dp">

? ? ? <TextView
? ? ? ? ?android:layout_height="wrap_content"
? ? ? ? ?android:layout_width="wrap_content"
? ? ? ? ?android:text="可回彈的Scollview"/>

? ?</LinearLayout>

</com.mycompany.myapp.MyScrollView>
public class MyScrollView extends ScrollView
{
? ?private View convertView;
? ?private Rect originalRect=new Rect();
? ?private int startY,offsetY;

? ?public MyScrollView(Context context)
? ?{
? ? ? super(context);
? ?}
? ?public MyScrollView(Context context, AttributeSet attrs)
? ?{
? ? ? super(context, attrs);
? ?}
? ?public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr)
? ?{
? ? ? super(context, attrs, defStyleAttr);
? ?}

? ?@Override
? ?protected void onFinishInflate()
? ?{
? ? ? super.onFinishInflate();
? ? ? //獲取子視圖
? ? ? convertView = getChildAt(0);
? ?}

? ?@Override
? ?protected void onLayout(boolean changed, int l, int t, int r, int b)
? ?{
? ? ? super.onLayout(changed, l, t, r, b);
? ? ? //記錄原來(lái)的位置
? ? ? originalRect.set(l,t,r,b);
? ?}
? ?
? ?
? ?@Override
? ?public boolean dispatchTouchEvent(MotionEvent ev)
? ?{
? ? ? switch (ev.getAction())
? ? ? {
? ? ? ? ?case MotionEvent.ACTION_DOWN:
? ? ? ? ? ? {
? ? ? ? ? ? ? ?//記錄第一次的手指觸摸位置
? ? ? ? ? ? ? ?startY = (int) ev.getY();
? ? ? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? ?case MotionEvent.ACTION_MOVE:
? ? ? ? ? ? {
? ? ? ? ? ? ? ?//記錄拖動(dòng)時(shí)的手指觸摸位置
? ? ? ? ? ? ? ?offsetY = ((int) ev.getY()) - startY;
? ? ? ? ? ? ? ?//讓子視圖跟隨手指拖動(dòng)
? ? ? ? ? ? ? ?convertView.layout(originalRect.left,originalRect.top+(int)(offsetY*0.5f)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ,originalRect.right,originalRect.bottom+(int)(offsetY*0.5f));
? ? ? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? ?case MotionEvent.ACTION_UP:
? ? ? ? ? ? {
? ? ? ? ? ? ? ?//回彈動(dòng)畫
? ? ? ? ? ? ? ?TranslateAnimation offsetAnim=new TranslateAnimation(0,0,convertView.getTop(),originalRect.top);
? ? ? ? ? ? ? ?offsetAnim.setDuration(200);
? ? ? ? ? ? ? ?convertView.startAnimation(offsetAnim);
? ? ? ? ? ? ? ?//讓子視圖回到原來(lái)的位置
? ? ? ? ? ? ? ?convertView.layout(originalRect.left,originalRect.top,originalRect.right,originalRect.bottom);
? ? ? ? ? ? }
? ? ? }
? ? ? return super.dispatchTouchEvent(ev);
? ?}
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論