Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to blur background image using BitMap?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 388
    Comment on it

     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-

    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:tools="http://schemas.android.com/tools"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. android:orientation="vertical"
    6.  
    7. android:id="@+id/relative"
    8. tools:context="com.example.androidimageview.MainActivity">
    9.  
    10.  
    11. <Button
    12. android:id="@+id/btn"
    13. android:text="Click"
    14. android:gravity="center"
    15. android:layout_width="wrap_content"
    16. android:layout_height="wrap_content" />
    17. <ImageView
    18.  
    19. android:id="@+id/image"
    20. android:layout_width="fill_parent"
    21. android:layout_height="fill_parent"/>
    22.  
    23. </RelativeLayout>

     

    Step(2)-MainActivity-

    1. public class MainActivity extends ActionBarActivity {
    2. ImageView imageView;
    3. Button button;
    4. Bitmap bitmapOriginal;
    5. View view;
    6.  
    7. RelativeLayout relativeLayout;
    8. @Override
    9. protected void onCreate(Bundle savedInstanceState) {
    10. super.onCreate(savedInstanceState);
    11. setContentView(R.layout.activity_main);
    12. imageView=(ImageView)findViewById(R.id.image);
    13. button=(Button)findViewById(R.id.btn);
    14. relativeLayout=(RelativeLayout)findViewById(R.id.relative);
    15. bitmapOriginal= BitmapFactory.decodeResource(getResources(),R.drawable.img);
    16. imageView.setBackground(getDrawable(R.drawable.one));
    17.  
    18.  
    19. button.setOnClickListener(new Button.OnClickListener() {
    20. @Override
    21. public void onClick(View v) {
    22.  
    23. BitmapDrawable ob = new BitmapDrawable(getResources(), blur(bitmapOriginal));
    24. imageView.setBackground(ob);
    25. imageView.setImageDrawable(ob);
    26. }
    27. });
    28.  
    29.  
    30. }
    31. //image effect
    32. private static final float BITMAP_SCALE = 0.4f;
    33. private static final float BLUR_RADIUS = 7.5f;
    34.  
    35. public Bitmap blur(Bitmap image) {
    36.  
    37. int width = Math.round(image.getWidth() * BITMAP_SCALE);
    38. int height = Math.round(image.getHeight() * BITMAP_SCALE);
    39.  
    40. Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    41. Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
    42.  
    43. //using RenderScript class
    44. RenderScript rs = RenderScript.create(this);
    45. ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    46. Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    47. Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
    48. theIntrinsic.setRadius(BLUR_RADIUS);
    49. theIntrinsic.setInput(tmpIn);
    50. theIntrinsic.forEach(tmpOut);
    51. tmpOut.copyTo(outputBitmap);
    52.  
    53. return outputBitmap;
    54.  
    55. }
    56. }
    57.  
    58.  

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: