gpt4 book ai didi

java - 如何在 Java/Android-Studio 中绘制一个简单的可重绘矩形?

转载 作者:行者123 更新时间:2023-12-01 10:58:18 26 4
gpt4 key购买 nike

我对 Java 和 Android Studio 很菜鸟,我只是找不到绘制矩形的好方法。为什么 Java 这么难?!我的 main_activity 中有一个按钮可以打开一个新 Activity 。这个:

package com.example.marty.pongtest;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;


public class GamePanelSingle extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_game_panel_single);

}

public class drawRectangle extends View{

public drawRectangle(Context context){
super(context);
}

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

Rect paddle = new Rect();
paddle.set(0, 0, canvas.getWidth()/2, canvas.getHeight()/2);

Paint white = new Paint();
white.setColor(Color.WHITE);
white.setStyle(Paint.Style.FILL);

canvas.drawRect(paddle,white);
}
}

我的 Xml 代码:

<?xml version="1.0" encoding="utf-8"?>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="GameSinglePlayer"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="55dp"
android:layout_marginStart="55dp"
android:layout_marginTop="22dp"
android:textColor="#FFFFFF" />

为什么这不起作用,我该如何让它起作用?

谢谢..

最佳答案

轻松平静:

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

canvas.save();

//Draw something

canvas.restore();
}

关于java - 如何在 Java/Android-Studio 中绘制一个简单的可重绘矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33485485/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com