尝试用地图构建一个安卓游戏(现在我们只是试图让它画一个圆圈),我们创建的视图不会正常膨胀,应用程序不断崩溃
活动
package edu.virignia.cs2110.dlf4mf.ghostapocalypse;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class GameActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
MapView mv = (MapView)findViewById(R.id.map_view);
}
}
活动布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<MapView
android:id="@+id/map_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
自定义视图
package edu.virignia.cs2110.dlf4mf.ghostapocalypse;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.SurfaceView;
import android.view.View;
public class MapView extends SurfaceView {
private final Paint mPaint = new Paint();
public MapView(Context context) {
super(context);
init();
}
public MapView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MapView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
int h = 500;
int w = 500;
Canvas grid = new Canvas(Bitmap.createBitmap(h,w, Bitmap.Config.ARGB_8888));
draw(grid);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setStyle(Paint.Style.FILL);
int c = R.color.material_deep_teal_500;
mPaint.setColor(c);
canvas.drawCircle(250, 250, 150, mPaint);
}
}
答案 0 :(得分:3)
使用<MapView
替换XML布局中的<edu.virignia.cs2110.dlf4mf.ghostapocalypse.MapView
。自定义视图的类名必须是完全限定的。