/**
* @author Qiushui
* @description 模糊图片工具类
* @revision Xiarui 16.09.05
*/
public class BlurBitmapUtil {
//图片缩放比例
private static final float BITMAP_SCALE = 0.4f;
/**
* 模糊图片的具体方法
*
* @param context 上下文对象
* @param image 需要模糊的图片
* @return 模糊处理后的图片
*/
public static Bitmap blurBitmap(Context context, Bitmap image,float blurRadius) {
// 计算图片缩小后的长宽
int width = Math.round(image.getWidth() * BITMAP_SCALE);
int height = Math.round(image.getHeight() * BITMAP_SCALE);
// 将缩小后的图片做为预渲染的图片
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
// 创建一张渲染后的输出图片
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
// 创建RenderScript内核对象
RenderScript rs = RenderScript.create(context);
// 创建一个模糊效果的RenderScript的工具对象
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
// 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间
// 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
// 设置渲染的模糊程度, 25f是最大模糊度
blurScript.setRadius(blurRadius);
// 设置blurScript对象的输入内存
blurScript.setInput(tmpIn);
// 将输出数据保存到输出内存中
blurScript.forEach(tmpOut);
// 将数据填充到Allocation中
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
}
defaultConfig {
renderscriptTargetApi 19
renderscriptSupportModeEnabled true
}
/**
* 初始化View
*/
@SuppressWarnings("deprecation")
private void initView() {
basicImage = (ImageView) findViewById(R.id.iv_basic_pic);
//拿到初始图
Bitmap initBitmap = BitmapUtil.drawableToBitmap(getResources().getDrawable(R.raw.pic));
//处理得到模糊效果的图
Bitmap blurBitmap = BlurBitmapUtil.blurBitmap(this, initBitmap, 20f);
basicImage.setImageBitmap(blurBitmap);
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/blurredview_blurred_img"
.../>
<ImageView
android:id="@+id/blurredview_origin_img"
.../>
</FrameLayout>
<resources>
<declare-styleable name="BlurredView">
<attr name="src" format="reference"/>
<attr name="disableBlurred" format="boolean"/>
</declare-styleable>
</resources>
/**
* @author Qiushui
* @description 自定义模糊View类
* @revision Xiarui 16.09.05
*/
public class BlurredView extends RelativeLayout {
/*========== 全局相关 ==========*/
private Context mContext;//上下文对象
private static final int ALPHA_MAX_VALUE = 255;//透明最大值
private static final float BLUR_RADIUS = 25f;//最大模糊度(在0.0到25.0之间)
/*========== 图片相关 ==========*/
private ImageView mOriginImg;//原图ImageView
private ImageView mBlurredImg;//模糊后的ImageView
private Bitmap mBlurredBitmap;//模糊后的Bitmap
private Bitmap mOriginBitmap;//原图Bitmap
/*========== 属性相关 ==========*/
private boolean isDisableBlurred;//是否禁用模糊效果
...
/**
* 以代码的方式添加待模糊的图片
*
* @param blurredBitmap 待模糊的图片
*/
public void setBlurredImg(Bitmap blurredBitmap) {
if (null != blurredBitmap) {
mOriginBitmap = blurredBitmap;
mBlurredBitmap = BlurBitmapUtil.blurBitmap(mContext, blurredBitmap, BLUR_RADIUS);
setImageView();
}
}
...
/**
* 填充ImageView
*/
private void setImageView() {
mBlurredImg.setImageBitmap(mBlurredBitmap);
mOriginImg.setImageBitmap(mOriginBitmap);
}
/**
* 设置模糊程度
*
* @param level 模糊程度, 数值在 0~100 之间.
*/
@SuppressWarnings("deprecation")
public void setBlurredLevel(int level) {
//超过模糊级别范围 直接抛异常
if (level < 0 || level > 100) {
throw new IllegalStateException("No validate level, the value must be 0~100");
}
//禁用模糊直接返回
if (isDisableBlurred) {
return;
}
//设置透明度
mOriginImg.setAlpha((int) (ALPHA_MAX_VALUE - level * 2.55));
}
...
}
<com.blurdemo.view.BlurredView
android:id="@+id/bv_custom_blur"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:src="@raw/pic"
app:disableBlurred="false" />
private void initView() {
customBView = (BlurredView) findViewById(R.id.bv_custom_blur);
//设置模糊度
customBView.setBlurredLevel(100);
}
/**
* 初始化View
*/
private void initView() {
customBView = (BlurredView) findViewById(R.id.bv_dynamic_blur);
//设置初始模糊度
initLevel = 100;
customBView.setBlurredLevel(initLevel);
}
/**
* 触摸事件
*/
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
float moveY = ev.getY();
//手指滑动距离
float offsetY = moveY - downY;
//屏幕高度 十倍是为了看出展示效果
int screenY = getWindowManager().getDefaultDisplay().getHeight() * 10;
//手指滑动距离占屏幕的百分比
movePercent = offsetY / screenY;
currentLevel = initLevel + (int) (movePercent * 100);
if (currentLevel < 0) {
currentLevel = 0;
}
if (currentLevel > 100) {
currentLevel = 100;
}
//设置模糊度
customBView.setBlurredLevel(currentLevel);
//更改初始模糊等级
initLevel = currentLevel;
break;
case MotionEvent.ACTION_UP:
break;
}
return super.onTouchEvent(ev);
}
//RecyclerView 滚动监听
mainRView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//滚动距离
mScrollerY += dy;
//根据滚动距离控制模糊程度 滚动距离是模糊程度的十倍
if (Math.abs(mScrollerY) > 1000) {
mAlpha = 100;
} else {
mAlpha = Math.abs(mScrollerY) / 10;
}
//设置透明度等级
recyclerBView.setBlurredLevel(mAlpha);
}
});
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有