android 自定义view如何控制view的高度_Android自定义View属性动画
屬性動畫
DEMO地址:https://github.com/chaozhouzhang/CustomProgressView
1、值動畫 ValueAnimator
值動畫具體實現(xiàn)步驟:
/**?*?屬性動畫之值動畫:
?*?1、指定動畫數(shù)值區(qū)間
?*?2、指定插值器,定義動畫數(shù)值的進度變化規(guī)律
?*?3、指定估值器,根據(jù)插值器的進度變化規(guī)律,計算具體的動畫數(shù)值
?*?4、設置監(jiān)聽器,監(jiān)聽估值器返回的具體動畫數(shù)值,然后根據(jù)此具體數(shù)值,來對控件做出屬性變化的設置操作
?*/
使用值動畫,實現(xiàn)拋物動畫:
ValueAnimator?animator?=?ValueAnimator.ofObject(new?FallingBallEvaluator(),new?Point(0,0),new?Point(500,500));animator.addUpdateListener(new?ValueAnimator.AnimatorUpdateListener()?{
????@Override
????public?void?onAnimationUpdate(ValueAnimator?animation)?{
????????mPoint?=?(Point)?animation.getAnimatedValue();
????????mIvBall.layout(mPoint.x,mPoint.y,mPoint.x+mIvBall.getWidth(),mPoint.y+mIvBall.getHeight());
????}
});
animator.setDuration(2000);
animator.start();
DEMO地址:https://github.com/chaozhouzhang/CustomProgressView/blob/master/CustomProgressView/app/src/main/java/androidstack/progress/animation/property/value/FallingBallValueActivity.java
2、對象動畫 ObjectAnimator
對象動畫具體實現(xiàn)步驟:
/**?*?屬性動畫之對象動畫:
?*?1、指定動畫數(shù)值區(qū)間以及屬性和控件
?*?2、指定插值器,定義動畫數(shù)值的進度變化規(guī)律
?*?3、指定估值器,根據(jù)插值器的進度變化規(guī)律,計算具體的動畫數(shù)值
?*?4、系統(tǒng)自定調(diào)用控件的set方法,根據(jù)屬性拼裝set方法并反射調(diào)用,并將當前值作為參數(shù)傳入
?*/
自定義ImageView:
public?class?FallingImageView?extends?androidx.appcompat.widget.AppCompatImageView?{????public?FallingImageView(Context?context)?{
????????super(context);
????}
????public?FallingImageView(Context?context,?AttributeSet?attrs)?{
????????super(context,?attrs);
????}
????public?FallingImageView(Context?context,?AttributeSet?attrs,?int?defStyleAttr)?{
????????super(context,?attrs,?defStyleAttr);
????}
????/**
?????*?對象動畫會使用反射自動調(diào)用此set方法
?????*?@param?point
?????*/
????public?void?setFallingPos(Point?point){
????????layout(point.x,point.y,point.x+getWidth(),point.y+getHeight());
????}
????/**
?????*?當指定一個動畫值時,動畫會通過get方法來獲取初始值,否則會崩潰
?????*?@return
?????*/
????public?Point?getFallingPos(){
????????return?new?Point(0,0);
????}?
}
使用值動畫,實現(xiàn)拋物動畫:
ObjectAnimator?objectAnimator?=?ObjectAnimator.ofObject(mIvBall,"fallingPos",new?FallingBallEvaluator(),new?Point(0,0),new?Point(500,500));objectAnimator.setDuration(2000);
objectAnimator.start();
DEMO地址:https://github.com/chaozhouzhang/CustomProgressView/blob/master/CustomProgressView/app/src/main/java/androidstack/progress/animation/property/object/FallingBallObjectActivity.java
3、動畫集合 AnimatorSet
使用動畫集合實現(xiàn)菜單動畫:
開:
private?void?menuOpen(View?view,?int?currIndex,?int?totalIndex,?int?radius)?{????//每一份角度
????double?degree?=?Math.toRadians(90)?/?(totalIndex?-?1)?*?currIndex;
????//X軸移動距離
????int?translationX?=?-(int)?(radius?*?Math.sin(degree));
????//Y軸移動距離
????int?translationY?=?-(int)?(radius?*?Math.cos(degree));
????AnimatorSet?animatorSet?=?new?AnimatorSet();
????ObjectAnimator?objectAnimatorTranslationX?=?ObjectAnimator.ofFloat(view,?"translationX",?0,?translationX);
????ObjectAnimator?objectAnimatorTranslationY?=?ObjectAnimator.ofFloat(view,?"translationY",?0,?translationY);
????ObjectAnimator?objectAnimatorScaleX?=?ObjectAnimator.ofFloat(view,?"ScaleX",?0F,?1F);
????ObjectAnimator?objectAnimatorScaleY?=?ObjectAnimator.ofFloat(view,?"ScaleY",?0F,?1F);
????ObjectAnimator?objectAnimatorAlpha?=?ObjectAnimator.ofFloat(view,?"alpha",?0F,?1F);
????animatorSet.playTogether(objectAnimatorTranslationX,?objectAnimatorTranslationY,?objectAnimatorScaleX,?objectAnimatorScaleY,?objectAnimatorAlpha);
????animatorSet.setDuration(500);
????animatorSet.start();
}
關(guān):
private?void?menuClose(View?view,?int?currIndex,?int?totalIndex,?int?radius)?{????//每一份角度
????double?degree?=?Math.PI?*?currIndex?/((totalIndex-1)*2);
????//X軸移動距離
????int?translationX?=?-(int)?(radius?*?Math.sin(degree));
????//Y軸移動距離
????int?translationY?=?-(int)?(radius?*?Math.cos(degree));
????
????AnimatorSet?animatorSet?=?new?AnimatorSet();
????ObjectAnimator?objectAnimatorTranslationX?=?ObjectAnimator.ofFloat(view,?"translationX",?translationX,?0);
????ObjectAnimator?objectAnimatorTranslationY?=?ObjectAnimator.ofFloat(view,?"translationY",?translationY,?0);
????ObjectAnimator?objectAnimatorScaleX?=?ObjectAnimator.ofFloat(view,?"ScaleX",?1F,?0F);
????ObjectAnimator?objectAnimatorScaleY?=?ObjectAnimator.ofFloat(view,?"ScaleY",?1F,?0F);
????ObjectAnimator?objectAnimatorAlpha?=?ObjectAnimator.ofFloat(view,?"alpha",?1F,?0F);
????animatorSet.playTogether(objectAnimatorTranslationX,?objectAnimatorTranslationY,?objectAnimatorScaleX,?objectAnimatorScaleY,?objectAnimatorAlpha);
????animatorSet.setDuration(500);
????animatorSet.start();
}
DEMO地址:https://github.com/chaozhouzhang/CustomProgressView/blob/master/CustomProgressView/app/src/main/java/androidstack/progress/animation/property/set/MenuActivity.java
4、屬性值與關(guān)鍵幀 PropertyValuesHolder Keyframe
/**?*?關(guān)鍵幀,為了方便控制動畫速率,表示某個時間點應該在某個位置上
?*?視頻,1秒要播24幀圖片
?*?一個關(guān)鍵幀的兩個元素:時間點和位置
?*?fraction?動畫進度
?*?value?對應動畫進度的動畫數(shù)值
?*?KeyFrame可以設置插值器,默認使用線性插值器
?*?可以做電話響鈴的動畫,一張電話圖片,11張關(guān)鍵幀
?*/
使用屬性值和關(guān)鍵幀實現(xiàn)電話鈴響動畫:
//旋轉(zhuǎn)效果,左右震動Keyframe?keyframe1?=?Keyframe.ofFloat(0f,?0);
Keyframe?keyframe2?=?Keyframe.ofFloat(0.1f,?-20f);
Keyframe?keyframe3?=?Keyframe.ofFloat(0.2f,?20f);
Keyframe?keyframe4?=?Keyframe.ofFloat(0.3f,?-20f);
Keyframe?keyframe5?=?Keyframe.ofFloat(0.4f,?20f);
Keyframe?keyframe6?=?Keyframe.ofFloat(0.5f,?-20f);
Keyframe?keyframe7?=?Keyframe.ofFloat(0.6f,?20f);
Keyframe?keyframe8?=?Keyframe.ofFloat(0.7f,?-20f);
Keyframe?keyframe9?=?Keyframe.ofFloat(0.8f,?20f);
Keyframe?keyframe10?=?Keyframe.ofFloat(0.9f,?-20f);
Keyframe?keyframe11?=?Keyframe.ofFloat(1.0f,?0f);
PropertyValuesHolder?propertyValuesHolder?=?PropertyValuesHolder.ofKeyframe("rotation",
????????keyframe1,?keyframe1,?keyframe2,?keyframe3,?keyframe4,?keyframe5,
????????keyframe6,?keyframe7,?keyframe8,?keyframe9,?keyframe10,?keyframe11);
//縮放效果?X
Keyframe?keyframeScaleX0?=?Keyframe.ofFloat(0,1f);
Keyframe?keyframeScaleX1?=?Keyframe.ofFloat(0.1f,1.1f);
Keyframe?keyframeScaleX9?=?Keyframe.ofFloat(0.9f,1.1f);
Keyframe?keyframeScaleX10?=?Keyframe.ofFloat(1f,1f);
????PropertyValuesHolder?propertyValuesHolderScaleX?=?PropertyValuesHolder.ofKeyframe("ScaleX",
????????????keyframeScaleX0,keyframeScaleX1,keyframeScaleX9,keyframeScaleX10);
//縮放效果?Y
Keyframe?keyframeScaleY0?=?Keyframe.ofFloat(0,1f);
Keyframe?keyframeScaleY1?=?Keyframe.ofFloat(0.1f,1.1f);
Keyframe?keyframeScaleY9?=?Keyframe.ofFloat(0.9f,1.1f);
Keyframe?keyframeScaleY10?=?Keyframe.ofFloat(1f,1f);
PropertyValuesHolder?propertyValuesHolderScaleY?=?PropertyValuesHolder.ofKeyframe("ScaleY",
????????keyframeScaleY0,keyframeScaleY1,keyframeScaleY9,keyframeScaleY10);
ObjectAnimator?objectAnimator?=?ObjectAnimator.ofPropertyValuesHolder(mIvPhone,
????????propertyValuesHolder,propertyValuesHolderScaleX,propertyValuesHolderScaleY);
objectAnimator.setDuration(1000);
objectAnimator.start();
DEMO地址:https://github.com/chaozhouzhang/CustomProgressView/blob/master/CustomProgressView/app/src/main/java/androidstack/progress/animation/property/advanced/PhoneActivity.java
5、布局動畫 LayoutTransition
/**?* LayoutTransition.APPEARING;新元素在容器中出現(xiàn)時的動畫;
?* LayoutTransition.CHANGE_APPEARING;容器中顯示新元素,其他元素需要變化時的動畫;
?* LayoutTransition.CHANGE_DISAPPEARING;容器中移除舊元素,其他元素需要變化時的動畫;
?*?LayoutTransition.CHANGING;
?* LayoutTransition.DISAPPEARING;舊元素在容器中移除時的動畫;
?*/
實現(xiàn)移除布局內(nèi)控件的動畫:
/**?*?設置移除容器內(nèi)圖片控件時候的動畫
?*/
LayoutTransition?layoutTransition?=?new?LayoutTransition();
ObjectAnimator?objectAnimator?=?ObjectAnimator.ofFloat(null,?"rotation",?0f,?90f,?0f);
layoutTransition.setAnimator(LayoutTransition.DISAPPEARING,?objectAnimator);
/**
?*?設置動畫間的間隔
?*/
layoutTransition.setStagger(LayoutTransition.DISAPPEARING,?1000);
mClLayoutTransition.setLayoutTransition(layoutTransition);
/**
?*?移除布局
?*/
mClLayoutTransition.removeView(mIvTest);
DEMO地址:https://github.com/chaozhouzhang/CustomProgressView/blob/master/CustomProgressView/app/src/main/java/androidstack/progress/animation/property/advanced/ViewGroupAnimateActivity.java
6、估值器 Evaluator
估值器,根據(jù)插值器的數(shù)值進度變化規(guī)律,計算出具體的動畫數(shù)值。自定義估值器,實現(xiàn)類型估值器TypeEvaluator,并傳入泛型的具體類型。
擴展:
自定義插值器,實現(xiàn)時間插值器TimeInterpolator后,實現(xiàn)獲取插值Interpolation的方法,也可以直接實現(xiàn)插值器Interpolator,因為Interpolator繼承的是時間插值器TimeInterpolator。
自定義估值器:
public?class?ReverseEvaluator?implements?TypeEvaluator?{????/**
?????*?@param?fraction???插值器中的進度返回值
?????*?@param?startValue?動畫數(shù)值區(qū)間的起始數(shù)值
?????*?@param?endValue???動畫數(shù)值區(qū)間的結(jié)束數(shù)值
?????*?@return?估值器返回給監(jiān)聽器的具體動畫數(shù)值
?????*/
????@Override
????public?Integer?evaluate(float?fraction,?Integer?startValue,?Integer?endValue)?{return?(int)?(endValue?-?fraction?*?(endValue?-?startValue));
????}
}
DEMO地址:https://github.com/chaozhouzhang/CustomProgressView/blob/master/CustomProgressView/app/src/main/java/androidstack/progress/animation/property/evaluator/ReverseEvaluator.java
歡迎關(guān)注微信公眾號,Android技術(shù)堆棧:
總結(jié)
以上是生活随笔為你收集整理的android 自定义view如何控制view的高度_Android自定义View属性动画的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机械秒表的使用方法_瓦楞纸箱防水性能检测
- 下一篇: ansys fluent udf man