Android:移动时使屏幕外位图可见

时间:2016-07-08 13:43:33

标签: java android android-animation android-bitmap

我需要在中心开始两个圆形位图的活动,它应该立即动画并稍微向侧面移动,这样一个位图的一部分在屏幕外并且不可见。稍后,当用户离开活动时,它应该以相同的方式动画,但是向后(从一侧移动到另一个中心)。

所以我实现了一个在主要位置(侧面)绘制这些位图的类,我现在遇到的问题是,当我动画那些时,位图的屏幕外部分从不显示,所以在我的屏幕上,我看到一个丑陋的移动位图丢失了一块。

这是位图的绘制方式:

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);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if(canvas==offscreen)
            super.onDraw(offscreen);

        else {

            super.draw(offscreen);

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

        }

    }
}

所以我的问题是如何更改此代码(或者其他一些代码),以便当我让这个类的实例移动时,用户可以看到它的所有部分?

1 个答案:

答案 0 :(得分:0)

我在这里找到了答案:

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

解决方案是将此行添加到布局中:

android:clipChildren="false"