android 中的类视图膨胀时出错。

发布于 2024-10-21 03:05:18 字数 3126 浏览 2 评论 0原文

我正在使用一个内部类,它是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如何视而不见 2024-10-28 03:05:18

发生这种情况是因为位图大小大于视图中的可用空间。

It is happening due to the bitmap size is higher than the space available in the view.

┼── 2024-10-28 03:05:18

尝试一下

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

。命名空间已经定义。

Try just

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

instead. The namespace is already defined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文