草庐IT

Android - 在更深层的现有 View 之上精确绘制 ImageView

coder 2023-12-17 原文

这是一个难以描述的情况,所以请耐心等待。我被要求制作一个像这样的“教练标记”屏幕:

但是,在我的例子中,我需要将一些底层图像“拉”到前面并以不同的颜色显示它们以使其脱颖而出。我无法确定这些图像在运行时之前的“帧”是什么。我的方法是添加一个覆盖整个屏幕的黑色半透明 View (我称之为“窗帘”),然后添加我需要的图像作为具有不同颜色的窗帘的 subview 。 编辑 这部分很简单,我不会为此寻求帮助(这就是为什么认为这是与建议问题重复的建议是不正确的)。

我不相信我可以使用 bringToFront()setTranslationZ() 因为这些方法不会将 ImageView走到我的窗帘前面。所以,我想要做的是获取 ImageView 的确切“框架”(它的 x、y、宽度和高度)相对于整个屏幕,然后尝试添加新的使用具有相同框架的相同可绘制对象的 ImageView 。

了解底层“ View ”实际上由多个 View (包括滑动抽屉)组成也很重要,这就是为什么我需要获取相对于屏幕的坐标。我希望这是有道理的。

我能够以正确的颜色将新的 ImageView 绘制为窗帘的 subview ,但图像的位置和大小有很大偏差。我怎样才能将这个新的 ImageView 精确地绘制在前一个之上?

我也对其他策略持开放态度(因为这个似乎不起作用)。感谢您的帮助。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ...

    ViewTreeObserver vto = mRootview.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            mRootview.getViewTreeObserver().removeOnGlobalLayoutListener(this);

            mDrawerLayout.openDrawer(Gravity.LEFT);

            RelativeLayout curtain = addOpaqueCurtainToRootView();
            int frame[] = getFrameOfExistingImageView();
            addNewImageViewOnTopOfEverything(curtain, frame);
        }
    });
}

private RelativeLayout addOpaqueCurtainToRootView() {
    RelativeLayout curtain = new RelativeLayout(context);
    RelativeLayout.LayoutParams curtainParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    curtain.setLayoutParams(curtainParams);
    curtain.setBackgroundColor(Color.argb(179, 0, 0, 0)); //70% opaque
    mRootview.addView(curtain);
    return curtain;
}

private int[] getFrameOfExistingImageView() {
    // proving that I have a reference to the original image
    mCurrentImage.setColorFilter(Color.rgb(255, 0, 0));

    int array[] = new int[2];
    mCurrentImage.getLocationOnScreen(array);
    Log.d(TAG, "width: " + Integer.toString(array[0]));
    Log.d(TAG, "height: " + Integer.toString(array[1]));
    Log.d(TAG, "x: " + Float.toString(mCurrentImage.getX()));
    Log.d(TAG, "y: " + Float.toString(mCurrentImage.getY()));

    int frame[] = new int[4]; // x,y,width,height
    frame[0] = (int)mCurrentImage.getX();
    frame[1] = (int)mCurrentImage.getY();
    frame[2] = array[0];
    frame[3] = array[1];
    return frame;
}

private void addNewImageViewOnTopOfEverything(RelativeLayout curtain, int[] frame) {
    ImageView iv = new ImageView(context);
    iv.setImageResource(R.drawable.imgView);
    iv.setColorFilter(Color.rgb(255, 255, 255));
    iv.setX(frame[0]);
    iv.setY(frame[1]);
    iv.setLayoutParams(new RelativeLayout.LayoutParams(frame[2], frame[3]));

    curtain.addView(iv);
}

最佳答案

我想通了。我有两个基本问题。

首先,我弄错了 ImageView 的框架。我需要在绘制后获取图像的左坐标和上坐标以及图像的高度和宽度。这就是我这样做的方式(我实际上正在转换为左、上、右、下,但其余的很简单):

private int[] getFrameOfImageView(ImageView imageView) {

    int array[] = new int[2];
    imageView.getLocationOnScreen(array);

    int frame[] = new int[4];

    frame[0] = array[0]; // left
    frame[1] = array[1]; // top
    frame[2] = array[0] + imageView.getWidth(); // right
    frame[3] = array[1] + imageView.getHeight(); // bottom

    return frame;
}

然后,我创建了一个新类来进行绘图。这是与我的OP最大的不同。最初,我想我可以将其添加为新的 ImageView 并设置其 LayoutParams 以将其放置在我想要的位置。相反,我最终使用 PaintCanvas 进行绘图、调整大小和放置:

public class CanvasPainter extends View {

    Context mContext;
    Drawable mDrawable;

    int mFrame[];
    int left;
    int top;
    int right;
    int bottom;
    int width;
    int height;

    private final int LEFT = 0;
    private final int TOP = 1;
    private final int RIGHT = 2;
    private final int BOTTOM = 3;

    public CanvasPainter(Context context, int frame[], Drawable drawable) {
        super(context);

        mContext = context;
        mFrame = frame;
        mDrawable = drawable;

        left = frame[LEFT];
        top = frame[TOP];
        right = frame[RIGHT];
        bottom = frame[BOTTOM];
        width = right - left;
        height = bottom - top;

    }

    public void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);


        Bitmap b = ((BitmapDrawable) mDrawable).getBitmap();
        Bitmap bitmapResized = Bitmap.createScaledBitmap(b, width, height, false);

        canvas.drawBitmap(bitmapResized, left, top, paint);
    }
}

因此,将它们放在一起,所有这些都从 onCreate() 中组织起来,就像这样;

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ... 

    mDrawerLayout.openDrawer(Gravity.LEFT);

    ViewTreeObserver vto = mRootview.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override 
        public void onGlobalLayout() { 

            mRootview.getViewTreeObserver().removeOnGlobalLayoutListener(this);

            RelativeLayout curtain = addOpaqueCurtainToRootView();
            int frame[] = getFrameOfExistingImageView();
            Drawable d = ContextCompat.getDrawable(context, R.drawable.imgView);
            CanvasPainter canvasPainter = new CanvasPainter(context, mCurrentImg, frame, d);
            curtain.addView(canvasPainter);
        } 
    }); 
} 

可能是一个有点晦涩的问题,但我希望这对某人有所帮助。

关于Android - 在更深层的现有 View 之上精确绘制 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38408787/

有关Android - 在更深层的现有 View 之上精确绘制 ImageView的更多相关文章

  1. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  2. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  3. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  4. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  5. ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中 - 2

    目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi

  6. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  7. ruby-on-rails - 在 haml View 中重构条件 - 2

    除了可访问性标准不鼓励使用这一事实指向当前页面的链接,我应该怎么做重构以下View代码?#navigation%ul.tabbed-ifcurrent_page?(new_profile_path)%li{:class=>"current_page_item"}=link_tot("new_profile"),new_profile_path-else%li=link_tot("new_profile"),new_profile_path-ifcurrent_page?(profiles_path)%li{:class=>"current_page_item"}=link_tot("p

  8. ruby - Sinatra 找不到 View 目录 - 2

    我正在尝试以一种更类似于普通RubyGem结构的方式构建我的Sinatra应用程序。我有以下文件树:.├──app.rb├──config.ru├──Gemfile├──Gemfile.lock├──helpers│  ├──dbconfig.rb│  ├──functions.rb│  └──init.rb├──hidden│  └──Rakefile├──lib│  ├──admin.rb│  ├──api.rb│  ├──indexer.rb│  ├──init.rb│  └──magnet.rb├──models│  ├──init.rb│  ├──invite.rb│  ├─

  9. ruby-on-rails - 如何让 Rails View 返回其关联的操作名称? - 2

    我有一个非常简单的Controller来管理我的Rails应用程序中的静态页面:classPagesController我怎样才能让View模板返回它自己的名字,这样我就可以做这样的事情:#pricing.html.erb#-->"Pricing"感谢您的帮助。 最佳答案 4.3RoutingParametersTheparamshashwillalwayscontainthe:controllerand:actionkeys,butyoushouldusethemethodscontroller_nameandaction_nam

  10. ruby-on-rails - 在现有数据库上进行 Rails 迁移 - 2

    我正在创建一个新的Rails3.1应用程序。我希望这个新应用程序重用现有数据库(由以前的Rails2应用程序创建)。我创建了新的应用程序定义模型,它重用了数据库中的一些现有数据。在开发和测试阶段,一切正常,因为它在干净的表数据库上运行,但是当尝试部署到生产环境时,我收到如下消息:PGError:ERROR:column"email"ofrelation"users"alreadyexists***[err::localhost]:ALTERTABLE"users"ADDCOLUMN"email"charactervarying(255)DEFAULT''NOTNULL但是我在迁移中有这

随机推荐