android - Applying a BitmapShader using relative coordinates when using canvas.drawBitmap -
i'm trying use bitmapshader draw on canvas. have 2 images of same dimensions (one alfa channel , 1 rgb), want draw on canvas created same size. problem seem though canvas.drawbitmap users absolute coordinates paint. there way draw relative coordinates.
public class maskedimagedrawable extends drawable { private final bitmap mmask; private final paint mpaint = new paint(); public maskedimagedrawable(bitmap mask, bitmap image) { mmask = mask; shader targetshader = new bitmapshader(image, shader.tilemode.clamp, shader.tilemode.clamp); mpaint.setshader(targetshader); } @override public void draw(canvas canvas) { canvas.drawbitmap(mmask, 0.0f, 0.0f, mpaint); } @override public int getopacity() { return pixelformat.translucent; } @override public void setalpha(int alpha) { mpaint.setalpha(alpha); } @override public void setcolorfilter(colorfilter cf) { mpaint.setcolorfilter(cf); } }
i can't seem able drawable's absolute coordinates on screen. know can use getlocationonscreen/inwindow view holds drawable doesn't take animations account.
i tried tried applying relative coordinates applying translation on shader in onboundschange coordinates here seems relative well.
matrix mtranslationmatrix = new matrix() @override protected void onboundschange(rect bounds) { super.onboundschange(bounds); mtranslationmatrix.settranslate(bounds.left, bounds.top); mpaint.getshader().setlocalmatrix(mtranslationmatrix); }
as reference - seems drawrect same paint use relative coordinates. replacing draw method following render rgb image on drawable correctly (but not mask)
@override public void draw(canvas canvas) { // getbounds returns relative coordinates canvas.drawrect(getbounds, mpaint); }
it seems hw acceleration issue. disabling hw acceleration on view moves shader relative coordinates.
if (android.os.build.version.sdk_int >= 11) { myview.disablehwacceleration(); }
Comments
Post a Comment