大佬教程收集整理的这篇文章主要介绍了android使用XML布局中的View来绘制画布,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://scheR_421_11845@as.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/bTest" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <View android:id="@+id/vMain" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
这是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); } } }
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://scheR_421_11845@as.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/bTest" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <sm.view.test.TheSurface android:id="@+id/vMain" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
在你的TheSurface中
实施可忽略的例程:
public TheSurface(Context C){ super(C); // Other setup code you want here } public TheSurface(Context C,AttributeSet attribs){ super(C,attribs); // Other setup code you want here } public TheSurface(Context C,AttributeSet attribs,int defStylE){ super(C,attribs,defStylE); // Other setup code you want here } protected void onDraw(Canvas canvas){ super.onDraw(canvas); Paint textPaint = new Paint(); textPaint.setColor(Color.WHITE); canvas.drawLine(0,textPaint); // Other drawing functions here!!! }
这应该让你的绘图完成!
同样在我的情况下,您不必将其实现为SurfaceView,您可以将其实现为View,并且它不需要实现runnable!
以上是大佬教程为你收集整理的android使用XML布局中的View来绘制画布全部内容,希望文章能够帮你解决android使用XML布局中的View来绘制画布所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。