How to set ANTI_ALIAS_FLAG?

bitstra

Member
Licensed User
Longtime User
How to set ANTI_ALIAS_FLAG? SOLVED by Erel

Hi,

I have a canvas for drawing a gps satellites position polarview. The drawed circles and lines are not really fine!

I've tried:

Dim Obj1 As Reflection
Obj1.Target = csvSats
Obj1.Target = Obj1.GetField("paint")
Obj1.RunMethod2("setAntiAlias", True, "java.lang.boolean")


... dos't work for me :(

Is there another way to set android ANTI_ALIAS_FLAG for my canvas?


regards

bitstra
 
Last edited:

bitstra

Member
Licensed User
Longtime User
thanx for the quick answer Erel,

but unfort. i can't also this to have get working

error: java.lang.NoSuchFieldExecption: paint ...

no idea!


regards


bitstra
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
My mistake. Your code looks correct.
I've tried this code and I don't get any errors:
B4X:
Sub Globals        
   Dim cvs As Canvas
End Sub

Sub Activity_Create(FirstTime As Boolean)
   cvs.Initialize(Activity)
   Dim r As Reflector
   r.Target = cvs
   r.Target = r.GetField("paint")
   r.RunMethod2("setAntiAlias", True, "java.lang.boolean")
End Sub
 
Upvote 0

bitstra

Member
Licensed User
Longtime User
Hi joseluis,

thnak you too. - but no succes :(

I'm googling and reading now for this some more...

bitstra
 
Upvote 0

bitstra

Member
Licensed User
Longtime User
O.K. found a java code sample for my problem
here

Draw lines anti-aliased:


code:

public class DemoActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageView = (ImageView) findViewById(R.id.ImageView01);
imageView.setImageBitmap(getBitmap());
}

private static Bitmap getBitmap() {
// setup bitmap, canvas and paint
Bitmap bitmap = Bitmap.createBitmap(300, 200, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();

// fill the background (gray)
Rect rect = canvas.getClipBounds();
paint.setColor(Color.GRAY);
canvas.drawRect(rect, paint);

// set line color (yellow) and stroke width
paint.setColor(Color.YELLOW);
paint.setStrokeWidth(2);

// draw line without antialiasing
canvas.drawLine(0, 20, 280, 200, paint);

// draw line with antialiasing
DrawFilter drawFilter = new PaintFlagsDrawFilter(0,Paint.ANTI_ALIAS_FLAG);
canvas.setDrawFilter(drawFilter);
canvas.drawLine(20, 0, 300, 180, paint);


return bitmap;
}
}



Tried to write a b4a library, but failed for now.

Any Ideas for getting 'canvas.setDrawFilter' via Reflection lib?

regards

bitstra
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Globals        
   Dim cvs As Canvas
End Sub

Sub Activity_Create(FirstTime As Boolean)
   cvs.Initialize(Activity)
   cvs.DrawCircle(100dip, 100dip, 100dip, Colors.Red, False, 1dip)
   SetAntiAlias(cvs)
   cvs.DrawCircle(200dip, 200dip, 100dip, Colors.Red, False, 1dip)
End Sub

Sub SetAntiAlias (c As Canvas)
   Dim r As Reflector
   Dim NativeCanvas As Object
   r.Target = c
   NativeCanvas = r.GetField("canvas")
   Dim PaintFlagsDrawFilter As Object
   PaintFlagsDrawFilter = r.CreateObject2("android.graphics.PaintFlagsDrawFilter", _
      Array As Object(0, 1), Array As String("java.lang.int", "java.lang.int"))
   r.Target = NativeCanvas
   r.RunMethod4("setDrawFilter", Array As Object(PaintFlagsDrawFilter), Array As String("android.graphics.DrawFilter"))
End Sub
 
Upvote 0

bitstra

Member
Licensed User
Longtime User
1000 thanks

Hi Erel,

that is fast and awesome. The code in your sub works great.
And my satview-panel for gps mockLocation provider looks very fine drwan!

Your service and response times are best I know :sign0188:
Keep up your great work.

1000 thanx

bitstra
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I modified a bit Erels' code to activate or deactivate the filter.
B4X:
Sub SetAntiAlias (c As Canvas, Active As Int)
    ' Active = 0 filter OFF
    ' Active = 1 filter ON
    Dim r As Reflector
    Dim NativeCanvas As Object
    r.Target = c
    NativeCanvas = r.GetField("canvas")
    Dim PaintFlagsDrawFilter As Object
    PaintFlagsDrawFilter = r.CreateObject2("android.graphics.PaintFlagsDrawFilter", _
        Array As Object(0, Active), Array As String("java.lang.int", "java.lang.int"))
    r.Target = NativeCanvas
    r.RunMethod4("setDrawFilter", Array As Object(PaintFlagsDrawFilter), Array As String("android.graphics.DrawFilter"))
End Sub
Best regards.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
This is an old thread, I know, but if someone searches for "anti-aliasing" he gets this thread, so I think to post here "my opinion".

I used the Erel's version and it works well with some methods of Canvas.
B4X:
Sub SetAntiAlias (c As Canvas)
   Dim r As Reflector
   Dim NativeCanvas As Object
   r.Target = c
   NativeCanvas = r.GetField("canvas")
   Dim PaintFlagsDrawFilter As Object
   PaintFlagsDrawFilter = r.CreateObject2("android.graphics.PaintFlagsDrawFilter", _
      Array As Object(0, 1), Array As String("java.lang.int", "java.lang.int"))
   r.Target = NativeCanvas
   r.RunMethod4("setDrawFilter", Array As Object(PaintFlagsDrawFilter), Array As String("android.graphics.DrawFilter"))
End Sub

To get the anti-aliasing on DrawBitmap Canvas method, PaintFlagsDrawFilter seems to should be set differently.

Then, I used:
public static final int FILTER_BITMAP_FLAG
Added in API level 1
Paint flag that enables bilinear sampling on scaled bitmaps.
Constant Value: 2 (0x00000002)
http://developer.android.com/reference/android/graphics/Paint.html#FILTER_BITMAP_FLAG

and this is the result:

with:
PaintFlagsDrawFilter = r.CreateObject2("android.graphics.PaintFlagsDrawFilter", _
Array As Object(0, 1), Array As String("java.lang.int", "java.lang.int"))
upload_2014-12-13_5-21-29.png



with:
PaintFlagsDrawFilter = r.CreateObject2("android.graphics.PaintFlagsDrawFilter", _
Array As Object(0, Flag), Array As String("java.lang.int", "java.lang.int"))
where Flag = FILTER_BITMAP_FLAG (= 2):
upload_2014-12-13_5-22-57.png



Ergo... Merry Christmas :)
 
Upvote 0
Top