简体   繁体   English

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

[英]android use a View from a XML layout to draw a canvas

So basically i want to use a xml layout, but i also want a canvas where i can have graphics performed. 所以基本上我想使用xml布局,但我也想要一个画布,我可以在其中执行图形。 What i did was make a view in my xml layout as you can see below. 我所做的是在我的xml布局中查看,如下所示。 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>

that is the xml layout 这是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);
        }
    }

}

} }

Start here (and this needs your input as well for the namespace portion "yourProjectNamespace"): 从这里开始(这也需要你的输入以及命名空间部分“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> 

In your TheSurface 在你的TheSurface中

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!!! 这应该让你的绘图完成!

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!!! 同样在我的情况下,您不必将其实现为SurfaceView,您可以将其实现为View,并且它不需要实现runnable!

I'm not 100% sure I understand what you are trying to do, but based on the fact that you don't seem to be doing anything with the canvas after you call View.draw() I believe you may be confused. 我并不是100%确定我理解你要做的事情,但是基于你在调用View.draw()后似乎没有对画布做任何事情这一事实我相信你可能会感到困惑。 View.draw(Canvas) draws the View onto the Canvas, it doesn't alter the view. View.draw(Canvas)将View绘制到Canvas上,它不会改变视图。

However, if you create the canvas from a bitmap you could then set the bitmap as an ImageView's image: 但是,如果从位图创建画布,则可以将位图设置为ImageView的图像:

Bitmap bm = Bitmap.createBitmap( x-size, y-size, Config.ARGB_8888);
Canvas c = new Canvas(bm);

Paint textPaint = new Paint();
textPaint.setColor(Color.WHITE);
canvas.drawLine(0, 0, canvas.getWidth(), canvas.getHeight(), textPaint);

ImageView iView = (ImageView) view;
iView.setImageBitmap(bm);

However, this is a less correct way than to implement your own View: 但是,这是一种不太正确的方式,而不是实现自己的视图:

button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            ((MyView)view).mContextVariable = true; //or false, etc
            //you might not need this invalidate, because the click event probably causes and invalidate to be called
            view.invalidate();
        }
    }

    class MyView extends View
    {
        Paint myPaint;
        boolean mContextVariable;

        public MyView(Context context) 
        {
            super(context);

             textPaint = new Paint();
             textPaint.setColor(Color.WHITE);

        }

        @Override
        protected void onDraw(Canvas canvas)
        {
            if(mContextVariable)
            {
                canvas.drawLine(0, 0, canvas.getWidth(), canvas.getHeight(), textPaint);
            }
            else
            {
                //draw something else
            }
            canvas.drawText("testing", 0,0, textPaint);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM