1

我正在尝试在屏幕上绘制一个椭圆形,但我得到的只是一张空白的白色画布。我不仅不确定我是否正确地绘制了椭圆,而且在屏幕上什么也没有。有谁知道为什么会这样,我认为问题在于它没有进入线程的绘图部分......

package hidden;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;

public class Surface extends Activity implements OnTouchListener
{
    SurfaceView v;

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(1);

        v = new SView(this);
        v.setOnTouchListener(this);
        v.setZOrderOnTop(true);
        setContentView(v);
    }
    public class SView extends SurfaceView implements Runnable
    {
        Thread t1 = null;
        SurfaceHolder holder;
        boolean isItOk = false;

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

            holder = getHolder();
            holder.setFormat(PixelFormat.TRANSPARENT);
        }
        public void run() 
        {
            while(isItOk)
            {
                if(!holder.getSurface().isValid())
                {
                    continue;
                }
                Canvas c = holder.lockCanvas();
                Paint p = new Paint();
                p.setColor(Color.BLUE);

                //OvalShape oval = new OvalShape();
                //oval.draw(c, p);

                Paint paint = new Paint();
                paint.setColor(Color.GREEN);
                paint.setStyle(Paint.Style.STROKE);
                float leftx = 20;
                float topy = 20;
                float rightx = 50;
                float bottomy = 100;
                RectF ovalBounds = new RectF(leftx, topy, rightx, bottomy);
                c.drawOval(ovalBounds, paint);

                holder.unlockCanvasAndPost(c);
            }
        }
        public void pause()
        {
            isItOk = false;
            while(true)
            {
                try
                {
                    t1.join();
                }
                catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
                break;
            }
            t1 = null;
        }
        public void resume()
        {
            isItOk = true;
            t1 = new Thread(this);
            t1.start();
        }
    }
    public boolean onTouch(View arg0, MotionEvent arg1)
    {
        return false;
    }
}
4

0 回答 0