我创建了一个布局,其中包含一些视图,还包含一个用于绘制图形的 LinearLayout
main_page_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bgcolor"
android:id="@+id/main_page_layout"
>
....
<LinearLayout
android:id="@+id/graphicView"
android:layout_width="match_parent"
android:layout_height="196dp"
android:layout_marginTop="64dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:background="@color/white"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/menuLayout" />
...
</androidx.constraintlayout.widget.ConstraintLayout>
我正在尝试使用名为 GraphPlotter 的自定义类在其上绘制图形
GraphPlotter.java
public class GraphPlotter extends View {
private Paint paint = null;
private Bitmap bitmap;
private Canvas canvas;
public GraphPlotter(Context context) {
super(context);
this.init();
}
/* Initiates graph plotter */
private void init()
{
this.paint = new Paint();
this.paint.setColor(Color.BLUE);
this.paint.setStrokeWidth(5);
this.paint.setStyle(Paint.Style.FILL_AND_STROKE);
// Create a bitmap and a canvas associated with the bitmap
this.bitmap = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888);
this.canvas = new Canvas(bitmap);
}
public void setColor(int color) {
this.paint.setColor(color);
invalidate(); // Force a redraw
}
/* Draws a line with a custom color */
public void drawLine(float starX, float startY, float stopX, float stopY,int color)
{
this.setColor(color);
this.canvas.drawLine(starX, startY, stopX, stopY, this.paint);
}
........
这是我要画的活动
MainPageActivity.java
public class MainPageActivity extends AppCompatActivity {
private ConnectionHelper connectionHelper = null;
private SQLQuery sqlQuery = null;
private Person person = null;
private TextView txtID = null;
private RecyclerView recyclerView = null;
private final ProductAdapter productAdapter = null;
private AsyncTaskRunner asyncTaskRunner = null;
private ViewHolder viewHolder = null;
private Spinner graphicTypeSpinner = null;
private Button menuButton = null;
private ConstraintLayout menuView = null;
private List<Product> productList = null;
private GraphPlotter graphPlotter = null;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState); // necessary for onCreate function
setContentView(R.layout.main_page_layout); // R = resource , R.layout => layouts directory
init();
LinearLayout graphicView = (LinearLayout) findViewById(R.id.graphicView);
graphPlotter = new GraphPlotter(this);
graphPlotter.drawAxisX(159,159,259, Color.BLUE);
graphicView.addView(graphPlotter);
}
...........
...................
但是我看不到任何线或轴。
我想使用画布为产品绘制图形,但我看不到任何结果,我的类或代码有什么问题
将您的绘制行为放入 onDraw() 函数中
@Override
protected void onDraw(Canvas canvas) {
drawLine(...);
drawAxisX();
}