android.view.InflateException:二进制XML文件:错误膨胀类

时间:2015-07-22 09:53:18

标签: java android xml android-layout

我收到错误

  

android.view.InflateException: Binary XML file line #16: Error inflating class DrawView

。我在DrawView课程中创建自定义视图。在调用OncreatesetContentView(R.layout.newimage_activity);方法,它会显示错误。

XML布局是:     

    <com.capricorn.RayMenu
    android:id="@+id/ray_menu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="5dp"
    android:paddingRight="5dp" />

    <com.element45.android.camera.activities.DrawView
    android:id="@+id/CustomView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white" 
     tools:context="com.element45.android.camera.activities.newImage_Activity"
    />

DrawView类代码是:

public class DrawView extends View {

    private Bitmap  mBitmap;
    private Canvas  mCanvas;
    private Path    mPath;
    private Paint   mBitmapPaint;
    private Paint   mPaint;

    private RectF   rectF ;
    private Region  Region1;
    private Region  Region2;
    private List<Point> mPoints;
    private List<Line> mLines;
    private boolean bModify_line;
    private float mX, mY;
    private float mOriginal_X, mOriginal_Y;

    private Line mLine ;
    private static final float TOUCH_TOLERANCE = 50;


    public DrawView(Context c) 
    {
        super(c);
        rectF = new RectF();
        mPath = new Path();
        mPath.computeBounds(rectF, true);
        mPaint =  new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setAntiAlias(true);
        mPaint.setXfermode(null);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mPoints = new ArrayList<Point>();
        mLines  = new ArrayList<Line>();
        bModify_line =  false;

    }

    int width,height;
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w ; height =h;

    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        canvas.drawColor(0xFFAAAAAA);//original color 0xFFAAAAAA
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        for(int i = 0 ; i < mLines.size();i++)
        {
            Line line = mLines.get(i);
            mCanvas.drawLine(line.getStartPoint().x, line.getStartPoint().y, line.getEndPoint().x, line.getEndPoint().y, mPaint);
        }

    }


    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) 
        {
            mX = x;
            mY = y;
        }
    }

    private void touch_start(float x, float y) 
    {
        int  iLineNumber = checkPointinLines(x, y);

        if(iLineNumber >= 0 )
        {
            bModify_line  = true;
            mPath.moveTo(x, y);
            mX = x;
            mY = y;
        }
        else
        {
            mLine =  new Line();
            mLine.setStartPoint(new Point((int)x, (int)y));
            mPath.moveTo(x, y);
            mX = x;
            mY = y;
        }
        mOriginal_X =  mX;
        mOriginal_Y =  mY;
    }

    private void touch_up() 
    {

        if(bModify_line)
        {
            int  iLineNumber = checkPointinLines(mOriginal_X, mOriginal_Y);
            if(iLineNumber > -1)
            {
                Line line = mLines.get(iLineNumber);
                int iPoint = checkPointOnLine(mX,mY,line);
                if(iPoint >= 0)
                {
                    if(iPoint == 0)
                    {
                        mPath.moveTo(line.getEndPoint().x, line.getEndPoint().y);
                        line.setStartPoint(new Point((int)mX, (int)mY));

                    }
                    else if(iPoint == 1)
                    {
                        mPath.moveTo(line.getStartPoint().x, line.getStartPoint().y);
                        line.setEndPoint(new Point((int)mX, (int)mY));
                    }

                    mLines.set(iLineNumber, line);
                    System.out.println("After setline size of lineArray::"+mLines.size());

                    bModify_line = false;
                }
            }
        }
        else
        {
            mPath.lineTo(mX, mY);
            mLine.setEndPoint(new Point((int)mX, (int)mY));
            mLines.add(mLine);
            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;
    }


    /**
     * Checks if user touch point with some tolerance
     */
    private int checkPointOnLine(float x, float y,Line line) 
    {
        int iPoint = -1; 

        Point point = line.getStartPoint();

        if(x > (point.x - TOUCH_TOLERANCE) && x < (point.x + TOUCH_TOLERANCE) && y > (point.y - TOUCH_TOLERANCE) && y < (point.y + TOUCH_TOLERANCE) )
            iPoint = 0;

        point = line.getEndPoint();

        if (x > (point.x - TOUCH_TOLERANCE) && x < (point.x + TOUCH_TOLERANCE) && (y > (point.y - TOUCH_TOLERANCE) && y < (point.y + TOUCH_TOLERANCE) ))

            iPoint = 1 ;


        return iPoint;
    }


    private int checkPointinLines(float x, float y) 
    {
        int iLine = -1; 
        for(int i=0 ;i < mLines.size();i++)
        {
            Line line = mLines.get(i);

            if(x > (line.getStartPoint().x - TOUCH_TOLERANCE) 
                    && x < (line.getStartPoint().x + TOUCH_TOLERANCE) 
                    && y > (line.getStartPoint().y - TOUCH_TOLERANCE) 
                    && y < (line.getStartPoint().y + TOUCH_TOLERANCE) 
                    ||
                    x > (line.getEndPoint().x - TOUCH_TOLERANCE) 
                    && x < (line.getEndPoint().x + TOUCH_TOLERANCE) 
                    && y > (line.getEndPoint().y - TOUCH_TOLERANCE) 
                    && y < (line.getEndPoint().y + TOUCH_TOLERANCE))
                iLine = i;

        }
        return iLine;
    }

    public void movePoint(int position)
    {
        switch(position)
        {
        case 0:
            //up
            movePointUp();
            break;
        case 1:
            //Right
            movePointRight();
            break;
        case 2:
            //Down
            movePointDown();
            break;
        case 3:
            //Left
            movePointLeft();
            break;

        }
    }

    private void movePointUp()
    {
        if(bModify_line)
        {
            int  iLineNumber = checkPointinLines(mOriginal_X, mOriginal_Y);
            if(iLineNumber > -1)
            {
                Line line = mLines.get(iLineNumber);
                int iPoint = checkPointOnLine(mX,mY,line);
                if(iPoint >= 0)
                {
                    int modifyPointX,modifyPointY;
                    if(iPoint == 0)
                    {
                        modifyPointX = line.getStartPoint().x ;
                        modifyPointY = line.getStartPoint().y - 2;
                        mPath.moveTo(line.getEndPoint().x, line.getEndPoint().y);
                        line.setStartPoint(new Point(modifyPointX, modifyPointY));
                    }
                    else if(iPoint == 1)
                    {
                        modifyPointX = line.getEndPoint().x;
                        modifyPointY = line.getEndPoint().y -2;
                        mPath.moveTo(line.getStartPoint().x, line.getStartPoint().y);
                        line.setEndPoint(new Point(modifyPointX, modifyPointY));
                    }

                    mLines.set(iLineNumber, line);
                    bModify_line = false;
                }
            }
        }
        else
        {
            Toast.makeText(getContext(), "Please select point", Toast.LENGTH_SHORT).show();
        }

    }

    private void movePointRight()
    {
        if(bModify_line)
        {
            int  iLineNumber = checkPointinLines(mOriginal_X, mOriginal_Y);
            if(iLineNumber > -1)
            {
                Line line = mLines.get(iLineNumber);
                int iPoint = checkPointOnLine(mX,mY,line);
                if(iPoint >= 0)
                {
                    int modifyPointX,modifyPointY;
                    if(iPoint == 0)
                    {
                        modifyPointX = line.getStartPoint().x + 2;
                        modifyPointY = line.getStartPoint().y ;
                        mPath.moveTo(line.getEndPoint().x, line.getEndPoint().y);
                        line.setStartPoint(new Point(modifyPointX, modifyPointY));
                    }
                    else if(iPoint == 1)
                    {
                        modifyPointX = line.getEndPoint().x + 2;
                        modifyPointY = line.getEndPoint().y ;
                        mPath.moveTo(line.getStartPoint().x, line.getStartPoint().y);
                        line.setEndPoint(new Point(modifyPointX, modifyPointY));
                    }

                    mLines.set(iLineNumber, line);
                    bModify_line = false;
                }
            }
        }
        else
        {
            Toast.makeText(getContext(), "Please select point", Toast.LENGTH_SHORT).show();
        }
    }

    private void movePointLeft()
    {
        if(bModify_line)
        {
            int  iLineNumber = checkPointinLines(mOriginal_X, mOriginal_Y);
            if(iLineNumber > -1)
            {
                Line line = mLines.get(iLineNumber);
                int iPoint = checkPointOnLine(mX,mY,line);
                if(iPoint >= 0)
                {
                    int modifyPointX,modifyPointY;
                    if(iPoint == 0)
                    {
                        modifyPointX = line.getStartPoint().x - 2 ;
                        modifyPointY = line.getStartPoint().y ;
                        mPath.moveTo(line.getEndPoint().x, line.getEndPoint().y);
                        line.setStartPoint(new Point(modifyPointX, modifyPointY));
                    }
                    else if(iPoint == 1)
                    {
                        modifyPointX = line.getEndPoint().x - 2;
                        modifyPointY = line.getEndPoint().y ;
                        mPath.moveTo(line.getStartPoint().x, line.getStartPoint().y);
                        line.setEndPoint(new Point(modifyPointX, modifyPointY));
                    }

                    mLines.set(iLineNumber, line);
                    bModify_line = false;
                }
            }
        }
        else
        {
            Toast.makeText(getContext(), "Please select point", Toast.LENGTH_SHORT).show();
        }
    }

    private void movePointDown()
    {
        if(bModify_line)
        {
            int  iLineNumber = checkPointinLines(mOriginal_X, mOriginal_Y);
            if(iLineNumber > -1)
            {
                Line line = mLines.get(iLineNumber);
                int iPoint = checkPointOnLine(mX,mY,line);
                if(iPoint >= 0)
                {
                    int modifyPointX,modifyPointY;
                    if(iPoint == 0)
                    {
                        modifyPointX = line.getStartPoint().x ;
                        modifyPointY = line.getStartPoint().y + 2;
                        mPath.moveTo(line.getEndPoint().x, line.getEndPoint().y);
                        line.setStartPoint(new Point(modifyPointX, modifyPointY));
                    }
                    else if(iPoint == 1)
                    {
                        modifyPointX = line.getEndPoint().x;
                        modifyPointY = line.getEndPoint().y + 2;
                        mPath.moveTo(line.getStartPoint().x, line.getStartPoint().y);
                        line.setEndPoint(new Point(modifyPointX, modifyPointY));
                    }

                    mLines.set(iLineNumber, line);
                    bModify_line = false;
                }
            }
        }
        else
        {
            Toast.makeText(getContext(), "Please select point", Toast.LENGTH_SHORT).show();
        }

    }
}

1 个答案:

答案 0 :(得分:2)

对于xml通胀,您需要覆盖将Context和AttributeSet作为参数的构造函数。从文档

  

从XML扩充视图时调用的构造方法。

添加

public DrawView(Context c, AttributeSet attrs) {
     super(c, attrs);
}

到您的自定义视图。您可以在执行初始化的两个构造函数之间共享一个公共的init()方法(以避免代码重复)