如何在画布中创建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);
}
答案 0 :(得分:0)
我怀疑和描述Here 你不能在画布对象中使用视图动画,因为它们属于Graphics Class。
这是一个简单的代码,可以帮助您入门。
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();
}
}
}