Android 单亲视图中的多个子视图
我做了两个视图类 在第一个视图类中,我制作了第二个视图的多个实例 并添加到第一个视图中,但它只显示一个视图 请帮帮我 这是父视图Android 单亲视图中的多个子视图,android,view,Android,View,我做了两个视图类 在第一个视图类中,我制作了第二个视图的多个实例 并添加到第一个视图中,但它只显示一个视图 请帮帮我 这是父视图 package com.game.AntSmash; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.Drawable; import andr
package com.game.AntSmash;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.SurfaceView;
import android.widget.AbsoluteLayout;
import android.widget.LinearLayout;
public class surface extends LinearLayout {
public surface(Context context) {
super(context);
this.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
this.setBackgroundDrawable(getResources().getDrawable(R.drawable.grass));
Drawable d = getResources().getDrawable(R.drawable.ant_big);
d.setBounds(160, 0, 160 + d.getIntrinsicWidth(), d.getIntrinsicHeight());
this.addView(new antView(context, d, 0.2f, 0, 0, 0, 0, 0));
d = getResources().getDrawable(R.drawable.ant_big);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
this.addView(new antView(context, d, 0.2f, 0, 0, 160, 0, 0));
}
}
这是子视图
package com.game.AntSmash;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Picture;
import android.graphics.drawable.Drawable;
import android.text.BoringLayout.Metrics;
import android.view.View;
import android.widget.ImageView;
public class antView extends View {
int screenW = 320;
int screenH = 480;
int X;
int Y;
int initialY ;
int ballW;
int ballH;
int angle;
float dY;
float dX;
float acc;
int initialX;
Drawable ball;
public antView(Context context, Drawable bmp, float speed, int dx, int dy, int initialX, int initialY, int angle) {
super(context);
this.ball = bmp;
this.acc = speed;
this.dX = dx;
this.dY = dy;
this.initialX = initialX;
this.initialY = initialY;
this.angle = angle;
}
@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
//compute roughly ball speed and location
Y+= (int) dY; //increase or decrease vertical position
if (Y > (screenH - ballH)) {
dY=(-1)*dY; //reverse speed when bottom hit
}
dY+= acc; //increase or decrease speed
//compute roughly ball speed and location
X+= (int) dX; //increase or decrease vertical position
if (X > (screenW - ballW)) {
dX=(-1)*dX; //reverse speed when bottom hit
}
dX+= acc / 5; //increase or decrease speed
//increase rotating angle
if ((angle = angle + 10) >360) angle =0;
//draw ball
canvas.save(); //save the position of the canvas
canvas.rotate(angle, X + (ballW / 2), Y + (ballH / 2)); //rotate the canvas
canvas.translate(X, Y);
ball.draw(canvas);
canvas.restore(); //rotate the canvas back so that it looks like ball has rotated
//call the next frame
if(X > screenW - ballW && Y > screenH - ballH) {
} else {
invalidate();
}
}
}
这就是活动
package com.game.AntSmash;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
public class AntSmashActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout v = new surface(this);
setContentView(v);
}
}
您需要为LinearLayout父视图设置一些布局参数,还可以使用
setOrientation()
定义子视图的显示方式
孩子的尺寸可能太大了
如果你想使用二维绘图,我推荐一个我写的。将SurfaceView用于二维图形比使用视图和布局更容易
结尾提示:类名应以大写开头
Surface
和AntView
而不是Surface
和AntView
什么样的视图?向我们展示一些代码,如果您使用的话,可能还有您的布局XML。我已经添加了代码,请查看它。如果有添加曲面,则我无法为child覆盖OnTouchEventview@Dharmendra当前位置我不理解你之前的评论。如果你打算写一个游戏(我在软件包名称中看到了这一点),那么我强烈推荐我的教程系列。在这里,您可以找到像“antsmash”@WarrenFaith这样的基本游戏所需的一切。我在您的教程中引用了您的教程。您正在曲面触控上添加位图,但现在如果我触控,我想停止移动位图bitmap@Dharmendra:如果您完成了旧零件(新零件目前尚未完成)您应该了解如何进行碰撞检测。RockScissorPaper使用基于位图的基本碰撞检测,您只需更改它即可检测与触摸事件的碰撞。就这些。只要一步一步地做。如果您仍然有问题,请创建一个新问题(在此处发布链接,以便我得到通知)