需要在自定义视图上绘图的帮助
我需要你的经验。 问题:我需要能够在 FlipperView 的一部分上绘制东西(矩形、圆形等)...... 我的 main.xml 有一个主要的 LinearLayout。在这个 LinearLayout 中,我有一个 ViewFlipper,其中有 2 个线性布局。第一个线性布局有一些按钮、输入字段等...第二个线性布局应该有一个特殊的视图,我可以在其中绘制我在第一部分中选择的内容。 所以我创建了一个新视图,它扩展了 View 类,这样我就可以使用 ondraw 方法。但我无法让它发挥作用。 这就是我到目前为止所拥有的...... MAIN.XML
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/layout_main" xmlns:android="http://schemas.android.com/apk/res/android">
<ViewFlipper android:id="@+id/details" android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="match_parent">
//BUTTONS TEXTFIELDS ETC
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="match_parent">
//我的新 ViewClass 的实例
<Vierbergen.Tim.ViewClass
android:id="@+id/draw" android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
VIEWCLASS.java <代码>
public class ViewClass extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
paint.setColor(Color.WHITE);
}
@Override
public void onDraw(Canvas canvas) {
//depending on some params....
draw this, draw that...
}
<代码>}
然后是我的主要活动 DRAWER.JAVA
public class SmsDraw extends Activity implements OnTouchListener{
ViewClass vClass;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vClass = (ViewClass) findViewById(R.id.draw);
}
// 某处有一个由按钮执行的绘图函数 私有无效开始(){ //我在哪里可以获得画布?画布 c = new Canvas(); 布拉布拉尔巴 vClass.onDraw(c); }
所以我需要能够在 main.xml 中使用 id = draw 来绘制 VIEWCLASS 的内容... 我该怎么做?请帮助我解释和解决方案,而不仅仅是解决方案:-)
谢谢 VeeTee
I need your experience.
Problem : I need to be able to draw thing (rect, circ,etc) on one part of a FlipperView....
My main.xml has a main linearLayout. In this LinearLayout I have a ViewFlipper with 2 linearlayouts in it. The first linearlayout has soms buttons, inputfiels,etc... the second one should have a special view in wich I can draw the things I choose in the first part.
So I have created a new view wich extends the View class so I can play with the ondraw methode. But I can not get it to work.
This is what I have so far...
MAIN.XML
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/layout_main" xmlns:android="http://schemas.android.com/apk/res/android">
<ViewFlipper android:id="@+id/details" android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="match_parent">
//BUTTONS TEXTFIELDS ETC
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="match_parent">
//an instance of my new ViewClass
<Vierbergen.Tim.ViewClass
android:id="@+id/draw" android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
The VIEWCLASS.java
public class ViewClass extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
paint.setColor(Color.WHITE);
}
@Override
public void onDraw(Canvas canvas) {
//depending on some params....
draw this, draw that...
}
}
and then my main activity
DRAWER.JAVA
public class SmsDraw extends Activity implements OnTouchListener{
ViewClass vClass;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vClass = (ViewClass) findViewById(R.id.draw);
}
// with somewhere a draw function excecuted by a button
private void start() {
//where can I get a canvas ? Canvas c = new Canvas();
blablalba
vClass.onDraw(c);
}
So I need to be able to draw on the thing VIEWCLASS with id = draw in my main.xml...
How can I do this ? please help me with an explanation and solution and not just a solution :-)
Thanks VeeTee
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的
View
附加到视图层次结构,则框架将调用您的onDraw
方法。你不需要自己调用它。如果您不确定
onDraw
代码,请尝试使用 DrawPoints。Your
onDraw
method will be called by the framework if yourView
is attached to the view hierarchy. You don't need to call it yourself.If you're unsure about your
onDraw
code, try using the code from a sample like DrawPoints in API Demos.正如马修所说,你不会自己调用 onDraw 。这是对 ViewClass 的一个非常简单的添加,它允许您绘制矩形或圆形。它尚未经过测试,因此请小心行事。
然后在你的视图类中,这样调用它:
You don't call onDraw yourself, as Matthew has said. Here's a very simple addition to your ViewClass that would allow you to draw rectangles or circles. It hasn't been tested, so proceed carefully.
Then in your view Class, call it like this:
似乎第一次不起作用的问题是......
当主要活动想要设置ContentView(...)...它崩溃...
但是当我把它排除在 xml 之外时
并在运行时创建它,如下所示
viewClass = new ViewClass(this);
有用....
Seems like the problem it wasn't working the first time was...
when the main activity want to setContentView(...)... it crashes....
But when I leave it out of the xml
and create it at runtime like this
viewClass = new ViewClass(this);
it works....