1

DrawRect メソッドを使用してキャンバスに一連の長方形を描画するにはどうすればよいですか? ブロックの座標を渡すだけで動的な屋内マップを生成しようとしています。

知りたい.. for ループ内で drawRect メソッドを使用する方法..!

Luke Taylor の助けを借りて 2 つのクラスを作成しました (ありがとう..! :) )

1 つは座標で、もう 1 つは DrawMapActivity と呼ばれるメイン クラスです。

パッケージ itgsm.drawmap;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;

public class DrawMapActivity extends Activity {
/** Called when the activity is first created. */

static Coordinates[] coordinates = new Coordinates[10]; // 10 is just an example

//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
//     
//    }

public void onDraw(Canvas canvas) {

    Paint myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    myPaint.setStrokeWidth(8/*1 /getResources().getDisplayMetrics().density*/);
    myPaint.setColor(0xffff0000);   //color.RED

    for(int i = 0; i < coordinates.length; i++) {
        canvas.drawRect(new Rect(coordinates[i].getX(), coordinates[i].getY(),   coordinates[i].getX() + 10,coordinates[i].getY() + 10), myPaint); // 10 is the dimension of your block
        }


}
}
4

2 に答える 2

2

座標を含むオブジェクトの配列を受け取るメソッドを作成できます。

これは、座標を含む「Coordinate」と呼ばれるクラスになります。

public class Coordinate {

int x;
int y;

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

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

public int getX() {
return this.x;
}

public int getY() {
return this.y;

}

}

個々の座標を含むこれらのオブジェクトの配列を作成できます。

Coordinates[] coordinates = new Coordinates[10]; // 10 is just an example

レンダリング方法で、個々のブロックをレンダリングできるようになりました。

public void drawMap(Coordinates[] coordinates) {
for(int i = 0; i < coordinates.length; i++) {
canvas.drawRect(new Rect(coordinates[i].getX, coordinates[i].getY, coordinates[i].getX + 10,coordinates[i].getY + 10), paint); // 10 is the dimension of your block
}
}

これがお役に立てば幸いです。

アップデート:

これは、配列の1つのインデックスをCoordinateオブジェクトで埋める方法です。

coordinates[1] = new Coordinate();
coordinates[1].setX(0);
coordinates[1].setY(0);
于 2012-08-18T16:55:39.937 に答える
0
    public class DrawDemo extends Activity {
DemoView demoview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    demoview = new DemoView(this);
    setContentView(demoview);
}

private class DemoView extends View{
    public DemoView(Context context){
        super(context);
    }

    @Override protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // custom drawing code here
        // remember: y increases from top to bottom
        // x increases from left to right
        int x = 0;
        int y = 0;
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);

        // make the entire canvas white
        paint.setColor(Color.WHITE);
        canvas.drawPaint(paint);
        // another way to do this is to use:
        // canvas.drawColor(Color.WHITE);

        // draw a solid green rectangle
        paint.setAntiAlias(false);
        paint.setColor(Color.GREEN);
                    for(int i=0;i<10;i++)
                     {
                    //make changes in arguments here
        canvas.drawRect(100, 5, 200, 30, paint);
                     }
                     }
                      }
                      }
于 2012-08-18T16:09:52.020 に答える