gpt4 book ai didi

java - 无法创建自定义 Canvas View 的多个实例,并且无法将输入值传递给 Canvas 函数 -Android

转载 作者:行者123 更新时间:2023-12-01 11:12:07 25 4
gpt4 key购买 nike

我遇到一个问题,无法在多个位置创建自定义用户界面的多个实例。并且无法将输入发送到 Mycanvas 进行进一步的 UI 修改。

MyCanvas 类:

public class MyCanvas extends View {
private int total;
private int width;
private int height;

@Override
public void onDraw(Canvas canvas){
// Drawing my stuff here with (width and height gives me 0 here)
if(total == 50 || total == 150){ total == 100;}
}
public int addToCanvas(int value){ total += value ;}
public int subtractToCanvas(int value){ total -= value ;}

//All three of contructors here
public MyCanvas(Context context) {
super(context);
total = 100;
width = getWidth();
height = getHeight();
}

public MyCanvas(Context context, AttributeSet attrs) {
super(context, attrs);
total = 100;
width = getWidth();
height = getHeight();
}

public MyCanvas(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
total = 100;
width = getWidth();
height = getHeight();
}
}

主要 Activity 类别:

public class MainActivity extends Activity {

private MyCanvas mycanvas;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mycanvas= (MyCanvas) findViewById(R.id.customcanvas);
mycanvas.addToCanvas(10);
}
}

主要 Activity XML:

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

<Button android:id="@+id/bTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<com.example.me.MyCanvas android:id="@+id/customcanvas"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

我想要的只是在具有不同实例的多个地方使用我的默认 Canvas ,就像进度栏一样..

最佳答案

主要问题似乎是您太早尝试获取有关 View 尺寸的信息。据我所知,最好的时间是在“onDraw()”方法中分别调用“getMeasuredHeight()”和“getMeasuredWidth()”。

编辑:

为了获得一个工作示例,我通过添加 Paint 和初始化它的方法以及“onDraw()”方法中的一些代码来更改“MyCanvas”。

public class MyCanvas extends View
{
private int total;
private int width;
private int height;

private Paint paintLine;

public void init()
{
// call this from all of the constructors
paintLine = new Paint();
paintLine.setStyle(Paint.Style.STROKE); // linie
paintLine.setColor(Color.rgb(0, 40, 80));
paintLine.setStrokeWidth(2.0f); // strichbreite
paintLine.setAntiAlias(true);
}

@Override
public void onDraw(Canvas canvas){
// Drawing my stuff here with (width and height gives me 0 here)

// use 'getMeasuredWidth()' and 'getMeasuredHeight()' here!
// calling 'getWidth()' and 'getHeight()' in the constructor is too early,
// the View is not yet laid out.
width = getMeasuredWidth();
height = getMeasuredHeight();

canvas.drawCircle(width/2.0f, height/2.0f, (total / 100f) * (width + height)/8.0f, paintLine);

// this won't compile:
//if(total == 50 || total == 150){ total == 100;}
if(total == 50 || total == 150){ total = 100;}
}


public int addToCanvas(int value)
{ total += value ;
// you need to return something!
return total;
}
public int subtractToCanvas(int value)
{ total -= value ;
// same here
return total;
}

//All three of contructors here
public MyCanvas(Context context) {
super(context);
total = 100;
// width = getWidth();
// height = getHeight();
init();
}

public MyCanvas(Context context, AttributeSet attrs) {
super(context, attrs);
total = 100;
// width = getWidth();
// height = getHeight();
init();
}

public MyCanvas(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
total = 100;
// width = getWidth();
// height = getHeight();
init();
}
}

MainActivity,出于测试驱动目的而更改:

public class MainActivity extends Activity
{

private MyCanvas mycanvas;
private Button myButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myButton = (Button)findViewById(R.id.bTest);
myButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
mycanvas.addToCanvas(10);
mycanvas.invalidate();
}
});
mycanvas= (MyCanvas) findViewById(R.id.customcanvas);
mycanvas.addToCanvas(10);
}
}

关于java - 无法创建自定义 Canvas View 的多个实例,并且无法将输入值传递给 Canvas 函数 -Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32222528/

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