如何从活动中调用自定义视图对象的更改

发布于 2024-10-01 05:24:45 字数 1280 浏览 2 评论 0原文

我有一个绘制到画布上的自定义视图。当用户从该视图所绑定的活动中按下按钮时,我试图调用对画布的更改。目前,我尝试通过调用我在视图内创建的名为 setNewDrawable 的公共方法来执行此操作。

当我发布 Invalidate 时,调试器为我提供不同的视图 ID 值,具体取决于我是在 setNewDrawable 内部还是在 Overridden onDraw 方法内部。

例如,调试器变量分别在 setNewDrawable 和 onDraw 中显示: (This - MyView id=830067720176) 或 (This - MyView id=830067712344) 。这让我觉得我基本上拥有该对象的两个副本,而我基本上正在与错误的副本进行交互。

如何向我的自定义视图获取信息以确定它绘制的内容?

这是我正在运行的代码。

public class Main extends Activity {

    MyView mView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = li.inflate(R.layout.main, null);   
        mView= (MyView) v.findViewById(R.id.my_view);

        //A button to modify what's drawn on the canvas
        Button switchLeft = (Button) findViewById(R.id.switch_left);
        switchLeft.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mView.setNewDrawable();
                mView.postInvalidate();
                }
            });
        }
    }

谢谢大家!

I have a custom view that draws to a canvas. I am trying to invoke changes to the canvas when the user pushes a button from the activity that this view is tied to. Currently, I am attempting to do this by calling a public method, called setNewDrawable, that I created inside the view.

The debugger is giving me different values for the view ID depending on whether I'm inside setNewDrawable or inside the Overridden onDraw method when I post an Invalidate.

For example the debugger variables show: (This - MyView id=830067720176) or (This - MyView id=830067712344) in setNewDrawable and onDraw respectively. This makes me think I basically have two copies of the object and I am essentially interacting with the wrong one.

How can I get information to my custom View in order to determine what it draws?

Here is the code I am running.

public class Main extends Activity {

    MyView mView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = li.inflate(R.layout.main, null);   
        mView= (MyView) v.findViewById(R.id.my_view);

        //A button to modify what's drawn on the canvas
        Button switchLeft = (Button) findViewById(R.id.switch_left);
        switchLeft.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mView.setNewDrawable();
                mView.postInvalidate();
                }
            });
        }
    }

Thanks all!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月朦胧 2024-10-08 05:24:45

问题出在充气机的布局上。我把它去掉了,效果很好。

The problem was the layout inflater. I removed it and it worked fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文