2016-04-13 38 views
1

私はAndroidが初めてで、キャンバスに矩形やテキストを描画するのに苦労しています。矩形のサイズ、テキスト、色は、 MySqlデータベース。androidのデータベースデータを使用してキャンバスを描画する方法

アクティビティ内でデータを選択することができましたが、MySqlデータをondraw()メソッドに渡す方法を理解できないため、データを使用して矩形とテキストを描画できます。

ご協力いただきますようお願い申し上げます。

public class MyTankActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Intent intent = getIntent(); 
     int useridInt = intent.getIntExtra("userid", -1); 
     String userid = useridInt+""; 

     Response.Listener<String> responseListener = new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       try { 
        JSONObject jsonResponse = new JSONObject(response); 

        int success = jsonResponse.getInt("success"); 
        JSONArray tank_data = jsonResponse.getJSONArray("tank_data"); 

        if (success == 1) { 
         int i; 
         for(i=0;i<tank_data.length();i++){ 
          // Log.v("Result--", "" + tank_data.getString(i)); 

          JSONObject tankObj = tank_data.getJSONObject(0); 

          String location = (String) tankObj.getString("Location"); 
          String color = (String) tankObj.getString("Color"); 
          String Level = (String) tankObj.getString("Level"); 
         } 
        } else { 
         // No records found in database 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     }; 

     MyTankRequest myTankRequest = new MyTankRequest(userid, responseListener); 
     RequestQueue queue = Volley.newRequestQueue(MyTankActivity.this); 
     queue.add(myTankRequest); 

     setContentView(new TankView(this)); 
    } 
} 

XMLレイアウト:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.nivelsonic.nivelsonic.MyTankActivity" 
    android:background="#AEECFF"> 

    <com.nivelsonic.nivelsonic.TankView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</RelativeLayout> 

TankViewクラス:

package com.nivelsonic.nivelsonic; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.view.View; 

public class TankView extends View { 

    private Paint _paintTank = new Paint(); 
    private Path _path = new Path(); 

    public TankView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
     init(null, 0); 
    } 

    public TankView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     // TODO Auto-generated constructor stub 
     init(attrs, 0); 
    } 

    public TankView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
     init(attrs, 0); 
    } 

    private void init(AttributeSet attrs, int defStyle) { 
     _paintTank.setColor(Color.RED); 
     _paintTank.setAntiAlias(true); 
     _paintTank.setStyle(Paint.Style.STROKE); 
    } 

    @Override 
    public void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     // canvas.drawText(); 
     // canvas.rect(); 
    } 
} 

答えて

0

1つの提案:グローバル変数へ

移動JSONデータ

public class MyTankActivity extends AppCompatActivity { 

    String location; 
    String color; 
    String Level; 
    //.... 

得られたデータを渡すことができTankViewの異なるコンストラクタとsetContentViewを変更:

setContentView(new TankView(this, location, color, Level)); 

も、この値を格納するTankViewにいくつかの変数を追加し、それに応じてTankViewのコンストラクタを変更:

public TankView(Context context, String loc, String color, String level) { 
    super(context); 
    init(null, 0); 
    this.location = loc;// ... do the same with color/level 
    ... 
} 

データを取得した後にonDrawを変更してください。

@Override 
    public void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     //using this.Location, color, text 
     // canvas.drawText();. 
     // canvas.rect(); 
    } 
+0

ありがとうTung、あなたの提案は素晴らしい仕事をしました。今は変数をonDrawメソッドに渡すことができますが、何らかの理由で変数の場所がResponse.Listenerコード内に決して設定されません。私は、Log.v.でデータを見ることができるので、SQL呼び出しが機能することを知っています。ロケーション変数が設定されない理由についてのアイデアはありますか?私はテストする場所= "HELLO"を設定し、それは "HELLO"を描画しますが、Listenerの中には設定されません。 –

+0

こんにちは、 'this.location =(String)tankObj.getString(" Location ");'正しい場所のデータを取得してください。通常、色のために働く場合、それは場所のために働くはずです。 –

+0

ありがとうございました。私はいくつかの調査を行っており、データベースへのネットワーク呼び出しとondrawプロセスが非同期で実行されるため、別の方法で処理する必要があることが分かりました。このサイトでは、https:// realm .io/news/android-threading-background-tasks / –

関連する問題