-2

如何在画布中创建 ScaleAnimation?我的画布上有几个精灵,我想应用 ScaleAnimation。我的代码:

public void onDraw(Canvas canvas){
    ScaleAnimation  scale = new ScaleAnimation(0, 2, 0, 2);
    scale.setInterpolator(new AccelerateDecelerateInterpolator());
    scale.setDuration(1000L);
    startAnimation(scale);
    rect.set(150, 150, 300, 300);
    paint.setColor(Color.RED);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(3);
    canvas.drawRoundRect(rect, sizeCage / 10, sizeCage / 10, paint);
}
4

1 回答 1

0

正如我在这里 所怀疑和描述的那样,您不能在画布对象内使用视图动画,因为它们属于图形类。

这是一个简单的代码,可以帮助您入门。

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView( new MyView(MainActivity.this));
}





class MyView extends View
{

    Rect rect;
    Paint paint;
    public MyView(Context context) {
        // TODO Auto-generated constructor stub
        super(context);
        init();
    }

    public void init()
    {

    paint= new Paint();
    paint.setStyle(Style.FILL);
    paint.setColor(Color.BLUE);     
    rect= new Rect(100, 100, 100, 100);

    }
    @Override
    protected void onDraw(final Canvas canvas) {
        // TODO Auto-generated method stub


        canvas.drawRect(rect, paint);
        new Thread( new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                for(int i=100;i<1000;i++)
                {       
                    rect.union(100, 100, i, i);
                    postInvalidate();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }
        }).start();




    }
}

}

于 2015-01-22T12:02:26.347 回答