草庐IT

Android:对 View 应用任意变换

coder 2023-12-21 原文

我想采用任意矩阵并将其应用于 android.views.View。

我发现的唯一可靠方法是这个 hack:

 MyAnimation animation = new MyAnimation(matrix);
 animation.setDuration(0);
 animation.setFillAfter(true);
 view.setAnimation(animation);

有没有更好的方法?我尝试利用 getChildStaticTransformation 并将其放入父项中,但没有成功(也许我做错了?)

最佳答案

最后,我基于 AbsoluteLayout 创建了自己的布局,向我的 LayoutParams 添加了一个 Matrix,利用了 getChildStaticTransformation,并覆盖了 dispatchTouchEvent,以便我的 child 在旋转时响应正确的边界。比我预期的要困难得多。

public class UIViewLayout extends ViewGroup {

@Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
    if(child instanceof UIViewLayout) {
        t.getMatrix().reset();
        UIViewLayout.LayoutParams params = (UIViewLayout.LayoutParams)child.getLayoutParams();
        t.setTransformationType(Transformation.TYPE_MATRIX);
        t.getMatrix().set(params.matrix);
    }
    return true;
}

public UIViewLayout(android.content.Context context) {
    super(context);
    this.setClipChildren(false);
    this.setClipToPadding(false);
    this.setChildrenDrawingOrderEnabled(true);
    this.setStaticTransformationsEnabled(true);
}

public UIViewLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public UIViewLayout(Context context, AttributeSet attrs,
                      int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int count = getChildCount();

    int maxHeight = 0;
    int maxWidth = 0;

    // Find out how big everyone wants to be
    measureChildren(widthMeasureSpec, heightMeasureSpec);

    // Find rightmost and bottom-most child
    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            int childRight;
            int childBottom;

            UIViewLayout.LayoutParams lp
                    = (UIViewLayout.LayoutParams) child.getLayoutParams();

            childRight = lp.x + child.getMeasuredWidth();
            childBottom = lp.y + child.getMeasuredHeight();

            maxWidth = Math.max(maxWidth, childRight);
            maxHeight = Math.max(maxHeight, childBottom);
        }
    }

    // Account for padding too
    //maxWidth += mPaddingLeft + mPaddingRight;
    //maxHeight += mPaddingTop + mPaddingBottom;

    // Check against minimum height and width
    maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
    maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());

    setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, 0),
            resolveSizeAndState(maxHeight, heightMeasureSpec, 0));
}

@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
    return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0);
}

@Override
protected void onLayout(boolean changed, int l, int t,
                        int r, int b) {
    int count = getChildCount();

    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        if (child.getVisibility() != GONE) {

            UIViewLayout.LayoutParams lp =
                    (UIViewLayout.LayoutParams) child.getLayoutParams();

            int childLeft = lp.x;
            int childTop = lp.y;
            child.layout(childLeft, childTop,
                    childLeft + child.getMeasuredWidth(),
                    childTop + child.getMeasuredHeight());

        }
    }
}

@Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
    return new UIViewLayout.LayoutParams(getContext(), attrs);
}

// Override to allow type-checking of LayoutParams.
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
    return p instanceof UIViewLayout.LayoutParams;
}

@Override
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
    return new LayoutParams(p);
}

@Override
public boolean shouldDelayChildPressedState() {
    return false;
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    for(int i = 0; i < this.getChildCount(); i++) {
        View child = getChildAt(i);
        if(child instanceof UIViewLayout) {
            UIViewLayout.LayoutParams params = (UIViewLayout.LayoutParams)child.getLayoutParams();
            if(!params.matrix.isIdentity()) {
                MotionEvent ev2 = MotionEvent.obtain(ev);

                ev2.setLocation(ev2.getX() - params.x, ev2.getY() - params.y);

                Matrix m = new Matrix();
                params.matrix.invert(m);
                ev2.transform(m);
                if(child.dispatchTouchEvent(ev2)) {
                    return true;
                }
                ev2.recycle();
            }
        }
    }
    return super.dispatchTouchEvent(ev);
}

public static class LayoutParams extends ViewGroup.LayoutParams {
    public int x;
    public int y;
    public Matrix matrix;

    public LayoutParams(int width, int height, int x, int y) {
        super(width, height);
        this.x = x;
        this.y = y;
        this.matrix = new Matrix();
    }

    public LayoutParams(Context c, AttributeSet attrs) {
        super(c, attrs);
    }

    public LayoutParams(ViewGroup.LayoutParams source) {
        super(source);
    }
}

}

关于Android:对 View 应用任意变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21708196/

有关Android:对 View 应用任意变换的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

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

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

  4. 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=>

  5. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  6. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  7. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  8. 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

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

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

  10. ruby-on-rails - 如何在 Gem 中获取 Rails 应用程序的根目录 - 2

    是否可以在应用程序中包含的gem代码中知道应用程序的Rails文件系统根目录?这是gem来源的示例:moduleMyGemdefself.included(base)putsRails.root#returnnilendendActionController::Base.send:include,MyGem谢谢,抱歉我的英语不好 最佳答案 我发现解决类似问题的解决方案是使用railtie初始化程序包含我的模块。所以,在你的/lib/mygem/railtie.rbmoduleMyGemclassRailtie使用此代码,您的模块将在

随机推荐