import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import com.enrique.stackblur.StackBlurManager;
public class MainActivity extends Activity {
private ImageView iv;
private SeekBar seekBar;
private StackBlurManager stackBlurManager;
private String IMAGE_TO_ANALYZE = "android_platform_256.png";
private int blurMode=0;
private int radius=30;
private int step=2;
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
radius -= step;
if (radius < 0) {
handler.removeCallbacks(runnable);
return;
}
onBlur(radius);
handler.postDelayed(this, 30);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView)findViewById(R.id.imageView);
seekBar=(SeekBar)findViewById(R.id.blur_amount);
stackBlurManager = new StackBlurManager(getBitmapFromAsset(this, "av.png"));
onBlur(radius);
handler.postDelayed(runnable, 1000);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int radius = progress;
onBlur(radius);
}
});
}
private Bitmap getBitmapFromAsset(Context context, String strName) {
AssetManager assetManager = context.getAssets();
InputStream is=null;
Bitmap bitmap = null;
try {
is = assetManager.open(strName);
bitmap = BitmapFactory.decodeStream(is);
} catch (IOException e) {
return null;
}
return bitmap;
}
public void setBlurMode(int mode) {
this.blurMode = mode;
onBlur(radius);
}
private void onBlur(int radius) {
switch(blurMode) {
case 0:
iv.setImageBitmap( stackBlurManager.process(radius) );
break;
case 1:
iv.setImageBitmap( stackBlurManager.processNatively(radius) );
break;
case 2:
iv.setImageBitmap( stackBlurManager.processRenderScript(this, radius) );
break;
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:gravity="center_horizontal"
android:orientation="vertical" >
<SeekBar
android:id="@+id/blur_amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="50" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
https://github.com/qiujuer/ImageBlurring
Medium上采用了的一种新的图片load方式,先显示一张虚化图,再显示完整的图片BlurImageView
http://www.jcodecraeer.com/a/opensource/2015/1112/3681.html
- android-stackblur-master.zip (7.9 MB)
- 下载次数: 227
- 大小: 136.5 KB
- 大小: 187.1 KB
- 查看图片附件