Android Example RoundBitmapWithBorder - inline java code

Discussion in 'Tutorials & Examples' started by Johan Schoeman, Mar 15, 2015.

  1. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Attached project draws a circular bitmap within an imageview including an adjustable border (colour and width) around the image. Making use of inline java code. Only JavaObject V2.01 required.

    my2doggies.png
     

    Attached Files:

    Last edited: Aug 2, 2015
  2. imbault

    imbault Well-Known Member Licensed User

    Hi @Johan Schoeman, how is it possible to add just a rectangular bitmap ajustable border, please?

    Thanks

    Patrick
     
  3. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Try the attached project:


    my2doggies2.png

    Sample Code:

    Code:
    #Region  Project Attributes
        
    #ApplicationLabel: RoundBitmapWithBorder
        
    #VersionCode: 1
        
    #VersionName:
        
    'SupportedOrientations possible values: unspecified, landscape or portrait.
        #SupportedOrientations: landscape
        
    #CanInstallToExternalStorage: False
    #End Region

    #Region  Activity Attributes
        
    #FullScreen: False
        
    #IncludeTitle: True
    #End Region

    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.
        Dim nativeMe As JavaObject
       
    End Sub

    Sub Globals
        
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.
       
        
    Dim bm, bm1, bm2 As Bitmap


        
    Private ImageView1 As ImageView
        
    Private ImageView2 As ImageView
       
    End Sub

    Sub Activity_Create(FirstTime As Boolean)

        
    Activity.LoadLayout("main")
        nativeMe.InitializeContext
       
        
    Dim borderWidth1 As Int = 50
        
    Dim borderColor1 As Int = Colors.White
        ImageView1.Bitmap = 
    Null
        bm.Initialize(
    File.DirAssets,"pluto.jpg")   
        bm1 = nativeMe.RunMethod(
    "getRoundBitmap",Array(bm,borderColor1, borderWidth1))  
        ImageView1.Bitmap = bm1
       
    '    Dim borderWidth2 As Int = 50
    '    Dim borderColor2 As Int = Colors.White
    '    ImageView2.Bitmap = Null
    '    bm.Initialize(File.DirAssets,"stitch.jpg")   
    '    borderColor2 = Colors.ARGB(255,200,0,0)
    '    bm2 = nativeMe.RunMethod("getRoundBitmap",Array(bm,borderColor2, borderWidth2))  
    '    ImageView2.Bitmap = bm2       


        
    Dim borderWidth2 As Int = 15
        
    Dim borderColor2 As Int = Colors.Magenta
        ImageView2.Bitmap = 
    Null
        bm.Initialize(
    File.DirAssets,"stitch.jpg")   
        borderColor2 = 
    Colors.ARGB(255,200,0,0)
        bm2 = nativeMe.RunMethod(
    "addSquareBorder",Array(bm, borderWidth2, borderColor2))  
        ImageView2.Bitmap = bm2       
       

    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub

    #If Java

    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Path;
    import android.graphics.Rect;
    import android.graphics.RectF;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.graphics.Path.FillType;
    import android.graphics.Paint;
    import android.graphics.Color;
    import android.view.View;

    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.graphics.BitmapShader;
    import android.graphics.Shader;
    import android.graphics.PorterDuffXfermode;
    import android.graphics.PorterDuff.Mode;


    public static Bitmap getRoundBitmap(Bitmap scaleBitmapImage, int borderColor, int borderWidth) {
        int targetWidth = 1000;
        int targetHeight = 1000;
        int radius = Math.min((targetHeight - 5)/2, (targetWidth - 5)/2);  
       
        Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
                Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(targetBitmap);
        Path path = new Path();
        path.addCircle(((float) targetWidth - 1) / 2,
                ((float) targetHeight - 1) / 2,
                (Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
                Path.Direction.CCW);

        canvas.clipPath(path);
       
        Bitmap sourceBitmap = scaleBitmapImage;
       
        canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),
                sourceBitmap.getHeight()), new Rect(0, 0, targetWidth,
                targetHeight), null);
               
               
        Paint p = new Paint();                                             
        p.setAntiAlias(true);                  
        //    canvas.drawBitmap(sourceBitmap, 4, 4, p);                                     
        p.setXfermode(null);                                               
        p.setStyle(Paint.Style.STROKE);                                          
        p.setColor(borderColor);                                           
        p.setStrokeWidth(borderWidth);                                               
        canvas.drawCircle((targetWidth / 2) , (targetHeight / 2) , radius, p);                             

        return targetBitmap;
    }   
       
        public static Bitmap addSquareBorder(Bitmap bmp, int borderSize, int bordercolor) {
               
            Bitmap bmpWithBorder = Bitmap.createScaledBitmap(bmp, bmp.getWidth() + borderSize * 2, bmp.getHeight() + borderSize * 2, false);           
               
            Canvas canvas = new Canvas(bmpWithBorder);
            canvas.drawColor(bordercolor);
            canvas.drawBitmap(bmp, borderSize, borderSize, null);
            return bmpWithBorder;
        }   
       
       


    #End If
     

    Attached Files:

    Indy, imbault, Erel and 1 other person like this.
  4. imbault

    imbault Well-Known Member Licensed User

    That is just perfect Johan.

    Thanks a lot
     
    Johan Schoeman likes this.
  5. Indy

    Indy Active Member Licensed User

    Hi

    How can I incorporate this into my project? I tried to copy and paste but get an error about Java when compiling. I have included the JavaObject library.

    Thanks
     
  6. Indy

    Indy Active Member Licensed User

    JavaObject - 2.02
    B4X - 5.50
     
  7. Indy

    Indy Active Member Licensed User

    This is where I differ slightly from your project. I'm not loading the image, instead I'm passing a bitmap to a function in which I have added your code.

    Code:
    iv.Bitmap = AddBorder(LoadBitmapSample(Main.Imagespath, filename, 320200))
    Code:
    Sub AddBorder(bm As BitmapAs Bitmap
       
    Dim bm2 As Bitmap
       
    Dim borderWidth2 As Int = 15
       
    Dim borderColor2 As Int = Colors.Magenta
       nativeMe.InitializeContext
       
    'iv.Bitmap = Null
       'bm.Initialize(File.DirAssets,"stitch.jpg")
       bm.Initialize3(bm)
       borderColor2 = 
    Colors.ARGB(25520000)
       bm2 = nativeMe.RunMethod(
    "addSquareBorder"Array(bm, borderWidth2, borderColor2))
       
    Log(bm2.Height)
       
    Return bm2
    End Sub
    When I run the above I don't get back the bitmap. The Java error I was referring to was when I add the code within #If Java tag.

    Thanks
     
  8. Johan Schoeman

    Johan Schoeman Expert Licensed User

    If you download the project in post #3 above - does it work when you run it?
     
  9. Indy

    Indy Active Member Licensed User

    Unfortunately, no. I get an error.

    Code:
    B4A version: 5.50
    Parsing code.  (
    0.00s)
    Compiling code.  (
    0.01s)
    Compiling layouts code.  (
    0.02s)
    Generating R 
    file.  (0.05s)
    Compiling generated Java code.  Error
    javac 
    1.7.0_45
    src\JHS\getRoundedCornerBitmap\main.java:
    14: error: package android.annotation does not exist
    import android.annotation.SuppressLint;
      ^
    1 erro
     
  10. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Delete this line from the inline Java code:
    import android.annotation.SuppressLint;

    ....and then try again.
     
    Last edited: Jan 10, 2016
  11. Indy

    Indy Active Member Licensed User

    It works! However, my function version doesn't seem to work. Can you take a quick look at my function and check that I haven't missed something?

    Thanks
     
  12. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Rather ZIP and upload your project - much easier to see what you are doing and where you have put what code than to guess with the code that you have posted.
     
  13. Indy

    Indy Active Member Licensed User

    Ok, I will try. It's just the project has confidential elements which if I strip out will not allow it to work. I will persevere with what I have got so far and change things around to see if that might make any difference.

    Thanks for your help with this - appreciate it very much.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice