本文實例為大家分享了Android自定義View,可以仿點贊往上飄+1的一個特效,或者點擊加入購物車商品拋物線特效。
FloatingText 是一個能夠在任何控件之上執行漂浮效果動畫的控件。
原文github地址
效果圖
1. AndroidStudio使用
dependencies { compile 'com.ufreedom.uikit:FloatingTextLibrary:0.2.0' }
2. 使用
FloatingText floatingText = new FloatingText.FloatingTextBuilder(Activity) .textColor(Color.RED) // 漂浮字體的顏色 .textSize(100) // 浮字體的大小 .textContent("+1000") // 浮字體的內容 .offsetX(100) // FloatingText 相對其所貼附View的水平位移偏移量 .offsetY(100) // FloatingText 相對其所貼附View的垂直位移偏移量 .floatingAnimatorEffect(FloatingAnimator) // 漂浮動畫 .floatingPathEffect(FloatingPathEffect) // 漂浮的路徑 .build(); floatingText.attach3Window(); //將FloatingText貼附在Window上 //啟動漂浮效果 floatingText.startFloating(View); // 傳入一個View,FloatingText 就會相對于這個View執行漂浮效果
自定義漂浮動畫
通過實現 FloatingAnimator 接口可以實現自定義漂浮動畫,詳情查看原github。
自定義漂浮路徑
通過實現 FloatingPathEffect 和 FloatingPathAnimator 可以自定義路徑動畫
FloatingPath 代表浮動路徑
JAVA
// +1 向上移動效果 final View layoutTranslateFloating = findViewById(R.id.layoutTranslateView); final View translateFloatingView = findViewById(R.id.translateView); final FloatingText translateFloatingText = new FloatingText.FloatingTextBuilder(MainActivity.this) .textColor(Color.RED) .textSize(100) .textContent("+1") .build(); translateFloatingText.attach3Window(); assert layoutTranslateFloating != null; layoutTranslateFloating.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { translateFloatingText.startFloating(translateFloatingView); } }); //+1 放大效果 View layoutScaleView = findViewById(R.id.layoutScaleView); final View scaleView = findViewById(R.id.scaleView); final FloatingText scaleFloatingText = new FloatingText.FloatingTextBuilder(MainActivity.this) .textColor(Color.parseColor("#7ED321")) .textSize(100) .offsetY(-100) .floatingAnimatorEffect(new ScaleFloatingAnimator()) .textContent("+1") .build(); scaleFloatingText.attach3Window(); assert scaleView != null; assert layoutScaleView != null; layoutScaleView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { scaleFloatingText.startFloating(scaleView); } }); //自定義 螺旋上升動畫 final FloatingText cubicFloatingText = new FloatingText.FloatingTextBuilder(MainActivity.this) .textColor(Color.RED) .textSize(100) .floatingAnimatorEffect(new CurvePathFloatingAnimator()) .floatingPathEffect(new CurveFloatingPathEffect()) .textContent("Hello! ") .build(); cubicFloatingText.attach3Window(); View layoutCurveView = findViewById(R.id.layoutCurveView); final View curveView = findViewById(R.id.curveView); assert curveView != null; assert layoutCurveView != null; layoutCurveView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { cubicFloatingText.startFloating(curveView); } });
XML
<FrameLayout android:id="@+id/layoutTranslateView" android:layout_width="234.4dp" android:layout_height="80dp" android:layout_alignParentLeft="true" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" > <TextView android:id="@+id/translateView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" android:text="Translate Floating" android:textColor="@android:color/white" /> </FrameLayout>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。