Android的使用View从XML布局来绘制画布 [英] android use a View from a XML layout to draw a canvas

查看:151
本文介绍了Android的使用View从XML布局来绘制画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我想使用一个XML布局,但我也希望有一个画布,我可以有执行图形。我所做的是使我的XML布局视图,你可以看到下面。然后在我的应用程序,我所做的视图中绘制在画布上,但它不工作。我不知道如果我的解决方法,这是完全错误还是什么。所以,请看看我的code,并告诉我,如果你看到一个快速解决方案,或者如果你有更好的方法。在此先感谢我真的AP preciate它。

So basically i want to use a xml layout, but i also want a canvas where i can have graphics performed. What i did was make a view in my xml layout as you can see below. Then in my application i made the view draw the canvas, but it is not working. I'm not sure if my method for solving this is completely wrong or what. So please just take a look at my code and tell me if you see a quick fix or if you have a better method. Thanks in advance I really appreciate it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.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, 0, canvas.getWidth(), canvas.getHeight(), textPaint);
        ourHolder.unlockCanvasAndPost(canvas);
        v.draw(canvas);
        }
    }

}

}

推荐答案

从这里开始(这需要你输入以及用于命名空间部分yourProjectNamespace):

Start here (and this needs your input as well for the namespace portion "yourProjectNamespace"):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.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

In your TheSurface

实施overideable套路:

Implement the overideable routines:

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, 0, canvas.getWidth(), canvas.getHeight(), textPaint);

    // Other drawing functions here!!!
}

这应该得到您的绘图完成!

This should get your drawing done!!!

另外,在我的情况下,你没有实现这个作为一个SurfaceView,你可以只实现它作为一个视图,它并不需要实现Runnable!

Also in my case, you dont have to implement this as a SurfaceView, you could just implement it as a View, and it does not need to implement runnable!!!

这篇关于Android的使用View从XML布局来绘制画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆