Daniil Orekhov Daniil Orekhov - 1 year ago 63
Java Question

Android: making offscreen bitmap visible when moved

I need to start activity with two circular bitmaps in the center, that should instantly animate and move slightly to the side, such that a part of one bitmap is offscreen and not visible. Later on, when the user leaves the activity, it should animate the same way but backwards (move from side to center).

So I've implemented a class that draws these bitmaps in their main location (on the side), and the problem I have right now is that when I am animating those, the offscreen part of the bitmap is never shown, so on my screen I see an ugly moving bitmap that is missing a piece.

This is how the bitmaps are drawn:

public class MyImageView extends ImageView {

private Canvas offscreen;

private static Bitmap pattern;
private static Bitmap logo;

private static int screenWidth, screenHeight; //The real size of the screen
private static int patternDiameter, logoDiameter; //bitmaps' dimensions, both images are circles

private static float topPatternMargin, leftPatternMargin;
private static float topLogoMargin, leftLogoMargin;

public MyImageView (Context context, AttributeSet attrs) {
super(context, attrs);

screenWidth = getSize(context)[0];
screenHeight = getSize(context)[1];

patternDiameter = (int)(screenWidth * (1005.0/1280.0));
logoDiameter = (int)(patternDiameter * (230.0/502.5));

pattern = BitmapFactory.decodeResource(context.getResources(), R.drawable.start_pattern);
pattern = Bitmap.createScaledBitmap(pattern, patternDiameter, patternDiameter, false);
logo = BitmapFactory.decodeResource(context.getResources(), R.drawable.start_logo);
logo = Bitmap.createScaledBitmap(logo, logoDiameter, logoDiameter, false);

leftPatternMargin = (float) ((screenWidth) * (430.0/1280.0) - (patternDiameter/2.0));
topPatternMargin = (float) ((screenHeight - patternDiameter)/2.0);
leftLogoMargin = (float) (leftPatternMargin + patternDiameter/2.0 - logoDiameter/2.0);
topLogoMargin = (float)(topPatternMargin + patternDiameter/2.0 - logoDiameter/2.0);

offscreen = new Canvas(pattern);

protected void onDraw(Canvas canvas) {


else {


canvas.drawBitmap(pattern, leftPatternMargin, topPatternMargin, null);
canvas.drawBitmap(logo, leftLogoMargin, topLogoMargin, null);



So my question is how would I change this code (or maybe some other code) such that, when I make an instance of this class move around, all parts of it are visible to the user?

Answer Source

I found the answer here:

When drawing outside the view clip bounds with Android: how do I prevent underling views from drawing on top of my custom view?

The solution is to add this line to the layout: