使用onDraw android

时间:2016-03-12 11:12:22

标签: java android canvas

我没有处理任何项目,只是想学习如何使用onDraw方法创建简单的动画。所以我开始在自定义视图上绘制多个对象,但我的程序只绘制了两个具有错误颜色的已定义对象。

这是我的简单对象类,名为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() + ")";
        }
    }

AbstractObject类



    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上创建四个正方形,红色,青色和黑色,但是它只创建了两个黑色正方形,我的问题是:问题是什么?为什么只画出两个物体以及为什么它们没有适当的颜色?

提前谢谢

1 个答案:

答案 0 :(得分:0)

不要在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);
}