在Android中使用路径重复平铺图像

时间:2018-06-27 09:31:46

标签: path shader paint bitmapdrawable

I want to create frame Like this image using path 我想使用仅在水平方向上平铺的位图,有什么方法可以做到,因此它只能在X上扩展,就像在CSS上重复x一样。我使用以下代码,但是位图块为水平和垂直。首先,我为drawPath()创建了四个绘画对象。然后,我创建了四个路径对象以绘制矩形框,并希望使用路径重复图像。

Paint wallpaint = new Paint();
Shader mShader1 = new BitmapShader(rotated_bitmap_180, 
                                 Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
wallpaint.setShader(mShader1);

Paint wallpaint1 = new Paint();
mShader1 = new BitmapShader(rotated_bitmap_bottom, 
                                     Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
wallpaint1.setShader(mShader1);


Paint wallpaint3 = new Paint();
mShader1 = new BitmapShader(rotated_bitmap_90, Shader.TileMode.REPEAT, 
                                        Shader.TileMode.REPEAT);
wallpaint3.setShader(mShader1);
wallpaint3.setStrokeWidth((int) frameWidth);


Paint wallpaint2 = new Paint();
mShader1 = new BitmapShader(rotated_bitmap_270, Shader.TileMode.REPEAT, 
                                          Shader.TileMode.REPEAT);
wallpaint2.setShader(mShader1);


Path wallpath1 = new Path();
wallpath1.moveTo(0, 0);
wallpath1.lineTo((float) frame_area_width, 0); // used for first point
wallpath1.lineTo((float) (frame_area_width - (frameWidth * ppi)), (float) 
                                     (frameWidth * ppi));
wallpath1.lineTo((float) (frameWidth * ppi), (float) (frameWidth*ppi));
wallpath1.close();

Path wallpath2 = new Path();
wallpath2.moveTo(0, (float) frame_area_height);
wallpath2.lineTo((float) (frameWidth * ppi), (float) (frame_area_height - 
                              (frameWidth * ppi))); // used for first point
wallpath2.lineTo((float) (frame_area_width - (frameWidth * ppi)), (float) 
                               (frame_area_height - (frameWidth * ppi)));
wallpath2.lineTo((float) frame_area_width, (float) frame_area_height);
wallpath2.close();

Path wallpath3 = new Path();
wallpath3.moveTo(0, 0);
wallpath3.lineTo((float) (frameWidth * ppi), (float) (frameWidth * ppi)); 
wallpath3.lineTo((float) (frameWidth * ppi), (float) (frame_area_height - 
                             (frameWidth * ppi)));
wallpath3.lineTo(0, (float) frame_area_height);
wallpath3.close();

Path wallpath4 = new Path();
wallpath4.moveTo((float) (frame_area_width - (frameWidth * ppi)), (float) 
                                       (frameWidth * ppi));
wallpath4.lineTo((float) frame_area_width, 0); // used for first point
wallpath4.lineTo((float) frame_area_width, (float) frame_area_height);
wallpath4.lineTo((float) (frame_area_width - (frameWidth * ppi)), (float) 
                              (frame_area_height - (frameWidth * ppi)));
wallpath4.close();

canvas.drawPath(wallpath1, wallpaint);
canvas.drawPath(wallpath2, wallpaint1);
canvas.drawPath(wallpath3, wallpaint2);
canvas.drawPath(wallpath4, wallpaint3);

0 个答案:

没有答案