在现有按钮下绘制位图并防止重叠
我正在尝试绘制一个用草块覆盖屏幕的位图。我正在绘制位图的屏幕已经有几个按钮和文本视图。当我将绘制位图的逻辑放入该类的 onDraw() 方法中时(该类对应于已经具有按钮和 TextView 的屏幕),位图将绘制在按钮上。结果看起来像这样:
看起来绿草如茵的斑块是绘制位图的结果。 “主页”按钮是页面上现有的按钮。正如您所看到的,绘制位图时出现的绿草斑块与“主页”按钮重叠。我不希望这种事发生。我希望“主页”按钮和“金钱:$550”文本显示在位图上。
我将如何做到这一点?
以下是我的一些代码供参考:
Screen.java
private Bitmap grassPatches;
private List<Grass> grassLocations = new ArrayList<>();
private int mapSize = 75 * Constants.SCREEN_WIDTH / 1000;
public GameScreenDrawableView(Context context) {
super(context);
grassPatches = BitmapFactory.decodeResource(this.getResources(), R.drawable.grass2);
grassPatches = Bitmap.createScaledBitmap(grassPatches, mapSize, mapSize, true);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 14; j++) {
grassLocations.add(new Grass(grassPatches, j * mapSize +
Constants.SCREEN_WIDTH / 2 - 6 * mapSize,
i * mapSize + 100 * Constants.SCREEN_HEIGHT / 1920, mapSize, mapSize));
}
}
}
protected void onDraw(Canvas canvas) {
for (int i = 0; i < grassLocations.size(); i++) {
canvas.drawBitmap(grassLocations.get(i).getBitMap(),
grassLocations.get(i).getX(),
grassLocations.get(i).getY(),
null);
}
}
Screen.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/game_screen_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#348C31"
tools:context=".GameActivity">
<TextView
android:id="@+id/h"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="472dp"
android:text="TextView"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/m"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="110dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/ex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:onClick="ex"
android:text="Home"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</FrameLayout>
I am trying to draw a Bitmap that covers a screen with grassy patches. The screen that I am drawing the Bitmap on already has a couple Buttons and TextViews. When I put the logic for drawing the Bitmap in the onDraw() method for the class (this class corresponds to the screen that already had Buttons and TextViews), the Bitmap gets drawn over the buttons. The result looks something like this:
The green grassy looking patches are the results of drawing the Bitmap. The Home button is the existing button on the page. As you can see, the green grassy patches that appear upon drawing the Bitmap overlap with the Home button. I don't want this to happen. I want the Home button and Money: $550 text to appear over the Bitmap.
How would I go about doing this?
Here is some of my code for reference:
Screen.java
private Bitmap grassPatches;
private List<Grass> grassLocations = new ArrayList<>();
private int mapSize = 75 * Constants.SCREEN_WIDTH / 1000;
public GameScreenDrawableView(Context context) {
super(context);
grassPatches = BitmapFactory.decodeResource(this.getResources(), R.drawable.grass2);
grassPatches = Bitmap.createScaledBitmap(grassPatches, mapSize, mapSize, true);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 14; j++) {
grassLocations.add(new Grass(grassPatches, j * mapSize +
Constants.SCREEN_WIDTH / 2 - 6 * mapSize,
i * mapSize + 100 * Constants.SCREEN_HEIGHT / 1920, mapSize, mapSize));
}
}
}
protected void onDraw(Canvas canvas) {
for (int i = 0; i < grassLocations.size(); i++) {
canvas.drawBitmap(grassLocations.get(i).getBitMap(),
grassLocations.get(i).getX(),
grassLocations.get(i).getY(),
null);
}
}
Screen.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/game_screen_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#348C31"
tools:context=".GameActivity">
<TextView
android:id="@+id/h"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="472dp"
android:text="TextView"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/m"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="110dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/ex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:onClick="ex"
android:text="Home"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</FrameLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论