【问题标题】:Call a method from activity that extends View从扩展 View 的活动中调用方法
【发布时间】:2013-09-30 08:52:21
【问题描述】:

我正在做一种靶心的类型,但我使用的是正方形而不是圆形。

但问题是:

一切都完成了,生成其他方块颜色的算法也完成了。

但是我实现了一个按钮,我让它刷新了靶心。

问题是我做不到,需要帮助。

这是 MainActivity,我会从这里检测按钮点击。

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FrameLayout frame = (FrameLayout) findViewById(R.id.frame);

    Draw draw = new Draw(this);
    frame.addView(draw);

    Button refresh = (Button) findViewById(R.id.refresh);

    refresh.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            draw.onDraw(canvas);
            frame.addView(draw);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }
}

这是 Draw 活动,这是我用来渲染图像的活动。

public class Draw extends View {

public Draw(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

Paint prop = new Paint();
Random color = new Random();

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);

    int width = getWidth();
    int height = getHeight();

    int oriwidth = 0;
    int oriheight = 0;

    for (int x = 0; x < 20; x++) {
        int red = color.nextInt();
        int green = color.nextInt();
        int blue = color.nextInt();
        prop.setARGB(0, red, green, blue);
        canvas.drawRect(oriwidth += 10, oriheight += 10, width -= 10,
                height -= 10, prop);
    }
}
}

有人可以帮助我吗?对不起英语。

【问题讨论】:

  • 究竟是什么问题。你提到“问题是我做不到,需要帮助。”是编译问题还是运行时问题?
  • 我无法调用onDraw方法再次运行。我想让它看到一个新颜色的新方块。

标签: android image methods call


【解决方案1】:

您在 onCreate 期间以编程方式添加 Draw 视图有什么特殊原因吗? 尝试在布局 xml 本身中定义 Draw 视图。这应该可以解决定义视图宽度/高度的任何问题(确保定义宽度和高度......首先尝试硬编码尺寸,例如 100dp x 100dp)

完成此操作后,请确保将您的“绘图”视图捕获为 Activity 的成员:

public class MainActivity extends Activity 
{
private Draw mDraw;

然后,在您的 onCreate 中:

mDraw = (Draw)findViewById(R.id.myDrawId);

然后,在您的按钮单击侦听器中,只需调用 invalidate:

refresh.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mDraw.invalidate();
        }
    });

【讨论】:

  • 谢谢你!我只需要使用无效方法。我学到了一些东西!再次感谢!
【解决方案2】:

尝试使用这部分代码进行更改:

public void onClick(View v) {
     MainActivity.this.draw.onDraw(canvas);
     MainActivity.this.frame.addView(draw);
}

【讨论】:

    【解决方案3】:

    您不应直接致电onDraw()。试试draw.invalidate()

    【讨论】:

    • 他告诉我改变draw.onDraw(canvas);和 frame.addView(draw);修饰符为final。当我这样做时。我单击刷新按钮,它崩溃了。它仍然不起作用。
    • 好的,您不需要在 OnClickListener 中再次调用frame.addView(),此时视图已添加。尝试从侦听器中删除该行。
    猜你喜欢
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    相关资源
    最近更新 更多