2011-03-10 83 views
0

我使用的內部類是View類的子類。下面是內部類的代碼:錯誤膨脹類view.View在android中

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:二進制XML文件行#75:錯誤膨脹類視圖。查看

請幫我解決這個問題。

+0

顯示堆棧跟蹤 – vnshetty 2011-03-10 12:28:40

回答

1

由於位圖大小高於視圖中的可用空間而發生。

0

儘量只

<view class="mypackage.android.DrawGame$MyView" 

代替。名稱空間已經定義。

+0

按照您的建議嘗試,但仍然收到相同的錯誤。 – 2011-03-11 03:57:38

相關問題