In the below example code I have created blur background image function. In activity_main.xml layout I have added a Button and ImageView.
Now see programming area, here I have used Bitmap class. When a user clicks on button background image will get blur easily without any delay in time.
See below code I have Completely described "to blur background image with using BitMap".
Step(1)-activity_main.xml-
<RelativeLayout 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:orientation="vertical"
android:id="@+id/relative"
tools:context="com.example.androidimageview.MainActivity">
<Button
android:id="@+id/btn"
android:text="Click"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
Step(2)-MainActivity-
public class MainActivity extends ActionBarActivity {
ImageView imageView;
Button button;
Bitmap bitmapOriginal;
View view;
RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView)findViewById(R.id.image);
button=(Button)findViewById(R.id.btn);
relativeLayout=(RelativeLayout)findViewById(R.id.relative);
bitmapOriginal= BitmapFactory.decodeResource(getResources(),R.drawable.img);
imageView.setBackground(getDrawable(R.drawable.one));
button.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
BitmapDrawable ob = new BitmapDrawable(getResources(), blur(bitmapOriginal));
imageView.setBackground(ob);
imageView.setImageDrawable(ob);
}
});
}
//image effect
private static final float BITMAP_SCALE = 0.4f;
private static final float BLUR_RADIUS = 7.5f;
public Bitmap blur(Bitmap image) {
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);
//using RenderScript class
RenderScript rs = RenderScript.create(this);
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
theIntrinsic.setRadius(BLUR_RADIUS);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
}
0 Comment(s)