gpt4 book ai didi

android - onDraw 在创建时不触发

转载 作者:行者123 更新时间:2023-11-30 03:47:53 29 4
gpt4 key购买 nike

我的自定义 View 的 onDraw() 没有被触发。为什么?我正在使用 2.3.3。 View 创建时不应该自动绘制吗? onMeasure() 方法被触发,我得到正确的屏幕宽度和高度。

package com.example.assignment2b;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;

public class AcesActivity extends Activity {

private static final String TAG = "DEBUG";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HeartView heartView = new HeartView(this);
setContentView(heartView);
heartView.invalidate();
}


class HeartView extends View {

private int screenWidth;
private int screenHeight;
private Context context;

public HeartView(Context context) {
super(context);
this.context = context;

}

@Override
public void onDraw(Canvas canvas) {
Log.println(Log.DEBUG, TAG, "onDraw fired");
int cardWidth = screenWidth - 10;
int cardHeight = (int) (cardWidth * 1.4);


canvas.drawColor(Color.DKGRAY);

Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(5, (screenHeight/2) - (cardHeight/2), screenWidth - 5, (screenHeight/2) + (cardHeight/2), paint);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(5, (screenHeight/2) - (cardHeight/2), screenWidth - 5, (screenHeight/2) + (cardHeight/2), paint);

}

@Override
public void onMeasure(int width, int height) {
Log.println(Log.DEBUG, TAG, "onMeasure fired");
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
setMeasuredDimension(screenWidth, screenHeight);
Log.println(Log.DEBUG, TAG, "screenWidth = " + screenWidth);
Log.println(Log.DEBUG, TAG, "scrrenHeight = " + screenHeight);
super.onMeasure(screenWidth, screenHeight);
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
}

最佳答案

一些不同的问题正在发生。首先正如@dragonwrenn 所建议的那样,如果没有 ViewGroup 和布局参数,您的 View 就没有内在边界。调用 invalidate() 是不必要的,但不会有问题。

public class AcesActivity extends Activity {

private static final String TAG = "DEBUG";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
setContentView(rl);

HeartView heartView = new HeartView(this);
heartView.setLayoutParams(params);
rl.addView(heartView);
}

class HeartView extends View {

private int screenWidth;
private int screenHeight;
private Context context;

// Moved a couple of variable declarations out of the onDraw method
Paint paint;
int cardWidth;
int cardHeight;

public HeartView(Context context) {
super(context);
this.context = context;
paint = new Paint();

}

其次,您对矩形的数学运算使它们超出了屏幕的边界。我输入了一些硬性数字作为测试。你会想要重新处理你的数字以获得你真正想要的。另外,你的笔划没有宽度,所以请注意

        @Override
public void onDraw(Canvas canvas) {

Log.println(Log.DEBUG, TAG, "onDraw fired");

canvas.drawColor(Color.DKGRAY);

paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(10, 10, 50, 50, paint);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
canvas.drawRect(10, 10, 50, 50, paint);

}

第三,我不确定 onMeasure 中的所有内容都试图实现什么,但它会导致 View 出现问题。你不应该需要比下面更多的东西:

        @Override
public void onMeasure(int width, int height) {
Log.println(Log.DEBUG, TAG, "onMeasure fired");
screenWidth = width;
screenHeight = height;

Log.println(Log.DEBUG, TAG, "screenWidth = " + screenWidth);
Log.println(Log.DEBUG, TAG, "scrrenHeight = " + screenHeight);
super.onMeasure(screenWidth, screenHeight);
cardWidth = screenWidth - 10;
cardHeight = (int) (cardWidth * 1.4);
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
}

编辑:请注意,以上是在 Eclipse 中测试的功能代码。如果每个部分都被准确地复制和粘贴,它将起作用。

关于android - onDraw 在创建时不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14658048/

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