我的自定义视图的onDraw()未被触发。为什么?我正在使用2.3.3。视图创建时是否应自动绘制?正在触发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;
}
}
答案 0 :(得分:1)
正在发生一些不同的问题。首先,正如@dragonwrenn建议的那样,没有ViewGroup和布局参数,您的视图没有内在界限。拨打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中的所有内容是否都在尝试实现,但这会导致视图出现问题。你不应该需要比以下更多的东西:
@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中测试的功能代码。如果准确地复制和粘贴每个部分,它将起作用。
答案 1 :(得分:0)
在onMeasure()
方法中执行以下更改:
在您的班级中声明显示指标。
private DisplayMetrics m_metrics;
@Override public void onMeasure(int width, int height) { Log.println(Log.DEBUG, TAG, "onMeasure fired"); Display m_display = getWindowManager().getDefaultDisplay(); m_metrics = context.getResources().getDisplayMetrics(); m_display.getMetrics(m_metrics); screenWidth = m_display.getWidth(); screenHeight = m_display.getHeight(); super.onMeasure(screenWidth, screenHeight); }