Android動(dòng)畫(huà)特效之自定義view:
http://www.reibang.com/p/a74704da0db3
由于上期Android動(dòng)畫(huà)特效之自定義View中,通過(guò)例子可以看出,如何自定義View以及使用步驟蚜锨,這是重中之重验游,為后期一方面實(shí)現(xiàn)Animator動(dòng)畫(huà)效果鋪墊作用,另一方面對(duì)Android自定義View以及View繪制基本流程具備基本技能哆窿。
實(shí)現(xiàn)Animator屬性動(dòng)畫(huà)叭喜,通過(guò)這次例子,先看下效果皂贩。
上面可以看出,實(shí)現(xiàn)了三種動(dòng)畫(huà)效果昆汹,出現(xiàn)時(shí)的動(dòng)畫(huà)效果、消失時(shí)的動(dòng)畫(huà)效果婴栽、平移時(shí)動(dòng)畫(huà)效果满粗。接下來(lái)看看下如何實(shí)現(xiàn)呢。
1.layout_base_view.xml布局文件增加三個(gè)按鈕控件愚争。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/appearAniamtor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="appearAniamtor"
android:textAllCaps="false"
/>
<Button
android:id="@+id/disAppearAniamtor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="disAppearAniamtor"
android:layout_below="@+id/appearAniamtor"
android:textAllCaps="false"
/>
<Button
android:id="@+id/translationAniamtor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="translationAniamtor"
android:layout_below="@+id/disAppearAniamtor"
android:textAllCaps="false"
/>
</RelativeLayout>
2.BaseLayoutView.java
// 因?yàn)槲覀兊牟季植捎肦elativeLayout映皆,所以這里繼承RelativeLayout
public class BaseLayoutView extends RelativeLayout {
private static final String TAG = "BaseLayoutView";
private float centerX = 0f;
private float centerY = 0f;
private Button appearAniamtor,disAppearAniamtor,translationAniamtor;
public BaseLayoutView(Context context) {
this(context, null);
}
public BaseLayoutView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public BaseLayoutView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
// 初始化UI,可根據(jù)業(yè)務(wù)需求設(shè)置默認(rèn)值
private void initView(Context context) {
View view = LayoutInflater.from(context).inflate(R
.layout.layout_base_view, null, false);
addView(view);
centerX = Constants.SCREEN_WIDTH / Constants.FLOAT_TWO;
centerY = Constants.SCREEN_HEIGHT / Constants.FLOAT_TWO - ScreenDensityUtil.getStatusBarHeight();
LogUtil.info(TAG, "initView centerX= " + centerX + " centerY= " + centerY);
LogUtil.info(TAG, "initView getStatusBarHeight= " + ScreenDensityUtil.getStatusBarHeight());
appearAniamtor = view.findViewById(R.id.appearAniamtor);
disAppearAniamtor = view.findViewById(R.id.disAppearAniamtor);
translationAniamtor = view.findViewById(R.id.translationAniamtor);
}
// 返回中心X坐標(biāo)
public float getCenterX() {
return centerX;
}
// 返回中心Y坐標(biāo)
public float getCenterY() {
return centerY;
}
public Button getAppearAniamtor() {
return appearAniamtor;
}
public Button getDisAppearAniamtor() {
return disAppearAniamtor;
}
public Button getTranslationAniamtor() {
return translationAniamtor;
}
/**
* 在父布局需要顯示多大就顯示多大轰枝,也就是自身的BaseLayoutView
* 在子視圖捅彻,也就是CenterView或者其他自定義的View
* 在確定CenterView或者其他自定義的View在父布局BaseLayoutView的坐標(biāo)位置。
* 子視圖CenterView類或者其他自定義的View類里面不做自身測(cè)量鞍陨,
* 也就是CenterView類或者其他自定義的View類的onMeasure方法步淹。
*
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int measureWidth = measureWidth(widthMeasureSpec);
int measureHeight = measureHeight(heightMeasureSpec);
setMeasuredDimension(measureWidth, measureHeight);
}
private int measureWidth(int measureSpec) {
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
// 設(shè)置一個(gè)默認(rèn)值,就是這個(gè)View的默認(rèn)寬度為xxx,
int result = Constants.CENTER_LAYOUT_PIX_SIZE;
if (specMode == MeasureSpec.AT_MOST) { // 相當(dāng)于我們?cè)O(shè)置成wrap_content
result = specSize;
} else if (specMode == MeasureSpec.EXACTLY) { // 相當(dāng)于我們?cè)O(shè)置成match_content或者一個(gè)具體的值
result = specSize;
}
return result;
}
private int measureHeight(int measureSpec) {
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
// 設(shè)置一個(gè)默認(rèn)值诚撵,就是這個(gè)View的默認(rèn)寬度為xxx,
int result = Constants.CENTER_LAYOUT_PIX_SIZE;
if (specMode == MeasureSpec.AT_MOST) { // 相當(dāng)于我們?cè)O(shè)置成wrap_content
result = specSize;
} else if (specMode == MeasureSpec.EXACTLY) { // 相當(dāng)于我們?cè)O(shè)置成match_content或者一個(gè)具體的值
result = specSize;
}
return result;
}
}
該類做了如下兩件事:
首先缭裆,初始化三個(gè)Button控件,并提供公有方法提供外部調(diào)用寿烟,拿到三個(gè)Button實(shí)例澈驼。
其次是onMeasure方法。
上面的注釋寫(xiě)得很清楚筛武,BaseLayoutView是基礎(chǔ)視圖缝其,需要占滿整個(gè)屏幕,因此需要通過(guò)onMeasure方法進(jìn)行測(cè)量徘六,通過(guò)setMeasuredDimension方法保存測(cè)量數(shù)據(jù)内边,告訴父View我需要占滿整個(gè)屏幕。這樣CenterView或者其他自定義的View在BaseLayoutView視圖里面可以確定顯示坐標(biāo)位置硕噩,這樣可以在屏幕的范圍內(nèi)實(shí)現(xiàn)動(dòng)畫(huà)效果了假残。CenterView或者其他自定義的View的onMeasure方法不需要在測(cè)量了。
CenterView.java
// 因?yàn)槲覀兊牟季植捎肦elativeLayout,所以這里繼承RelativeLayout
public class CenterView extends RelativeLayout {
private static final String TAG = "CenterView";
private ImageView ivDeviceView;
private int deviceDpWith;
private int devicePxWith;
private Context context;
public CenterView(Context context) {
this(context, null);
}
public CenterView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CenterView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
// 初始化UI辉懒,可根據(jù)業(yè)務(wù)需求設(shè)置默認(rèn)值
private void initView(Context context) {
this.context = context;
View view = LayoutInflater.from(context).inflate(R
.layout.layout_center_view, null, false);
ivDeviceView = view.findViewById(R.id.ivDeviceView);
devicePxWith = Constants.CENTER_LAYOUT_PIX_SIZE;
// 先添加addView后阳惹,再獲取ivDeviceView.getLayoutParams(),否則會(huì)報(bào)空
refreshing();
addView(view);
}
private void refreshing() {
ivDeviceView.setImageResource(R.mipmap.wifi_icon);
// 拿到的是ivDeviceView父布局的參數(shù)眶俩,也就是RelativeLayout莹汤。
LayoutParams params= (LayoutParams) ivDeviceView.getLayoutParams();
params.width = devicePxWith;
params.height = devicePxWith;
ivDeviceView.setLayoutParams(params);
}
public void setDeviceWith(int deviceWith) {
this.deviceDpWith = deviceWith;
devicePxWith = ScreenDensityUtil.dp2px(context, deviceDpWith);
refreshing();
}
public void setDeivceImageResource(int resId) {
ivDeviceView.setImageResource(resId);
}
}
該類CenterView里面沒(méi)有onMeasure方法了。
3.AppearDeviceAnimator.java
/**
* 插入或出現(xiàn)動(dòng)畫(huà)的View
*/
public class AppearDeviceAnimator extends Animator {
private static final String TAG = "AppearDeviceAnimator";
private long DEVICE_RUNNING_TIME = Constants.DEAULT_500MS;
private static final float START_SCALE = 0.6F;
private static final float MIDDLE_SCALE = 1.2F;
private static final float END_SCALE = 1.0F;
private static final float START_ALPHA = 0F;
private static final float END_ALPHA = 1F;
@Override
public long getStartDelay() {
return 0;
}
@Override
public void setStartDelay(long startDelay) {
}
@Override
public Animator setDuration(long duration) {
DEVICE_RUNNING_TIME = duration;
return this;
}
@Override
public long getDuration() {
return DEVICE_RUNNING_TIME;
}
@Override
public void setInterpolator(TimeInterpolator value) {
}
@Override
public boolean isRunning() {
return false;
}
@Override
public void start() {
super.start();
LogUtil.info(TAG, "執(zhí)行start");
}
@Override
public void setTarget(@Nullable Object target) {
super.setTarget(target);
performMakeUpAppearAnimator(target);
}
private void performMakeUpAppearAnimator(Object target) {
LogUtil.info(TAG, "執(zhí)行setTarget");
CenterView centerView = (CenterView) target;
PropertyValuesHolder scaleHolderX = PropertyValuesHolder.ofFloat(Constants.SCALE_X, START_SCALE, MIDDLE_SCALE, END_SCALE);
PropertyValuesHolder scaleHolderY = PropertyValuesHolder.ofFloat(Constants.SCALE_Y, START_SCALE, MIDDLE_SCALE, END_SCALE);
PropertyValuesHolder alphaHolder = PropertyValuesHolder.ofFloat(Constants.ALPHA, START_ALPHA, END_ALPHA);
ObjectAnimator scaleAnimator = ObjectAnimator.ofPropertyValuesHolder(centerView, scaleHolderX, scaleHolderY);
ObjectAnimator alphaAnimator = ObjectAnimator.ofPropertyValuesHolder(centerView, alphaHolder);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(scaleAnimator).with(alphaAnimator);
animatorSet.setDuration(DEVICE_RUNNING_TIME);
animatorSet.start();
}
}
AppearDeviceAnimator類繼承自Animator颠印,PropertyValuesHolder纲岭、ObjectAnimator類實(shí)現(xiàn)縮放、透明度動(dòng)畫(huà)效果线罕,縮放過(guò)程由0.6放大至1.2止潮,其次縮小至1,透明度由0到1钞楼,實(shí)現(xiàn)組合的View出現(xiàn)動(dòng)畫(huà)喇闸。
4.DisAppearDeviceAnimator.java
/**
* 消失的動(dòng)畫(huà)
*/
public class DisAppearDeviceAnimator extends Animator {
private static final String TAG = "DisAppearDeviceAnimator";
private long DEVICE_RUNNING_TIME = Constants.DEAULT_500MS;
private static final float START_SCALE = 1.0F;
private static final float END_SCALE = 0.5F;
private static final float START_ALPHA = 1F;
private static final float END_ALPHA = 0F;
@Override
public long getStartDelay() {
return 0;
}
@Override
public void setStartDelay(long startDelay) {
}
@Override
public Animator setDuration(long duration) {
DEVICE_RUNNING_TIME = duration;
return this;
}
@Override
public long getDuration() {
return 0;
}
@Override
public void setInterpolator(TimeInterpolator value) {
}
@Override
public boolean isRunning() {
return false;
}
@Override
public void start() {
super.start();
LogUtil.info(TAG, "執(zhí)行start");
}
@Override
public void setTarget(@Nullable Object target) {
super.setTarget(target);
LogUtil.info(TAG, "setTarget");
performMakeUpDisAppearAnimator(target);
}
private void performMakeUpDisAppearAnimator(Object target) {
LogUtil.info(TAG, "執(zhí)行setTarget");
CenterView centerView = (CenterView) target;
PropertyValuesHolder scaleHolderX = PropertyValuesHolder.ofFloat(Constants.SCALE_X, START_SCALE, END_SCALE);
PropertyValuesHolder scaleHolderY = PropertyValuesHolder.ofFloat(Constants.SCALE_Y, START_SCALE, END_SCALE);
PropertyValuesHolder alphaHolder = PropertyValuesHolder.ofFloat(Constants.ALPHA, START_ALPHA, END_ALPHA);
ObjectAnimator scaleAnimator = ObjectAnimator.ofPropertyValuesHolder(centerView, scaleHolderX, scaleHolderY);
ObjectAnimator alphaAnimator = ObjectAnimator.ofPropertyValuesHolder(centerView, alphaHolder);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(scaleAnimator).with(alphaAnimator);
animatorSet.setDuration(DEVICE_RUNNING_TIME);
animatorSet.start();
}
}
DisAppearDeviceAnimator類繼承自Animator,PropertyValuesHolder询件、ObjectAnimator類實(shí)現(xiàn)縮放燃乍、透明度動(dòng)畫(huà)效果,縮放過(guò)程由1縮小至至0.5宛琅,透明度由1到0刻蟹,實(shí)現(xiàn)組合的View消失動(dòng)畫(huà)。
5.TranslationAnimator.java
public class TranslationAnimator {
private static final String TAG = "TranslationAnimator";
private static ObjectAnimator objectAnimator;
public static void changePositonAnimator(CenterView centerView, float dstX, float dstY) {
PropertyValuesHolder holderX = PropertyValuesHolder.ofFloat(Constants.TRANSLATION_X, dstX);
PropertyValuesHolder holderY = PropertyValuesHolder.ofFloat(Constants.TRANSLATION_Y, dstY);
objectAnimator = ObjectAnimator.ofPropertyValuesHolder(centerView, holderX, holderY);
objectAnimator.setDuration(2000);
objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
LogUtil.info(TAG, "onAnimationUpdate = " + animation.getAnimatedValue());
}
});
objectAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
LogUtil.info(TAG, "onAnimationStart X= " + centerView
.getX() + "Y= " + centerView.getY());
}
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
LogUtil.info(TAG, "onAnimationEnd X= " + centerView
.getX() + "Y= " + centerView.getY());
}
});
objectAnimator.start();
}
}
TranslationAnimator類的方法執(zhí)行平移的動(dòng)畫(huà)效果嘿辟,使用ObjectAnimator類以及ObjectAnimator對(duì)象的addUpdateListener舆瘪、addListener事件打印更新的值。
6.SolarControl.java
public class SolarControl {
private static final String TAG = "SolarControl";
private ViewGroup parentView;
private BaseLayoutView baseView;
private CenterView centerView;
public SolarControl(ViewGroup parent) {
parentView = parent;
baseView = new BaseLayoutView(parentView.getContext());
initView();
initOnClickEvent();
}
private void initView() {
centerView = new CenterView(baseView.getContext());
float centerX = baseView.getCenterX() - Constants.CENTER_LAYOUT_PIX_SIZE / Constants.FLOAT_TWO;
float centerY = baseView.getCenterY() - Constants.CENTER_LAYOUT_PIX_SIZE / Constants.FLOAT_TWO;
centerView.setX(centerX);
centerView.setY(centerY);
LogUtil.info(TAG, "initView centerX= " + centerX + " centerY= " + centerY);
baseView.addView(centerView);
parentView.addView(baseView);
}
public void release() {
if (baseView != null) {
baseView.removeView(centerView);
parentView.removeView(baseView);
baseView = null;
parentView = null;
}
if (centerView != null) {
centerView = null;
}
}
private void initOnClickEvent() {
baseView.getAppearAniamtor().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AppearDeviceAnimator appearDeviceAnimator = new AppearDeviceAnimator();
appearDeviceAnimator.setTarget(centerView);
}
});
baseView.getDisAppearAniamtor().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DisAppearDeviceAnimator disAppearDeviceAnimator = new DisAppearDeviceAnimator();
disAppearDeviceAnimator.setTarget(centerView);
}
});
baseView.getTranslationAniamtor().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TranslationAnimator.changePositonAnimator(centerView, centerView.getX() / Constants.FLOAT_TWO, centerView.getY());
}
});
}
}
SolarControl類里面主要拿到三個(gè)Button實(shí)例仓洼,在不同按鈕監(jiān)聽(tīng)事件實(shí)行不同的動(dòng)畫(huà)邏輯介陶。運(yùn)行此項(xiàng)目,看到處于文章開(kāi)頭的界面動(dòng)畫(huà)效果色建。
通過(guò)例子可以看出哺呜,實(shí)現(xiàn)Animator動(dòng)畫(huà)效果并不復(fù)雜,實(shí)現(xiàn)起來(lái)比較簡(jiǎn)單箕戳,主要是如何使用ObjectAnimator類某残、PropertyValuesHolder類來(lái)實(shí)現(xiàn)簡(jiǎn)單的動(dòng)畫(huà)效果,但實(shí)現(xiàn)Animator動(dòng)畫(huà)效果并離不開(kāi)Android自定義View陵吸,因此主要掌握如何編寫(xiě)復(fù)雜的自定義View類玻墅,配合Animator類來(lái)完成動(dòng)畫(huà)效果。小伙伴們有興趣的話壮虫,搜索并關(guān)注公眾號(hào)“Android技術(shù)迷”關(guān)注后可閱讀更多文章澳厢,感謝各位關(guān)注环础。
Android動(dòng)畫(huà)特效之自定義view:
http://www.reibang.com/p/a74704da0db3