首先,ShapeDrawable構造的時候可以指定描畫的形狀,
其次,可以通過shape.getPaint().setShader();指定Shader,shader可以接受一個圖片和matrix
所以問題就順利的解決了:)
具體實現如下:
[java]
複製代碼 代碼如下:float scale = 1.2f;
int cx = 224;
int cy = 357;
int r = 200;
// 指定形狀建立一個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); // 圖形變換可以在這裡實現,包括地區指定
// 為ShapeDrawable設定Shader
shape.getPaint().setShader(bs);
// 指定描畫目標位置
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);
float scale = 1.2f;
int cx = 224;
int cy = 357;
int r = 200;
// 指定形狀建立一個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); // 圖形變換可以在這裡實現,包括地區指定
// 為ShapeDrawable設定Shader
shape.getPaint().setShader(bs);
// 指定描畫目標位置
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);