我创建了一个从视图类扩展的自定义视图。我打电话就可以了
setContentView(new Customview());
但是它不会从xml布局文件中加载。我不确定我在做什么错。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ThumbnailTest">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="36dp"
android:layout_marginTop="16dp"
android:text="Hello World!"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.example.christopher.thumbnailtest.ThumbnailFrame
class="com.example.christopher.thumbnailtest.ThumbnailFrame"
id="@+id/ThumbnailFrame"
android:layout_width="265dp"
android:layout_height="230dp"
android:layout_marginStart="60dp"
android:layout_marginTop="112dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
这只是一个简单的圆圈,这就是我要绘制的全部。
编辑:这是自定义视图类。现在,视图类被调用,但是圆圈没有在屏幕上打印圆圈。 drawCircle()
方法的前两个参数非常高(从调试日志WHAT中报告),但是我将数字除以降低它们,但是它们仍然没有显示任何内容。就像该函数在从布局文件中将其作为视图加载时不会在画布上绘制对象。
public class ThumbnailFrame extends View {
private Paint thumbnailPaint;
private Canvas thumbnailCanvas;
private float xMax;
private float yMax;
private float xMin;
private float yMin;
private float PositX;
private float PositY;
private float radius;
public ThumbnailFrame(Context context, AttributeSet attrs) {
super(context, attrs);
thumbnailPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
thumbnailPaint.setStyle(Paint.Style.STROKE);
thumbnailPaint.setColor(Color.YELLOW);
//Default radius
radius = 20;
}
@Override
protected void onSizeChanged(int w,
int h,
int oldw,
int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Log.d("FRAME_CALLED", "I'm called!");
float xpad = (float)(getPaddingLeft() + getPaddingRight());
float ypad = (float)(getPaddingTop() + getPaddingBottom());
float ww = (float)w - xpad;
float hh = (float)h - ypad;
xMax = ww - radius;
xMin = ww + radius;
yMax = hh - radius;
yMin = hh + radius;
PositX = ww/2;
PositY = hh/2;
}
public void redraw() {
Log.d("THUMBNAIL_CALLED", "Am I called?");
//Make sure our circle does not leave the bounds of our screen.
if (PositX > xMax) {
PositX = xMax;
}
if (PositX < xMin) {
PositX = xMin;
}
if (PositY > yMax) {
PositY = yMax;
}
if (PositY < yMin) {
PositY = yMin;
}
Log.d("WHAT", String.valueOf(PositX) + ":" + String.valueOf(PositY) + ":" + String.valueOf(radius) + ":" + thumbnailPaint.toString());
thumbnailCanvas.drawCircle(PositX, PositY, 100, thumbnailPaint);
}
public void setRadius(float value) {
radius = value;
}
public void setFrameX(float x) {
PositX = x;
}
public void setFrameY(float y) {
PositY = y;
}
public void removeFrame() {
thumbnailCanvas.save();
thumbnailCanvas.restore();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.d("THUMBNAIL_CALLED", "Am I called?");
thumbnailCanvas = canvas;
redraw();
}
}
答案 0 :(得分:0)
您的代码按您编写的方式工作。
您只是看不到黄色,因为它的宽度很小。
我建议在构造函数中添加thumbnailPaint.setStrokeWidth(10f/*or what you want*/);
:
public ThumbnailFrame(Context context, AttributeSet attrs) {
super(context, attrs);
thumbnailPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
thumbnailPaint.setStyle(Paint.Style.STROKE);
thumbnailPaint.setColor(Color.YELLOW);
thumbnailPaint.setStrokeWidth(10f);
//Default radius
radius = 20;
}
您还应该从以下位置更改布局
<com.example.christopher.thumbnailtest.ThumbnailFrame
class="com.example.christopher.thumbnailtest.ThumbnailFrame"
id="@+id/ThumbnailFrame"
android:layout_width="265dp"
android:layout_height="230dp"
android:layout_marginStart="60dp"
android:layout_marginTop="112dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
收件人:
<com.example.christopher.thumbnailtest.ThumbnailFrame
android:id="@+id/ThumbnailFrame"
android:layout_width="265dp"
android:layout_height="230dp"
android:layout_marginStart="60dp"
android:layout_marginTop="112dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
此外,您应该致电setContentView(R.layout.activity_main/*your id*/);
而不是setContentView(new Customview());
之后,您可以看到: