0

My ImageView is matching screen size on x-axis and is using remaining space on y-axis in my layout. I want to create bitmap into this ImageView with exactly the same size as the ImageView is. How to make it please? Can it be done by some automatic setting, should I call some measure function?

I tried SetAdjustViewBounds() but it didn't work for me.

Creating Bitmap big enough (I don't like much such a memory wasting) and setting SetScaleType(ImageView.ScaleType.Matrix) works, but still when I'm making drawing operations on canvas, I don't know real size of area I should paint into, both canvas and bitmap height are equal to yScreen while imgWeekView height is pretending to be 0, even though it paints whole desired area with gray color.

enter image description here

imgWeekView = new ImageView(context);
//imgWeekView.SetAdjustViewBounds(true);
imgWeekView.SetScaleType(ImageView.ScaleType.Matrix);

layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent,1f);
layoutParams.Height = 0;
imgWeekView.LayoutParameters = layoutParams;

Bitmap bitmap = Bitmap.CreateBitmap((int)xScreen, (int)yScreen, Bitmap.Config.Argb8888);
cnvWeekView = new Canvas(bitmap);
imgWeekView.SetImageBitmap(bitmap);
linearLayout.AddView(imgWeekView); //whole activity layout

//Test
cnvWeekView.DrawColor(new Color(128, 128, 128));
Paint paint = new Paint(PaintFlags.AntiAlias);
paint.Color = new Color(255, 255,0);
cnvWeekView.DrawCircle(50, 50, 40, paint);
hoacin
  • 340
  • 1
  • 2
  • 19

1 Answers1

0

Finally I found a way how to measure my ImageView and here I will post my answer.

I believed that there should be much easier solution, but maybe there isn't. From this question I took most of the important data:

How to get the width and height of an android.widget.ImageView?

Things look however a little different in my android application and I'm not experienced enough to tell why. I had to change things a little. I had to learn a bit about interfaces and this question helped too.

Implementing the View.IOnTouchListener interface

Here is how I combined things. First I created class that will do the measure.

public class MyPredrawListener : Java.Lang.Object, ViewTreeObserver.IOnPreDrawListener
{
    ImageView imageView;
    public MyPredrawListener(ImageView img)
    {
        imageView = img;
    }
    public bool OnPreDraw()
    {
        imageView.ViewTreeObserver.RemoveOnPreDrawListener(this);
        int finalHeight = imageView.MeasuredHeight;
        int finalWidth = imageView.MeasuredWidth;
        Bitmap bitmap = Bitmap.CreateBitmap(finalWidth, finalHeight, Bitmap.Config.Argb8888);
        imageView.SetImageBitmap(bitmap);

        //Test to see result
        Canvas cnv = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.Color = new Color(255, 255, 0);
        cnv.DrawColor(new Color(128, 128, 128));
        cnv.DrawCircle(finalWidth-50, finalHeight-50, 50, paint);

        return true;
    }
}

And in code where I create my imageView I set the listener like this.

imgWeekView = new ImageView(context);
MyPredrawListener listener=new MyPredrawListener(imgWeekView);
imgWeekView.ViewTreeObserver.AddOnPreDrawListener(listener);

In OnPreDraw function I put test code to see the result graphically, clearing bitmap to gray color and painting yellow circle to bottom right of a view.

enter image description here

Community
  • 1
  • 1
hoacin
  • 340
  • 1
  • 2
  • 19