简体   繁体   中英

Custom View In Android using SurfaceView

I'm trying to create custom view which fill and empty by certain color but it is not working. I'm new to creating custom views programmatically, so if there may be some "common sense" mistakes.

code:

public class PitchView extends SurfaceView implements SurfaceHolder.Callback {

private int width, height;
private final Paint paint = new Paint();
private int cx;
private int cy;

public PitchView(Context context) {
    super(context);
}

public PitchView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public PitchView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    width = MeasureSpec.getSize(widthMeasureSpec);
    height = MeasureSpec.getSize(heightMeasureSpec);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    cx = 0;
    cy = 0;
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    width = w;
    height = h;
}

@Override
protected void onDraw(Canvas canvas) {
    paint.setStrokeWidth((float) width);
    paint.setColor(Color.WHITE);
    canvas.drawLine(0, 0, width, height, paint);
    paint.setColor(Color.BLUE);
    canvas.drawLine(0, 0, cx, cy, paint);
}

Thread t = new Thread(new Runnable() {

    private boolean moveUp = true;

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            while (true) {
                if (cx >= width) {
                    moveUp = false;
                } else if (cx <= 0) {
                    moveUp = true;
                }
                if (moveUp)
                    cy++;
                else
                    cy--;
                Thread.sleep(500);
                postInvalidate();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.e("PitchView Error", e.getMessage(), e);
        }
    }
});

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    t.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    t.interrupt();
}

}

You don't need to override the onDraw method, all drawing code can write in the thread, thus you can draw complex graphics without interrupting main thread. The SurfaceView is a special subclass of View that offers a dedicated drawing surface within the View hierarchy. The aim is to offer this drawing surface to an application's secondary thread, so that the application isn't required to wait until the system's View hierarchy is ready to draw. Instead, a secondary thread that has reference to a SurfaceView can draw to its own Canvas at its own pace.

Thread t = new Thread(new Runnable() {

private boolean moveUp = true;

@Override
public void run() {
    // TODO Auto-generated method stub
    try {
        while (true) {
            if (cx >= width) {
                moveUp = false;
            } else if (cx <= 0) {
                moveUp = true;
            }
            if (moveUp)
                cy++;
            else
                cy--;
                //Here you can draw graphics
                Canvas canvas = surfaceHolder.lockCanvas();
                paint.setStrokeWidth((float) width);
                paint.setColor(Color.WHITE);
                canvas.drawLine(0, 0, width, height, paint);
                paint.setColor(Color.BLUE);
                canvas.drawLine(0, 0, cx, cy, paint);
                surfaceHolder.unlockCanvasAndPost(canvas);
            Thread.sleep(500);
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.e("PitchView Error", e.getMessage(), e);
    }
}

});

private SurfaceHolder surfaceHolder = null;

@Override
public void surfaceCreated(SurfaceHolder holder) {
    surfaceHolder = holder;
    t.start();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int frmt, int w, int h) {
    surfaceHolder = holder;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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