android 中的类视图膨胀时出错。
我正在使用一个内部类,它是 View 类的子类。下面是内部类的代码:
public static class MyView extends View {
private Bitmap mBitmap, bg_bmp;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
public MyView(Context c, AttributeSet attrs) {
super(c, attrs);
Bitmap bmp_canvas = BitmapFactory.decodeResource(getResources(), R.drawable.bg_fun_game_draw_canvas);
mBitmap = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.bg_fun_game_draw_canvas), 0, 0, bmp_canvas.getWidth(), bmp_canvas.getHeight());
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}
下面是 xml 布局代码:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_fun_game_draw_bottom"
android:orientation="vertical" >
<view xmlns:android="http://schemas.android.com/apk/res/android"
class="mypackage.android.DrawGame$MyView"
android:id="@+id/linedraw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:layout_gravity="center_horizontal" />
</LinearLayout>
我收到以下错误:
android.view.InflateException: Binary XML file line #75: Error inflate class view.View
请帮我解决这个问题。
I am using an inner class which is a subclass of View class. Below is the code of the inner class:
public static class MyView extends View {
private Bitmap mBitmap, bg_bmp;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
public MyView(Context c, AttributeSet attrs) {
super(c, attrs);
Bitmap bmp_canvas = BitmapFactory.decodeResource(getResources(), R.drawable.bg_fun_game_draw_canvas);
mBitmap = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.bg_fun_game_draw_canvas), 0, 0, bmp_canvas.getWidth(), bmp_canvas.getHeight());
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}
Below is the xml layout code:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_fun_game_draw_bottom"
android:orientation="vertical" >
<view xmlns:android="http://schemas.android.com/apk/res/android"
class="mypackage.android.DrawGame$MyView"
android:id="@+id/linedraw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:layout_gravity="center_horizontal" />
</LinearLayout>
I am getting the following error:
android.view.InflateException: Binary XML file line #75: Error inflating class view.View
Please help me to solve this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生这种情况是因为位图大小大于视图中的可用空间。
It is happening due to the bitmap size is higher than the space available in the view.
尝试一下
。命名空间已经定义。
Try just
instead. The namespace is already defined.