android画布xml,android使用XML布局中的View来绘制画布

所以基本上我想使用xml布局,但我也想要一个画布,我可以在其中执行图形.我所做的是在我的xml布局中查看,如下所示.然后在我的应用程序中我使视图绘制画布,但它不起作用.我不确定我解决这个问题的方法是完全错误还是什么.所以,请看看我的代码并告诉我你是否看到了快速修复或者你有更好的方法.在此先感谢,我真的很感激.

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/bTest"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button" />

android:id="@+id/vMain"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

这是xml布局

package sm.view.test;

import android.app.Activity;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Paint.Align;

import android.os.Bundle;

import android.view.MotionEvent;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.view.View;

import android.view.View.OnTouchListener;

import android.widget.Button;

public class ViewActivity extends Activity implements OnTouchListener {

/** Called when the activity is first created. */

View v;

Button b;

boolean isRun =true;

SurfaceHolder ourHolder;

Thread ourThread;

Canvas canvas;

boolean isTure = true;

TheSurface ourSurfaceView;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

b= (Button) findViewById(R.id.bTest);

v = (View) findViewById(R.id.vMain);

canvas = new Canvas();

ourSurfaceView = new TheSurface(this);

ourSurfaceView.setOnTouchListener(this);

v.draw(canvas);

// v.setBackgroundColor(Color.BLUE);

}

protected void onPause() {

// TODO Auto-generated method stub

super.onPause();

ourSurfaceView.pause();

}

@Override

protected void onResume() {

// TODO Auto-generated method stub

super.onResume();

ourSurfaceView.resume();

}

public boolean onTouch(View arg0,MotionEvent arg1) {

// TODO Auto-generated method stub

return false;

}

public class TheSurface extends SurfaceView implements Runnable{

public TheSurface(Context context) {

super(context);

ourHolder= getHolder();

}

public void resume(){

isRun= true;

ourThread = new Thread(this);

ourThread.start();

}

public void pause(){

isRun = false;

while(true){

try {

ourThread.join();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

break;

}

ourThread= null;

}

public void run() {

// TODO Auto-generated method stub

Paint textPaint = new Paint();

textPaint.setColor(Color.WHITE);

while(isTure){

if(!ourHolder.getSurface().isValid())

continue;

//v.draw(canvas);

canvas = ourHolder.lockCanvas();

canvas.drawLine(0,canvas.getWidth(),canvas.getHeight(),textPaint);

ourHolder.unlockCanvasAndPost(canvas);

v.draw(canvas);

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值