如何在android中制作Canvas视图的一部分?

时间:2012-01-01 04:39:59

标签: android image canvas

我正在开发一个具有触摸画布的代码,但代码是单独的&我希望canvas成为xml的一部分,其中包含其他imageview和其他。这是我的代码

public class Tutorial2D extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(new Panel(this));
}

}

这是另一部分

public class Panel extends SurfaceView implements SurfaceHolder.Callback {

private ViewThread mThread;
private ArrayList<Element> mElements = new ArrayList<Element>();

public Panel(Context context) {
    super(context);
    getHolder().addCallback(this);
    mThread = new ViewThread(this);
}

public void doDraw(Canvas canvas) {
    canvas.drawColor(Color.TRANSPARENT);
    synchronized (mElements) {
        for (Element element : mElements) {
            element.doDraw(canvas);
        }
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    // TODO Auto-generated method stub
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    if (!mThread.isAlive()) {
        mThread = new ViewThread(this);
        mThread.setRunning(true);
        mThread.start();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    if (mThread.isAlive()) {
        mThread.setRunning(false);
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    synchronized (mElements) {
        mElements.add(new Element(getResources(), (int) event.getX(), (int) event.getY()));
    }
    return super.onTouchEvent(event);
}

}

如何制作xml的画布部分,对此的任何片段都会非常有帮助。谢谢

0 个答案:

没有答案