本文介绍了添加的画布 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我动态创建一个帆布:
画布油画=新的Canvas();
但我怎么可以把它添加到我的LinearLayout?
的LinearLayout LL =新的LinearLayout(本);
解决方案
您可以用一个简单的addView做到这一点,或者如果你正在做的事情更像是需要一个新的线程来完成你的图形画,那么你可以把它添加到复杂使用自定义的SurfaceView你的XML布局
< com.util.MyDraw
机器人:ID =@ + ID / surfaceView
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =10px的
机器人:layout_marginLeft =10px的
机器人:layout_marginRight =10px的
机器人:layout_below =@ + ID / spinner1
/>
然后创建一个类MyDraw延伸SurfaceView,并在那里,你可以打电话给你的主题作画。
包com.util;
公共类MyDraw扩展了SurfaceView实现回调{
私人MyThread myThread;
私人SurfaceHolder持有人;
民营涂料粉刷;
路径道路;
公共链表<整数GT;清单; {
名单=新的LinkedList<整数GT;();
}
公共MyDraw(上下文的背景下){
超(上下文);
支架= getHolder();
holder.addCallback(本);
油漆=新的油漆(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setColor(Color.GREEN);
路径=新路径();
}
公共无效surfaceCreated(SurfaceHolder持有者){
myThread =新MyThread(持有人,这一点);
myThread.setFlag(真正的);
myThread.start();
}
公共无效surfaceChanged(SurfaceHolder持有人,INT格式,诠释的宽度,高度INT){}
公共无效surfaceDestroyed(SurfaceHolder持有者){
myThread.setFlag(假);
}
@覆盖
保护无效的OnDraw(帆布油画){
path.rewind();
path.reset();
如果(帆布!= NULL){
canvas.drawColor(Color.BLACK);
如果(名单= NULL和放大器;!&安培;则为list.size()0){
path.moveTo(0,list.get(0));
INT秒;
对于(秒= 1;秒<则为list.size();秒++){
path.lineTo(秒,(list.get(秒)/ divFactor));
}
canvas.drawPath(路径,油漆);
}
}
}
I dynamically create a canvas with:
Canvas canvas = new Canvas();
But how can I add it to my LinearLayout?
LinearLayout ll = new LinearLayout(this);
解决方案
You can either do it with a simple addView or if you are doing something more complex like needing a new thread to do your graphics painting then you can add it to your xml layout with a custom SurfaceView
<com.util.MyDraw
android:id="@+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
android:layout_below="@+id/spinner1"
/>
Then create a class called MyDraw that extends SurfaceView and in there you can call your Thread to paint.
package com.util;
public class MyDraw extends SurfaceView implements Callback {
private MyThread myThread;
private SurfaceHolder holder;
private Paint paint;
Path path;
public LinkedList<Integer> list; {
list = new LinkedList<Integer>();
}
public MyDraw(Context context) {
super(context);
holder = getHolder();
holder.addCallback(this);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setColor(Color.GREEN);
path = new Path();
}
public void surfaceCreated(SurfaceHolder holder) {
myThread = new MyThread(holder, this);
myThread.setFlag(true);
myThread.start();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
public void surfaceDestroyed(SurfaceHolder holder) {
myThread.setFlag(false);
}
@Override
protected void onDraw(Canvas canvas) {
path.rewind();
path.reset();
if (canvas != null) {
canvas.drawColor(Color.BLACK);
if (list != null && list.size() > 0) {
path.moveTo(0, list.get(0));
int sec;
for(sec = 1; sec < list.size(); sec++) {
path.lineTo(sec, (list.get(sec)/divFactor));
}
canvas.drawPath(path, paint);
}
}
}
这篇关于添加的画布 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!