如何让一个圆圈慢慢地“弹出”到屏幕上

时间:2015-04-25 18:04:10

标签: java android android-animation android-canvas

我正在制作一个在屏幕上显示多个随机圆圈的应用。我想知道我是否可以扩展半径,而它显示圆圈然后是disapeers。我已经编写了代码来随机显示这里的圆圈。

public class SplashLaunch extends View{
    Handler cool = new Handler();
    DrawingView v;
    ObjectAnimator aa = new ObjectAnimator();
    Paint newPaint = new Paint();
    int randomWidthOne = 0;
    int randomHeightOne = 0;
    private static int radiusOne = 300;
    final int redColorOne = Color.RED;
    final int greenColorOne = Color.GREEN;
    private static int lastColorOne;
    private final Random theRandom = new Random();
    public SplashLaunch(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    private final Runnable circleUpdater = new Runnable() {
        @SuppressLint("NewApi")
        @Override 
        public void run() {
            lastColorOne = theRandom.nextInt(2) == 1 ? redColorOne : greenColorOne;
            newPaint.setColor(lastColorOne);           
            cool.postDelayed(this, 500);
            int x = 0;
            while(x<=255){
                newPaint.setAlpha(x);
                x++;
                }
            invalidate();
        }
    };

    @Override
    protected void onAttachedToWindow(){
        super.onAttachedToWindow();
        cool.post(circleUpdater);
    }
    protected void onDetachedFromWindow(){
        super.onDetachedFromWindow();
        cool.removeCallbacks(circleUpdater);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        if(theRandom == null){
            randomWidthOne =(int) (theRandom.nextInt(Math.abs(getWidth()-radiusOne/2)) + radiusOne/2f);
            randomHeightOne = (theRandom.nextInt((int)Math.abs((getHeight()-radiusOne/2 + radiusOne/2f))));
        }else {
            randomWidthOne =(int) (theRandom.nextInt(Math.abs(getWidth()-radiusOne/2)) + radiusOne/2f);
            randomHeightOne = (theRandom.nextInt((int)Math.abs((getHeight()-radiusOne/2 + radiusOne/2f))));
        }
        canvas.drawCircle(randomWidthOne, randomHeightOne, radiusOne, newPaint);
    }

}

1 个答案:

答案 0 :(得分:1)

  

我想知道我是否可以扩展半径,而它显示圆圈然后消失

是的,你可以。您只需将视图的Scale设置为大于1的值,即可展开视图,然后返回0以使其“消失”。