如何利用 android 中的数据库数据利用 canvas

How to draw on canvas using database data in android

我是 Android 的新手,我一直在努力绘制 canvas 矩形和文本,其中矩形的大小、文本和颜色取决于从MySql 数据库。

我设法在我的 Activity 中选择了数据,但我就是不知道如何将 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>

坦克视图class:

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();
    }   
}

一条建议:

移动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();
    }