Dali
Dali 是一個(gè)Android端提供圖像模糊的開(kāi)源庫(kù),內(nèi)部是使用RenderScript實(shí)現(xiàn),內(nèi)置了多種圖像過(guò)濾器,而且很容易擴(kuò)展。
- 安裝使用
compile 'at.favre.lib:dali:0.3.4'
還需要在build.gradle中添加下面的內(nèi)容,使RenderScript工作
android {
...
defaultConfig {
...
renderscriptTargetApi 20
renderscriptSupportModeEnabled true
}
}
- 靜態(tài)圖片模糊(背景圖片等)
Dali.create(context).load(R.drawable.test_img1).blurRadius(12).into(imageView);
Dali還提供了一些圖片處理的算法,如(亮度調(diào)節(jié)、對(duì)比度、顏色等)
Dali.create(context).load(R.drawable.test_img1).placeholder(R.drawable.test_img1).blurRadius(12)
.downScale(2).colorFilter(Color.parseColor("#ffccdceb")).concurrent().reScale().into(iv3)
- 一切view都可以模糊
使用Dali,所有的view都可以模糊并且在imageview中顯示。
Dali.create(context).load(rootView.findViewById(R.id.blurTemplateView)).blurRadius(20)
.downScale(2).concurrent().reScale().skipCache().into(imageView);
- 跳過(guò)模糊
如果只想使用Dali的其他功能,兒不想使用模糊效果,可以使用下面的代碼
Dali.create(context).load(R.drawable.test_img1).algorithm(EBlurAlgorithm.NONE).brightness(70).concurrent().into(iv);
- 動(dòng)態(tài)圖像模糊
這個(gè)特性可以在 ViewPager Scrollview RecyclerView等view中使用
blurWorker = Dali.create(getActivity()).liveBlur(rootViewPagerWrapperView,topBlurView,bottomBlurView).downScale(8).assemble(true);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
blurWorker.updateBlurView();
}
@Override public void onPageSelected(int position) {}
@Override public void onPageScrollStateChanged(int state) {}
});

抽屜導(dǎo)航的背景模糊
Dali可以結(jié)合DrawerLayout使用,模糊導(dǎo)航控件的背景
protected void onCreate(Bundle savedInstanceState) {
...
mDrawerToggle = new DaliBlurDrawerToggle(this, mDrawerLayout,
toolbar, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
// Set the drawer toggle as the DrawerListener
mDrawerLayout.addDrawerListener(mDrawerToggle);
...
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
return super.onPrepareOptionsMenu(menu);
}

模糊的過(guò)場(chǎng)動(dòng)畫(huà)
在使用Dali的過(guò)程中,在使圖像模糊的過(guò)程中可以添加對(duì)應(yīng)的動(dòng)畫(huà)
BlurKeyFrameManager man = new BlurKeyFrameManager();
man.addKeyFrame(new BlurKeyFrame(2,4,0,300));
man.addKeyFrame(new BlurKeyFrame(2,8,0,300));
