来自静态方法Android的Invalidate / onDraw

时间:2013-07-29 13:22:20

标签: java android static-methods

我正在开发Android项目。我有ViewPage(活动),它有片段PainterFragment。在PainterFragment方法onCreateView中,我返回Painter(getActivity());

在我的Painter类中,我绘制对象(onDraw)。现在我需要刷新View。我有静态方法刷新(我从ViewPage调用)但它不能使非静态函数无效,所以我的View只在我点击它时刷新,因为onScale / onTouch。

从Painter类刷新View应该怎么做?当我想刷新它时,可能以编程方式编写onTouch事件?

public class Painter extends View {


    private static final int INVALID_POINTER_ID = -1;
    ...
    static Context baseContext;

    public Painter(Context context, AttributeSet attrs) {
        this(context, attrs, 0);     
        baseContext = getContext();

    }

    public Painter(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        baseContext = getContext();
        mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
    }

    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {


        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFactor *= detector.getScaleFactor();

            // Zabezpieczenie przed nieskończonym zoomowaniem
            mScaleFactor = Math.max(0.5f, Math.min(mScaleFactor, 10.0f));      
            invalidate();
            return true;
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // Let the ScaleGestureDetector inspect all events.

        mScaleDetector.onTouchEvent(ev);

        final int action = ev.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN: {
            final float x = ev.getX();
            final float y = ev.getY();

            float px = ev.getX() / mScaleFactor + myRect.left;
            float py = ev.getY() / mScaleFactor + myRect.top;

            mLastTouchX = x;
            mLastTouchY = y;
            mActivePointerId = ev.getPointerId(0);
            for(int i = 0; i<listPointer.size(); i++) {
                ...
            }
            break;
        }

        case MotionEvent.ACTION_MOVE: {
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(pointerIndex);
            final float y = ev.getY(pointerIndex);

            // Only move if the ScaleGestureDetector isn't processing a gesture.
            if (!mScaleDetector.isInProgress()) {
                final float dx = x - mLastTouchX;
                final float dy = y - mLastTouchY;

                mPosX += dx;
                mPosY += dy;

                invalidate();
            }

            mLastTouchX = x;
            mLastTouchY = y;

            break;
        }

        case MotionEvent.ACTION_UP: {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }

        case MotionEvent.ACTION_CANCEL: {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }

        case MotionEvent.ACTION_POINTER_UP: {
            final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) 
                    >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
            final int pointerId = ev.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId) {

                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mLastTouchX = ev.getX(newPointerIndex);
                mLastTouchY = ev.getY(newPointerIndex);
                mActivePointerId = ev.getPointerId(newPointerIndex);
            }
            break;
        }
        }

        return true;
    }

    public Painter(Context context) {

        this(context, null, 0);
        baseContext = getContext();
        paint.setColor(Color.BLACK);

        refresher();

    }

    public void refresher() {

        if(printTable != null) printTable.clear();

        if(listOrders!=null & baseContext!=null) listOrders = Bridge.getOrders(baseContext);

        if(listTable!=null) {
            for(Table table: listTable) {

            printTable.add(table);
            }

        }

        invalidate();

    }
    public static void refresh() {

        if(printTable != null) printTable.clear();

        if(listOrders!=null & baseContext!=null) listOrders = Bridge.getOrders(baseContext);

        if(listTable!=null) {
            for(Table table: listTable) {

            printTable.add(table);
            }

        }
    }

    @Override
    public void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        ...   
    }

    public void Draw(Table item, Canvas canvas)
    {
        ...     
    }    
    }



}

1 个答案:

答案 0 :(得分:3)

为什么不能将视图转换为像

这样的变量

查看myView = Painter.this(); myView.invalidate();在画家班里面。