我有一个Java类,我正在辩论我应该为我的Android应用程序做什么。我不确定是否应该将其设为活动,视图或两者。Android查看或活动?
我不清楚原因与具有有参数,组件监听构造的它做,并绘制在画布上。
需要明确的是,试图使它成为一个视图时,我曾与构造的问题。如果是一项活动,我不确定如何在两者之间进行交互(即组件大小调整,颜色改变等) - 可能是否使用了Intents?因此,从逻辑上讲,也许最好的方式是视图和活动的混合。我对这个想法很陌生,所以请记住这一点。
我不知道如何那些谁创建了一些Android应用程序去确定你以前的Java类应该是什么。我的班似乎可以任何方式。
下面是我的课的一部分(在Java中,但我也有它转换到Android):
public class GraphDisplay extends JPanel implement Serializable {
public static final int BLUE_RED = 0;
public static final int BLACK_WHITE = 1;
protected int displayBoard;
protected int centerXcoord;
protected int centerYcoord;
private GeneralPath circlePath;
private GeneralPath rectPath;
private Color triangleColor;
private Color rectColor;
/*
* Create an instance of GraphDisplay using the default
* blue/red display
*/
public GraphDisplay() {
this(BLUE_RED, 0);
}
/*
* Create an instance of GraphDisplay using the board value
* and index of 0. Throws exception if model isn't a
* prederfined value
*/
public GraphDisplay(int board) {
this(model, 0);
}
public GraphDisplay(int board, int index) throws IllegalArgumentException {
if(board == BLUE_RED || board == BLACK_WHITE) {
displayBoard = board;
this.index = index;
if(displayBoard == BLUE_RED) {
setBackground(Color.blue);
triangleColor = Color.red;
rectColor = Color.red;
} else {
setBackground(Color.black);
triangleColor = Color.white;
rectColor = Color.white;
}
compListener = new GraphListener();
addGraphListener(compListener);
mListener = new GraphMouseListener();
addMouseMotionListener(mListener);
trianglePath = new GeneralPath();
rectPath = new GeneralPath();
setMinimumSize(new Dimension(400, 400));
setPreferredSize(new Dimension(400,400));
} else {
throw new IllegalArgumentException("Improper model")
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
centerXcoord = getWidth()/2;
centerYcoord = getHeight()/2;
drawTriangle(g2);
drawCircle(g2);
}
private void drawTriangle(Graphics2D g2) {
//code for drawing triangle with lines
}
private void drawRectangle(Graphics2D g2) {
//code for drawing circle
}
public void setTriangleColor(Color color) {
triangleColor = color;
repaint();
}
public Color getTriangleColor() {
return triangleColor;
}
//Omitted other getters/setter for brevity.
//The above get/set methods are a variety of what the class entails
protected class GraphMouseMotionListener extends MouseMotionAdapter {
public void mouseMoved(MouseEvent me) {
int x = mouseEvent.getX();
int y = mouseEvent.getY();
//if in component display message
}
}
protected class GraphComponentListener extends ComponentsAdapter {
public void componentResized(ComponentEvent ce) {
super.componentResized(ce);
}
}
}
所以我的问题是,你会使得绘制一个视图,视图的活动, 或者是其他东西?如果没有太多的麻烦,你能详细说明你为什么要按照你提到的方式来做? (如果将它在活动和视图之间分开,是否可以提供一个简要示例,它不必涉及我的课程 - 只是希望了解该概念)