그래픽 그리는 샘플들 안드로이드(android, java)



private void drawOvalAndArrow(Canvas canvas){


    Paint circlePaint = new Paint();
    circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
    circlePaint.setAntiAlias(true);
    circlePaint.setStrokeWidth(2);
    circlePaint.setColor(Color.CYAN);

    float centerWidth = canvas.getWidth()/2; //get center x of display
    float centerHeight = canvas.getHeight()/2; //get center y of display
    float circleRadius = 20; //set radius 
    float circleDistance = 200; //set distance between both circles

    //draw circles
    canvas.drawCircle(centerWidth, centerHeight, circleRadius, circlePaint);
    canvas.drawCircle(centerWidth+circleDistance, centerHeight, circleRadius, circlePaint);


    //to draw an arrow, just lines needed, so style is only STROKE
    circlePaint.setStyle(Paint.Style.STROKE);       
    circlePaint.setColor(Color.RED);

    //create a path to draw on
    Path arrowPath = new Path();

    //create an invisible oval. the oval is for "behind the scenes" ,to set the path´
    //area. Imagine this is an egg behind your circles. the circles are in the middle of this egg
    final RectF arrowOval = new RectF();
    arrowOval.set(centerWidth, 
            centerHeight-80, 
            centerWidth + circleDistance, 
            centerHeight+80);

    //add the oval to path
    arrowPath.addArc(arrowOval,-180,180);

    //draw path on canvas
    canvas.drawPath(arrowPath, circlePaint);


    //draw arrowhead on path start
     arrowPath.moveTo(centerWidth,centerHeight ); //move to the center of first circle
     arrowPath.lineTo(centerWidth-circleRadius, centerHeight-circleRadius);//draw the first arrowhead line to the left
     arrowPath.moveTo(centerWidth,centerHeight );//move back to the center
     arrowPath.lineTo(centerWidth+circleRadius, centerHeight-circleRadius);//draw the next arrowhead line to the right

     //same as above on path end
     arrowPath.moveTo(centerWidth+circleDistance,centerHeight );
     arrowPath.lineTo((centerWidth+circleDistance)-circleRadius, centerHeight-circleRadius);
     arrowPath.moveTo(centerWidth+circleDistance,centerHeight );
     arrowPath.lineTo((centerWidth+circleDistance)+circleRadius, centerHeight-circleRadius);

     //draw the path
     canvas.drawPath(arrowPath,circlePaint);

}

--------4

public class ArrowView extends View {

    private int width;
    private int height;
    private List<Float> angles = new LinkedList<Float>();
    private List<Integer> colors = new LinkedList<Integer>();
    private float scale = 1f;

    private static final Path path = new Path();
    private static final Paint arrowStroke = new Paint();

    static {
        path.moveTo(0f, -5f);
        path.lineTo(-2f, 1f);
        path.lineTo(2f, 1f);
        path.close();

        arrowStroke.setStyle(Style.STROKE);
        arrowStroke.setColor(Color.WHITE);
        arrowStroke.setStrokeWidth(0.1f);
        arrowStroke.setAntiAlias(true);
    }

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

    @Override
    protected void onDraw(final Canvas canvas) {

        for (int i = 0; i < angles.size(); i++) {

            float angle = angles.get(i);
            int color = colors.get(i);

            final Paint arrowPaint = new Paint();
            arrowPaint.setColor(color);
            arrowPaint.setAntiAlias(true);

            canvas.translate(width / 2f, height / 2f);
            canvas.rotate(angle);
            canvas.scale(width / 10f * scale, height / 10f * scale);
            canvas.drawPath(path, arrowPaint);
            canvas.drawPath(path, arrowStroke);
        }
    }

    public void addArrow(final float angle, final int color) {
        angles.add(angle);
        colors.add(color);
        postInvalidate();
    }

    public void removeArrow(final int color) {
        final int i = colors.indexOf(color);
        angles.remove(i);
        colors.remove(i);
        postInvalidate();
    }

    public void setScale(final float scale) {
        this.scale = scale;
        postInvalidate();
    }

    @Override
    protected void onMeasure(final int wMeasureSpec, final int hMeasureSpec) {
        final int wMode = MeasureSpec.getMode(wMeasureSpec);
        final int wSize = MeasureSpec.getSize(wMeasureSpec);

        if (wMode == MeasureSpec.EXACTLY)
            width = wSize;
        else if (wMode == MeasureSpec.AT_MOST)
            width = Math.min(width, wSize);

        final int hMode = MeasureSpec.getMode(hMeasureSpec);
        final int hSize = MeasureSpec.getSize(hMeasureSpec);

        if (hMode == MeasureSpec.EXACTLY)
            height = hSize;
        else if (hMode == MeasureSpec.AT_MOST)
            height = Math.min(height, hSize);

        setMeasuredDimension(this.width, this.height);
    }
}



구글광고