【问题标题】:How to measure ImageView before creating bitmap into it?如何在创建位图之前测量 ImageView?
【发布时间】:2016-11-06 17:59:07
【问题描述】:

我的 ImageView 与 x 轴上的屏幕尺寸匹配,并且在我的布局中使用 y 轴上的剩余空间。我想在这个 ImageView 中创建与 ImageView 完全相同大小的位图。请问如何制作?可以通过一些自动设置来完成,我应该调用一些测量函数吗?

我尝试了 SetAdjustViewBounds(),但它对我不起作用。

创建足够大的位图(我不喜欢这样浪费内存)和设置 SetScaleType(ImageView.ScaleType.Matrix) 有效,但是当我在画布上进行绘图操作时,我不知道我应该绘制的区域的实际大小,画布和位图高度都等于 yScreen,而 imgWeekView 高度假装为 0,即使它用灰色绘制整个所需区域。

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);

【问题讨论】:

    标签: layout bitmap imageview xamarin.android measure


    【解决方案1】:

    最后我找到了一种方法来测量我的 ImageView,我将在这里发布我的答案。

    我相信应该有更简单的解决方案,但也许没有。我从这个问题中获取了大部分重要数据:

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

    但在我的 android 应用程序中看起来有些不同,而且我没有足够的经验来解释原因。我不得不稍微改变一下。我必须学习一些关于接口的知识,这个问题也有帮助。

    Implementing the View.IOnTouchListener interface

    我是这样组合的。首先,我创建了将进行测量的类。

    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;
        }
    }
    

    在我创建 imageView 的代码中,我将监听器设置为这样。

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

    在 OnPreDraw 函数中,我输入测试代码以图形方式查看结果,将位图清除为灰色并将黄色圆圈绘制到视图的右下角。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 2021-04-20
      相关资源
      最近更新 更多