Android 中的视图占据整个屏幕 - 如何告诉画布尊重视图的限制?
编辑:
好的,所以我已经将视图的长度和宽度扩展到屏幕的很大一部分,我可以假设 onDraw() 方法没有考虑视图的限制,因为它会遍历所有屏幕我可以假设。所以现在的问题是如何告诉画布尊重视图的限制?有什么想法,也许强制一些参数?
编辑结束
我已经实现了一些用于绘制自定义视图的代码,但画布是空白的。我确信它正在运行,因为我已经添加了日志跟踪,并且这些显示得很好。
如何正确实现自定义视图的绘图,以及我下面所做的有什么问题?
我的自定义视图位于如下布局中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<pis.core.JMI_VistaMenuIn android:layout_height="150dip" android:layout_width="150dip" android:layout_alignParentTop="true" android:id="@+id/viewAnd01" ></pis.core.JMI_VistaMenuIn>
<Button android:layout_width="wrap_content" android:id="@+id/btnJugar" android:layout_height="wrap_content" android:text="Jugar" android:layout_below="@+id/viewAnd01" android:layout_alignLeft="@+id/viewAnd01" android:layout_alignRight="@+id/viewAnd01"></Button>
</RelativeLayout>
自定义视图称为:pis.core .JMI_VistaMenuIn。在预览中,我收到了“java.lang.UnsupportedOperationException”,但是当加载 .apk 时,xml 布局可以工作。
这是我的自定义视图的代码。正如我之前所说,包含日志记录的行受到攻击,但我的画布是空白的:
public class JMI_VistaMenuIn extends View {
public static final String QUOTE = "Mind Logic+";
public Animation anim;
//Default constructors
public void createAnim(Canvas canvas) {
Log.i("SC", "Exe canvas 01");
anim = new RotateAnimation(0, 360, canvas.getWidth() / 2, canvas
.getHeight() / 2);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(10000L);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
startAnimation(anim);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
// creates the animation the first time
if (anim == null) {
createAnim(canvas);
}
Path circle = new Path();
int centerX = canvas.getWidth() / 2;
int centerY = canvas.getHeight() / 2;
int r = Math.min(centerX, centerY);
circle.addCircle(centerX, centerY, r, Direction.CW);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(30);
paint.setAntiAlias(true);
canvas.drawTextOnPath(QUOTE, circle, 0, 30, paint);
Log.i("SC", "Exe canvas 02");
}
}
EDIT:
OK, so I've extended the lenght and the width of the view to a big part of the screen I can assume that the onDraw() method is NOT TAKING the limits of the view, because it goes through all the screen as I can suppose. So the question now would be how to tell the canvas to respect the limits of the view? Any ideas, maybe forcing some parameters?
END OF EDIT
I have implemented some code for drawing a custom view, but the canvas is blank. I am sure it is running, because I have added log traces, and those are showing up just fine.
How do I properly implement drawing for custom views, and what is wrong with what I've done below?
My custom view that goes inside a layout like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<pis.core.JMI_VistaMenuIn android:layout_height="150dip" android:layout_width="150dip" android:layout_alignParentTop="true" android:id="@+id/viewAnd01" ></pis.core.JMI_VistaMenuIn>
<Button android:layout_width="wrap_content" android:id="@+id/btnJugar" android:layout_height="wrap_content" android:text="Jugar" android:layout_below="@+id/viewAnd01" android:layout_alignLeft="@+id/viewAnd01" android:layout_alignRight="@+id/viewAnd01"></Button>
</RelativeLayout>
The customview is the one called: pis.core.JMI_VistaMenuIn. In the preview I got an "java.lang.UnsupportedOperationException" but when the .apk is loaded the xml layout WORKS.
Here is the code for my custom view. As I said before, the lines that contain the logging are getting hit, but my canvas is blank:
public class JMI_VistaMenuIn extends View {
public static final String QUOTE = "Mind Logic+";
public Animation anim;
//Default constructors
public void createAnim(Canvas canvas) {
Log.i("SC", "Exe canvas 01");
anim = new RotateAnimation(0, 360, canvas.getWidth() / 2, canvas
.getHeight() / 2);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(10000L);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
startAnimation(anim);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
// creates the animation the first time
if (anim == null) {
createAnim(canvas);
}
Path circle = new Path();
int centerX = canvas.getWidth() / 2;
int centerY = canvas.getHeight() / 2;
int r = Math.min(centerX, centerY);
circle.addCircle(centerX, centerY, r, Direction.CW);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(30);
paint.setAntiAlias(true);
canvas.drawTextOnPath(QUOTE, circle, 0, 30, paint);
Log.i("SC", "Exe canvas 02");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类似的代码对我来说效果很好,包括动画。您的代码中的其他地方还存在另一个问题:尝试设置绘画样式。
Similar code works just fine for me, including the animation. You have another issue somewhere else in your code: try setting the paint style.