gpt4 book ai didi

android - Canvas 的大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:35 26 4
gpt4 key购买 nike

我有一个 Android 应用程序,它有几个 fragment 。

在其中一个 fragment 中,有

  1. 一个字母,
  2. 自定义 View ,
  3. 两个带按钮的面板。

在自定义 View (第 2 项)中,我需要绘制几个图形,其中一个与 View 的大小相关,即。 e.应该有一个圆角矩形,其大小等于 Canvas 的大小减去填充。

为此,我需要获取 Canvas 的宽度和高度。

我尝试了以下操作:

  1. 在方法 onSizeChanged 中获取 View 的尺寸(新的宽度/高度)。
  2. onLayout 方法中获取 View 的尺寸。
  3. onDraw 方法中获取 View 的尺寸(canvas.getWidth()/getHeight(), View.getMeasuredWidth()/getMeasuredHeight())。<

所有这三种方法都返回相同的宽度和高度,但都不起作用 - 图形太窄(仅填充大约 60% 的可用空间,而不是 100%)并且太高(图形底部不可见)。

确定自定义 View 尺寸(RectF 实例)的正确方法是什么?

请注意,我在横向模式下的 Nexus 7 模拟器上测试了此应用程序。

更新 1(2013 年 3 月 28 日 21:42 MSK)

对应 fragment 的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/simulation_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simulation"
android:textAppearance="?android:attr/textAppearanceLarge" />

<co.mycompany.ccp.android.impl.simulationcanvas.SimulationCanvasView
android:id="@+id/simulation_canvas_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.8" />

<LinearLayout
android:id="@+id/simulationExecutionPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1" >

<Button
android:id="@+id/restartSimulationButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/restart_simulation" />

<Button
android:id="@+id/simulationStepButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/simulation_step" />

<Button
android:id="@+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pause" />

<Button
android:id="@+id/continueButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/continue_button" />

<Button
android:id="@+id/simulateAdInfinitumButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/simulate_ad_infinitum" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/currentCycleLabel" />

<TextView
android:id="@+id/currentCycleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cycle"
android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1" >

<Button
android:id="@+id/addCompanyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add_company2" />

<Button
android:id="@+id/removeCompanyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/remove_company" />

<Button
android:id="@+id/setLabourForceButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/set_labour_force" />
</LinearLayout>

</LinearLayout>

这是 View 的代码(@+id/simulation_canvas_view):

import co.mycompany.ccp.android.api.economypartsdimensioncalculator.EconomyPartsDimensionCalculator;
import co.mycompany.ccp.android.api.systemboundary.SystemBoundaryGraphicsCalculator;
import co.mycompany.ccp.android.impl.economypartsdimensioncalculator.DefaultEconomyPartsDimensionCalculator;
import co.mycompany.ccp.android.impl.systemboundary.DefaultSystemBoundaryGraphicsCalculator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
* @author DP118M
*
*/
public class SimulationCanvasView extends View {

private static final int SYSTEM_BOUNDARY_COLOUR = Color.LTGRAY;

[...]

private int width = -1;
private int height= -1;
private SystemBoundaryGraphicsCalculator systemBoundaryGraphicsCalculator = new DefaultSystemBoundaryGraphicsCalculator();
[...]
private Rect systemBoundaryDimensions = new Rect(100, 100, 100 + 100,
100 + 100);
private Rect externalEconomyDimensions;

[...]

public SimulationCanvasView(final Context aContext) {
super(aContext);
}

public SimulationCanvasView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}

public SimulationCanvasView(final Context context,
final AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

[...]

private void updateSystemBoundaryGraphicsCalculatorDimensions() {
systemBoundaryGraphicsCalculator.setCanvasHeight(height);
systemBoundaryGraphicsCalculator.setCanvasWidth(width);
try {
systemBoundaryGraphicsCalculator.run();
systemBoundaryDimensions = systemBoundaryGraphicsCalculator
.getSystemBoundaryDimensions();
} catch (final Exception exception) {
throw new RuntimeException(exception);
}
}

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

this.width = this.getWidth();
this.height = this.getHeight();

updateSystemBoundaryGraphicsCalculatorDimensions();

[...]

drawRectangleWithRoundedEdges(aCanvas, systemBoundaryDimensions,
SYSTEM_BOUNDARY_COLOUR);

[...]
}

private void drawRectangleWithRoundedEdges(final Canvas aCanvas,
final Rect aDimensions, int aStrokeColour) {
final Paint paint = new Paint();

paint.setColor(aStrokeColour);
paint.setStrokeWidth(1);
paint.setStyle(Paint.Style.STROKE);

aCanvas.drawRoundRect(new RectF(aDimensions), 20, 20, paint);
}
}

下面是计算圆角矩形尺寸的类:

package co.mycompany.ccp.android.impl.systemboundary;

import android.graphics.Rect;
import co.mycompany.ccp.android.api.systemboundary.SystemBoundaryGraphicsCalculator;

/**
* @author DP118M
*
*/
public class DefaultSystemBoundaryGraphicsCalculator implements
SystemBoundaryGraphicsCalculator {
private int canvasWidth;
private int canvasHeight;
private int xPadding = SYSTEM_BOUNDARY_X_PADDING;
private int yPadding = SYSTEM_BOUNDARY_Y_PADDING;
private Rect systemBoundaryDimensions;

public void setXPadding(final int xPadding) {
this.xPadding = xPadding;
}

public void setYPadding(final int yPadding) {
this.yPadding = yPadding;
}

@Override
public Rect getSystemBoundaryDimensions() {
return systemBoundaryDimensions;
}

@Override
public void setCanvasWidth(final int width) {
this.canvasWidth = width;
}

@Override
public void setCanvasHeight(final int height) {
this.canvasHeight = height;
}

@Override
public void run() throws Exception {
this.systemBoundaryDimensions = new Rect(0 + xPadding, 0 + yPadding,
Math.max(this.canvasWidth - xPadding, 0), Math.max(
this.canvasHeight - yPadding, 0));
}
}

更新 2:

截图如下:

Screenshot

更新 3 (31.03.2013 19:38 MSK):如果我从 onLayoutonSizeChanged 报告的宽度中减去 150 code>onMeasure,矩形正确显示。

更新 4(05.04.2013 21:07 MSK):这是主要 Activity 的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<fragment
android:id="@+id/menu_pane"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
class="co.altruix.ccp.android.impl.fragments.MenuFragment" />

<FrameLayout
android:id="@+id/content_fragment2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="co.altruix.ccp.android.impl.fragments.ContentFragment2"/>

</LinearLayout>

最佳答案

我看到 android:layout_height="wrap_content" 用于自定义 View 。

在这种情况下,父/容器 ViewGroup 在测量此 View 时想知道内容的高度。

但是,要绘制内容,您依赖于布局测量的尺寸,而布局仍然不知道内容的高度。

android:layout_height设置为0dp,这将允许使用android:layout_weight属性,然后View将有一个pre根据可用空间测量高度。

此外,onSizeChanged() 足以在再次测量布局时通知您尺寸变化。

关于android - Canvas 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15680744/

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