Android利用動(dòng)畫實(shí)現(xiàn)背景逐漸變暗
前言
之前寫了一篇Android-實(shí)現(xiàn)底部彈出PopupWindow并讓背景逐漸變暗,介紹利用Handler動(dòng)態(tài)改變背景透明度從而達(dá)到變暗的效果。現(xiàn)在補(bǔ)充一種方法,使用動(dòng)畫來(lái)實(shí)現(xiàn)相同的效果。
ValueAnimator 和 Interpolator
今天的主角就是這倆,關(guān)于ValueAnimator和Interpolator(插值器)的概念請(qǐng)各位自行補(bǔ)充,這里主要講述怎么用到我們這里來(lái)(因?yàn)槲乙膊缓芏?捂臉))。
效果
跟之前沒有太大區(qū)別,只是為了演示變暗、變亮的過程 ↓

代碼
AnimUtil.java
/**
* 動(dòng)畫工具類
* UpdateListener: 動(dòng)畫過程中通過添加此監(jiān)聽來(lái)回調(diào)數(shù)據(jù)
* EndListener: 動(dòng)畫結(jié)束的時(shí)候通過此監(jiān)聽器來(lái)做一些處理
*/
public class AnimUtil {
private ValueAnimator valueAnimator;
private UpdateListener updateListener;
private EndListener endListener;
private long duration;
private float start;
private float end;
private Interpolator interpolator = new LinearInterpolator();
public AnimUtil() {
duration = 1000; //默認(rèn)動(dòng)畫時(shí)常1s
start = 0.0f;
end = 1.0f;
interpolator = new LinearInterpolator();// 勻速的插值器
}
public void setDuration(int timeLength) {
duration = timeLength;
}
public void setValueAnimator(float start, float end, long duration) {
this.start = start;
this.end = end;
this.duration = duration;
}
public void setInterpolator(Interpolator interpolator) {
this.interpolator = interpolator;
}
public void startAnimator() {
if (valueAnimator != null){
valueAnimator = null;
}
valueAnimator = ValueAnimator.ofFloat(start, end);
valueAnimator.setDuration(duration);
valueAnimator.setInterpolator(interpolator);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
if (updateListener == null) {
return;
}
float cur = (float) valueAnimator.getAnimatedValue();
updateListener.progress(cur);
}
});
valueAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {}
@Override
public void onAnimationEnd(Animator animator) {
if(endListener == null){
return;
}
endListener.endUpdate(animator);
}
@Override
public void onAnimationCancel(Animator animator) {}
@Override
public void onAnimationRepeat(Animator animator) {}
});
valueAnimator.start();
}
public void addUpdateListener(UpdateListener updateListener) {
this.updateListener = updateListener;
}
public void addEndListner(EndListener endListener){
this.endListener = endListener;
}
public interface EndListener {
void endUpdate(Animator animator);
}
public interface UpdateListener {
void progress(float progress);
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private AnimUtil animUtil;
private float bgAlpha = 1f;
private boolean bright = false;
PopupWindow popupWindow;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
animUtil = new AnimUtil();
button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bottomwindow(button);
toggleBright();
}
});
}
private void toggleBright() {
//三個(gè)參數(shù)分別為: 起始值 結(jié)束值 時(shí)長(zhǎng) 那么整個(gè)動(dòng)畫回調(diào)過來(lái)的值就是從0.5f--1f的
animUtil.setValueAnimator(0.5f, 1f, 350);
animUtil.addUpdateListener(new AnimUtil.UpdateListener() {
@Override
public void progress(float progress) {
//此處系統(tǒng)會(huì)根據(jù)上述三個(gè)值,計(jì)算每次回調(diào)的值是多少,我們根據(jù)這個(gè)值來(lái)改變透明度
bgAlpha = bright ? progress : (1.5f - progress);//三目運(yùn)算,應(yīng)該挺好懂的。
backgroundAlpha(bgAlpha);//在此處改變背景,這樣就不用通過Handler去刷新了。
}
});
animUtil.addEndListner(new AnimUtil.EndListener() {
@Override
public void endUpdate(Animator animator) {
//在一次動(dòng)畫結(jié)束的時(shí)候,翻轉(zhuǎn)狀態(tài)
bright = !bright;
}
});
animUtil.startAnimator();
}
/***
* 此方法用于改變背景的透明度,從而達(dá)到“變暗”的效果
*/
private void backgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
getWindow().setAttributes(lp);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
void bottomwindow(View view) {
if (popupWindow != null && popupWindow.isShowing()) {
return;
}
LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.window_popup, null);
popupWindow = new PopupWindow(layout,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//點(diǎn)擊空白處時(shí),隱藏掉pop窗口
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setAnimationStyle(R.style.Popupwindow);
int[] location = new int[2];
view.getLocationOnScreen(location);
popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.BOTTOM, 0, -location[1]);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
toggleBright();
}
});
}
}
代碼很簡(jiǎn)單,注釋都寫了。
小結(jié):
對(duì)比之前的那種用Handler的方法寫的,這種感覺代碼更簡(jiǎn)潔,更容易在多處使用,也算是填坑吧,哈哈。 如果本文埋下了另一個(gè)坑,請(qǐng)大家指正,謝謝!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android App開發(fā)中RecyclerView控件的基本使用教程
這篇文章主要介紹了Android App開發(fā)中RecyclerView控件的基本使用教程,RecyclerView在Android 5.0之后伴隨著Material Design出現(xiàn),管理布局方面十分強(qiáng)大,需要的朋友可以參考下2016-04-04
Android編程之SQLite數(shù)據(jù)庫(kù)操作方法詳解
這篇文章主要介紹了Android編程之SQLite數(shù)據(jù)庫(kù)操作方法,簡(jiǎn)單介紹了SQLite數(shù)據(jù)庫(kù)及Android操作SQLite數(shù)據(jù)庫(kù)的步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-08-08
Android網(wǎng)絡(luò)訪問之Retrofit使用教程
Retrofit?是一個(gè)?RESTful?的?HTTP?網(wǎng)絡(luò)請(qǐng)求框架的封裝,網(wǎng)絡(luò)請(qǐng)求的工作本質(zhì)上是?OkHttp?完成,而?Retrofit?僅負(fù)責(zé)?網(wǎng)絡(luò)請(qǐng)求接口的封裝2022-12-12
Android應(yīng)用動(dòng)態(tài)修改主題的方法示例
今天小編就為大家分享一篇關(guān)于Android應(yīng)用動(dòng)態(tài)修改主題的方法示例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
OpenHarmony實(shí)現(xiàn)屏幕亮度動(dòng)態(tài)調(diào)節(jié)方法詳解
大家在拿到dayu之后,都吐槽說(shuō),會(huì)經(jīng)常熄屏,不利于調(diào)試,那么有沒有一種辦法,可以讓app不熄屏呢,答案是有的,今天我們就來(lái)揭秘一下,如何控制屏幕亮度2022-11-11
android 應(yīng)用內(nèi)部懸浮可拖動(dòng)按鈕簡(jiǎn)單實(shí)現(xiàn)代碼
本篇文章主要介紹了android 應(yīng)用內(nèi)部懸浮可拖動(dòng)按鈕簡(jiǎn)單實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-10-10
android實(shí)現(xiàn)音樂播放器進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)音樂播放器進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Android開發(fā)時(shí)盡管已root但是ddms還是沒有data路徑怎么辦
這篇文章主要介紹了Android開發(fā)時(shí)盡管已root但是ddms還是沒有data路徑怎么辦的相關(guān)資料,需要的朋友可以參考下2015-12-12

