2017-11-14 1 views
0

CSV 파일의 일부 항목을 캔버스보기에 추가하려고합니다. 나는 네가 구멍을 파고 전리품을 얻는 게임을 만들고있다. 지뢰 찾기 게임과 같습니다. 하지만 캔버스에 임의로 항목을 추가하는 방법을 모르겠습니다. 내 모든 항목은 arraylist에 있지만 게임에 추가하는 방법을 모릅니다. 그리고 전리품을 발견 할 때마다 점수를 업데이트하십시오. 캔버스보기에 임의 항목 추가

내 코드 :

// List of items from CSV file. 
private ArrayList<ItemObject> mLoot; 
private String itemSize; 

@Override 
protected void onFinishInflate() { 
    super.onFinishInflate(); 

    // Let the view know that we want to draw. 
    setWillNotDraw(false); 

    getHolder().addCallback(this); 

    Resources res = getResources(); 

    // background image 
    mBackground = BitmapFactory.decodeResource(res, R.drawable.field); 
    mBlackPaint = new Paint(); 

    // Score text 
    mScoreTextPaint = new Paint(); 
    mScoreTextPaint.setColor(Color.WHITE); 
    mScoreTextPaint.setTextSize(60.0f); 
    mScoreTextPaint.setTextAlign(Paint.Align.CENTER); 

    // Show hole image 
    mHole = BitmapFactory.decodeResource(res, R.drawable.hole); 

    mPoints = new ArrayList<>(); 

    // Get CSV items 
    InputStream inputStream = getResources().openRawResource(R.raw.items); 
    GetCSVFile csvFile = new GetCSVFile(inputStream); 
    mLoot = csvFile.read(); 

    itemSize = String.valueOf(mLoot.size()); 

    for(ItemObject itemData:mLoot) { 

     //TODO: add a random x,y position for loot? 

     // itemData.getLoot() - "gold" 
     // itemData.getValue() - "50" 

    } 

내 된 onDraw 방법 :

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

    // Clear the canvas by drawing a single color. 
    canvas.drawColor(Color.BLACK); 

    // Draw image to cover all dimensions of the screen. 
    canvas.drawBitmap(mBackground, null, mDimensions, mBlackPaint); 

    // Draw a circle at each touch. 
    for(Point p: mPoints){ 
     // show hole's image when a player touches the screen 
     canvas.drawBitmap(mHole,p.x,p.y,null); 

    } 

    // Draw text in middle of screen. 
    canvas.drawText("Hidden Treasure: " + itemSize, 
      mDimensions.width()/2.0f, 
      mDimensions.height()/15.0f, 
      mScoreTextPaint); 

} // end onDraw 
+0

디스플레이를 무작위로 지정 하시겠습니까? 그래서 개체가 임의의 순서로 추가됩니까? – Barns

+0

@Bans 예.하지만 확실하지 않습니다. – art3mis

+0

코드에 추가하고자하는 부분은 다음과 같습니다 ::'// TODO : loot에 임의의 x, y 위치를 추가 하시겠습니까? '?? – Barns

답변

1

그런 다음 화면의 폭과 높이를 얻을 전체 화면을 사용하는 경우

DisplayMetrics displayMetrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
int maxHeight = displayMetrics.heightPixels; 
int maxWidth = displayMetrics.widthPixels; 

이 그렇지 않으면를 결정을 maxX 및 maxY 값이 필요합니다.

랜덤 한 위치를 얻기위한 간단한 방법을 만듭니다. 개체에 대한 "ItemObject"추가해야 세터와 게터에서

private int randomPosition(int min, int max) 
{ 
    int range = (max - min) + 1; 
    return (int)(Math.random() * range) + min; 
} 

좌표 :

String item = ""; 
int x = 0; 
int y = 0; 

public void setItem(String item) { this.item = item; } 
public void setX(int x) { this.x = x; } 
public void setY(int y) { this.y = y; } 

public String getItem() { return this.item; } 
public int getX() { return this.x; } 
public int getY() { return this.y; } 

당신이 문자열로 CSV 파일의 콘텐츠를 후에는 스플릿를 호출 할 수 있습니다 구분 기호 :

String myDelimiter = ","; // Or what ever your delimiter is 
String[] lines = csvData.Split(myDelimiter); 

이제 루프를 반복하면서 임의의 위치를 ​​얻으십시오.

for(String s : lines) { 
    // if 0 is the smallest x coordinates 
    int x = random(0, maxWidth); 
    int y = random(0, maxHeight); 
    ItemObject item = new ItemObject(); 
    item.setItem(s) 
    item.setX(x); 
    item.setY(y); 
    mLoot.add(item); 
} 

드로잉 루틴에서 "mLoot"를 단계별로 실행하여 개체를 그립니다.

+0

감사합니다. 또한, 어떻게 캔버스에 이러한 항목을 그릴까요? – art3mis

+0

@ art3mis :: 의도 한대로 작동 했습니까? – Barns

+0

해냈어, 고마워. – art3mis