首先,ShapeDrawable構(gòu)造的時(shí)候可以指定描畫的形狀,
其次,可以通過(guò)shape.getPaint().setShader();指定Shader,shader可以接受一個(gè)圖片和matrix
所以問(wèn)題就順利的解決了:)
具體實(shí)現(xiàn)如下:
[java]
float scale = 1.2f;
int cx = 224;
int cy = 357;
int r = 200;
// 指定形狀創(chuàng)建一個(gè)ShapeDrawable
ShapeDrawable shape=new ShapeDrawable(new OvalShape());
Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.bg1);
BitmapShader bs = new BitmapShader(bm, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Matrix m = new Matrix();
m.setTranslate(r-cx, r-cy);
m.postScale(scale, scale);
bs.setLocalMatrix(m); // 圖形變換可以在這里實(shí)現(xiàn),包括區(qū)域指定
// 為ShapeDrawable設(shè)置Shader
shape.getPaint().setShader(bs);
// 指定描畫目標(biāo)位置
shape.setBounds((int)(cx-r*scale),(int)(cy-r*scale),(int)(cx+r*scale),(int)(cy+r*scale));
canvas.drawBitmap(bm, 0, 0, null);
shape.draw(canvas);
新聞熱點(diǎn)
疑難解答
圖片精選