gpt4 book ai didi

java - 使用 onDraw android 在 View 中绘制多个对象

转载 作者:太空宇宙 更新时间:2023-11-04 12:49:36 26 4
gpt4 key购买 nike

我没有从事任何项目,只是想学习如何使用 view onDraw 方法创建简单的动画。因此,我开始在自定义 View 上绘制多个对象,但我的程序仅绘制了两个颜色错误的已定义对象。

这是我的简单对象类,名为 Square,它扩展了抽象类 AbstractObject,它定义了方形对象的属性和方法。



package com.kgkg.animationlearn.myDrawings;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
import com.kgkg.animationlearn.MainActivity;
/**
* Created by Krzysiek on 2016-03-11.
*/
public class Square extends AbstractShape {
//****************************************************FIELDS*******************
private int side;

//**************************************************CONSTRUCTORS*******************************
private Square() {}

public Square(int side) {
super();
this.side = side;
}

public Square(Paint fillColor, int side) {
super(fillColor);
this.side = side;
}

public Square(Paint fillColor, int x, int y, int side) {
super(fillColor, x, y);
this.side = side;
}

public Square(int x, int y, int side) {
super(x, y);
this.side = side;
}

//************************************************GETTERS AND SETTERS**************************
public int getSide() {
return side;
}

public void setSide(int side) {
this.side = side;
}

//****************************************************METHODS**********************************

/**
* side, canvas and paint fields cannot be null otherwise they will use defaults
* @param canvas
*/
@Override
public void drawOnCanvas(Canvas canvas) {
if (side > 0 && canvas != null){
canvas.drawRect(getX(), getY(), side, side, getFillColor());
} else {
Log.i(MainActivity.TAG, "Somthing is wrong with your object");
}
}

@Override
public String toString() {
return "Square object \n Properties: \n side: " + side + "\n fillColor: " + getFillColor() +
"\nPosition ( " + getX() + ", " + getY() + ")";
}
}

抽象对象类



public abstract class AbstractShape implements IDrawOnCanvas{

//****************************************************FIELDS***********************************
private Paint fillColor = new Paint(Color.DKGRAY);
private int x, y;

//**************************************************CONSTRUCTORS*******************************
public AbstractShape() {}

public AbstractShape(Paint fillColor) {
this.fillColor = fillColor;
}

public AbstractShape(Paint fillColor, int x, int y) {
this.fillColor = fillColor;
this.x = x;
this.y = y;
}
public AbstractShape(int x, int y) {
this.x = x;
this.y = y;
}

//************************************************GETTERS AND SETTERS**************************
public Paint getFillColor() {
return fillColor;
}

public void setFillColor(Paint fillColor) {
this.fillColor = fillColor;
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public void setPosition(int x, int y){
this.x = x;
this.y = y;
}

//****************************************************METHODS**********************************

}

Interface IDrawOnCanvas
<pre><code>
package com.kgkg.animationlearn.myDrawings;
import android.graphics.Canvas;
public interface IDrawOnCanvas {
void drawOnCanvas(Canvas canvas);
}

View class
<pre><code>
public class myView extends View {

private boolean toDraw;
private Square[] objects;

public myView(Context context) {
super(context);
init();
}

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

private void init(){
toDraw = false;
}

public boolean isToDraw() {
return toDraw;
}

public void setToDraw(boolean toDraw) {
this.toDraw = toDraw;
}

public Square[] getObjects() {
return objects;
}

public void setObjects(Square[] objects) {
this.objects = objects;
}

@Override
protected void onDraw(Canvas canvas) {
if (toDraw){
for (int i = 0; i < objects.length; i++){
canvas.save();
Log.i(MainActivity.TAG, "rysownaie + " + objects[i].toString());
objects[i].drawOnCanvas(canvas);
canvas.restore();
invalidate();
}
}
super.onDraw(canvas);
}


}

MainActivity
<pre><code>
public class MainActivity extends AppCompatActivity {

public final static String TAG = "kgkg";

RelativeLayout relLayout;
Button btnTest;
View myV;

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

relLayout = (RelativeLayout) findViewById(R.id.relLayout);
btnTest = (Button) findViewById(R.id.button);
myV = findViewById(R.id.view);
btnTest.setOnClickListener(c);
}

private View.OnClickListener c = new View.OnClickListener() {
@Override
public void onClick(View v) {
Square[] squares = {new Square(new Paint(Color.RED), 0, 0, 50), new Square(50, 50, 100), new Square(50, 0, 50), new Square(new Paint(Color.CYAN), 150, 150, 130)};
((myView)myV).setToDraw(true);
((myView) myV).setObjects(squares);
myV.invalidate();
}
};
}

在按钮的 onclick 方法之后,它应该在 myView 上创建四个红色、青色和黑色的正方形,但它只创建两个黑色的正方形,我的问题是:问题是什么?为什么只绘制了两个对象以及为什么它们没有正确的颜色?

提前谢谢

最佳答案

不要在 OnDraw 函数的 for 循环内无效。我还认为保存和恢复调用没有用处。改变这个

  @Override
protected void onDraw(Canvas canvas) {
if (toDraw){
for (int i = 0; i < objects.length; i++){
canvas.save();
Log.i(MainActivity.TAG, "rysownaie + " + objects[i].toString());
objects[i].drawOnCanvas(canvas);
canvas.restore();
invalidate();
}
}
super.onDraw(canvas);
}

到此

 @Override
protected void onDraw(Canvas canvas) {
if (toDraw){
for (int i = 0; i < objects.length; i++){
Log.i(MainActivity.TAG, "rysownaie + " + objects[i].toString());
objects[i].drawOnCanvas(canvas);
}
}
super.onDraw(canvas);
}

关于java - 使用 onDraw android 在 View 中绘制多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35956903/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com