سؤال

I want to create a screen like this:

image of the requirement

I have to divide width and height into 8 equal parts with lines (if possible, dotted lines). How to do that?

Here is my current code:

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);

    drawView = new DrawView(this);
    drawView.setBackgroundColor(Color.WHITE);
    setContentView(drawView);
    // setContentView(R.layout.activity_main);

    layout = (LinearLayout) findViewById(R.id.load);

    display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    width = size.x;
    height = size.y;

    equal_parts_width = width/8;
    equal_parts_height = width/8;

    Log.i("", width + " <==> " + height + " == "
        + equal_parts_width + "==" +equal_parts_height );   
}

public class DrawView extends View {
    Paint paint = new Paint();  

    public DrawView(Context context) {
        super(context);         
        paint.setColor(Color.GREEN);
    }

        @Override
        public void onDraw(Canvas canvas) {
            for(int i = 0 ; i < 7 ; i++) {
                canvas.drawLine(0, equal_parts_height*i,
                    width, equal_parts_height*i,  paint);
                canvas.drawLine(equal_parts_width*i, 0,
                    height, equal_parts_width*i,  paint);
            }        
        }
    }
}
هل كانت مفيدة؟

المحلول

Use Paint.setpathEffect(PathEffect effect) with DashPathEffect.

From docs: The intervals array must contain an even number of entries (>=2), with the even indices specifying the "on" intervals, and the odd indices specifying the "off" intervals.

paint.setPathEffect(new DashPathEffect(new float[]{on, off}, 0));

And add this to your DrawView constructor

public DrawView(Context context) {
    super(context);         
    paint.setColor(Color.GREEN);
    paint.setPathEffect(new DashPathEffect(new float[]{on, off}, 0));
}

EDIT complete code..

public class DrawView extends View {
    Paint paint = new Paint();  

    public DrawView(Context context) {
        super(context);         
        paint.setColor(Color.GREEN);
        paint.setStyle(Style.STROKE);
    }

    @Override
    public void onDraw(Canvas canvas) {  
        float width = canvas.getWidth();
        float height = canvas.getHeight();
        float size = height/(7*2); //just use 14 it's for convenience here
        paint.setStrokeWidth(WIDTH);
        paint.setPathEffect(new DashPathEffect(new float[]{size, size}, 0));
        for(int i = 0 ; i < 7 ; i++) {
            canvas.drawLine(0, size*i,
                width, size*i,  paint);
            canvas.drawLine(size*i, 0,
                height, size*i,  paint);
        }        
    }

}

you should be able to take this and simply alter it to fit you needs. You can also create rounded dots with this code.

 paint.setStrokeJoin(Paint.Join.ROUND);
 paint.setStrokeCap(Paint.Cap.ROUND);
 paint.setDither(true);
 paint.setAntiAlias(true);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top