I have a class called Preview that is added as a child to one of my FrameLayouts. This is the Preview's onDraw method. fingerprint.png is in my drawable folder, and
protected void onDraw(Canvas canvas) {
System.out.println("on draw");
Resources res = getResources();
fingerprint = res.getDrawable(R.drawable.fingerprint);
fingerprint.draw(canvas);
//fingerprintScaled.draw(canvas);
}
"on draw" prints, but the fingerprint image is not rendered.
The reason it is not drawing is because you are calling draw wrong.
in protected void onDraw(Canvas canvas)
do something like this (except dont decode stuff in onDraw)
Bitmap fingerprint = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_resource);
canvas.drawBitmap (fingerPrint, aMatrix, paintToDraw);
this should draw your bitmap properly.
Related
So I have a custom View which has a BitmapDrawable as its background which I draw to:
public void onDraw(Canvas canvas){
super.onDraw(canvas);
draw_game_map();
draw_player();
draw_overlay_with_circle();
}
(each of these methods just draws bitmap(s))
Now I'm calling View.draw(Canvas) 60 times per second and it works fine, except I want to have something like this:
public void onDraw(Canvas canvas){
super.onDraw(canvas);
//From this point, don't show what I'm about to draw
draw_game_map();
draw_player();
draw_overlay_with_circle();
//Show what I just drew
}
Any code I can add to only display the changes made only after I'm done drawing, rather than during?
I am writing a custom renderer for Xamarin.Forms. I am overriding the class ImageRenderer to do some tweaks to the image.
However, I want to overlay some parts of the image with circles, so I override the method void OnDraw(Canvas canvas).
Then I modify the Canvas a bit by drawing some circles:
protected override void OnDraw(Canvas canvas)
{
var paint = new Paint
{
Color = Color.Red
};
paint.SetStyle(Paint.Style.Fill);
foreach (var mapObject in _control.PointSource)
{
canvas.DrawCircle(mapObject.Location.X, mapObject.Location.Y, 100 / _scaleFactor, paint);
}
base.OnDraw(canvas);
}
However, everything is drawn on the background. The actual image is always on top. How can one draw circles on top of the image?
https://developer.xamarin.com/api/member/Android.Views.View.OnDraw/
In the docs they say the following:
the canvas on which the background will be drawn
So the current behaviour was expected.
I managed to fix it by moving the circle logic to the function: void Draw(Canvas canvas).
I created a customView class which exteds from View. In the method onDraw, I made a canvas and put the bitmap there, then I draw lines and circles on it. I called it in an other class in order to draw it.
Now I need to draw other bitmap on the First Bitmap. When I make another canvas in onDraw and draw the new bitmap there, the application stops working.
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Canvas singleUseCanvas = new Canvas();//Canvas in order draw second bitmap
canvas.drawBitmap(firstBitmap, 0, 0, new Paint());
singleUseCanvas.drawBitmap(roomIconBitmap,100,100,iconPaint); // Second bitmap
}
My question is: how can I draw a bitmap on an other bitmap by onDraw method?
I hope that I could ask my question well. Thank you in advance!
In my OnDraw method, I draw path into my canvas (circle with segements). All work well, now what I am not sure how to do is how can I tilt it (sort of rotation around one axis).
Basically, what I am trying to achieve is something like the tilt functionality of google map.
Here is the pseduo code:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// I do all the drawing here
canvas.tilt(45degrees) <-----??
}
I don't think you have to create the tilt effect within the onDraw method modifying your canvas manually. Instead use setRotationX() method on the whole View. You can also define it in xml as android:rotationX="45".
Use Canvas.concat(Matrix) with a rotation matrix, i.e.:
class MyView {
private final Matrix rotate = new Matrix();
public MyView()
{
rotate.setRotate(45.0f);
}
protected void onDraw(Canvas c)
{
super.onDraw(canvas);
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.concat(rotate);
canvas.restore();
}
}
Note also that you should not do any memory allocations inside onDraw() as per the android linter.
I want to display canvas contents on imageview in android
i am not understanding the
imageview.draw(canvas);
Heres my code:
public class Matrix extends Activity {
public Bitmap mybitmap,newbmp,bitmap,bmp;
ImageView imageview;
Paint paint;
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageview=(ImageView)findViewById(R.id.ImageView01);
imageview.setDrawingCacheEnabled(true);
}
protected void onDraw(Canvas canvas)
{
imageview.draw(canvas);
mybitmap=BitmapFactory.decodeResource(getResources(), R.drawable.image);
canvas.drawBitmap(mybitmap, 0, 0, paint);
}
}
"I want to display canvas contents on imageview in android"
So you want to draw what is on the Canvas in to your ImageView? If that's what you want then you need to read the links given by johike because you seem to have become confused a little.
The following in your code:
imageview.draw(canvas);
Does NOT mean draw the contents of canvas in to the imageview. It means the opposite, draw the imageview to the canvas.
Even if your question is not detailed enough to give you a precise answer I can give you the following hints:
Derive your on class from ImageView and then override the onDraw method
#Override
protected void onDraw(Canvas canvas) {
// draw a blue background
canvas.drawColor(Color.BLUE);
// additional drawings here
}
Further study the Android references:
http://developer.android.com/guide/topics/graphics/2d-graphics.html
http://developer.android.com/reference/android/graphics/Canvas.html