我有一个包含所有按钮和图片的XML布局,我想在布局顶部放置一个移动的云。因此,我创建了一个视图并让我的云移动,但是我无法将视图与布局链接起来。以下是我的视图代码:
public class CloudView extends View {
Bitmap cloud;
int ChangingX;
public CloudView(Context context) {
// TODO Auto-generated constructor stub
super(context);
cloud = BitmapFactory.decodeResource(getResources(), R.drawable.cloud);
ChangingX = 50;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(cloud, ChangingX , 50, null);
if (ChangingX < canvas.getWidth())
ChangingX += 2;
else
ChangingX = 50;
invalidate();
}
}
以下是我的MainActivity代码:
public class MainActivity extends Activity {
CloudView myView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myView = new CloudView(this);
setContentView(myView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我在安卓动画方面还是新手,您能详细解释一下如何将视图与布局链接吗?如果无法工作,除了View之外,还有哪些类可以使用。
感谢您的时间和考虑。对我的糟糕英语表示抱歉。