onlayout不被叫

时间:2014-01-21 23:24:36

标签: java android layout view ontouchevent

这是我设置3 * 2布局的代码,当我在onCreateView()方法中初始化视图,并使用setX(100)和第二个代码时,一切都按照我的想法工作,即整个3 * 2视图向右移动100像素。当我添加触摸事件监听器,并使用setX(100)更改视图位置时,则不调用onlayout(),它就像一个3 * 1布局。

那么当我收到触摸事件时如何在onLayout上调用它?

protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    int mTotalHeight = 0;  
    int mTotalWidth = 0;
    int childCount = getChildCount();  
    for (int i = 0; i < childCount; i++) {  
        View childView = getChildAt(i);  
        int measureHeight = childView.getMeasuredHeight();  
        int measuredWidth = childView.getMeasuredWidth();  
        childView.layout(mTotalWidth, mTotalHeight, mTotalWidth+measuredWidth, mTotalHeight + measureHeight);  
        mTotalWidth += measuredWidth;
        if (i%2 == 1) {
            mTotalWidth=0;
            mTotalHeight += measureHeight;  
        }
    }
}

这是我的触控听众:

    public boolean onTouchEvent(MotionEvent event) {
            PointF curr = new PointF(event.getX(), event.getY());
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                originalF = curr;
                break;
case MotionEvent.ACTION_MOVE:
    if ((curr.x-originalF.x)<screenWidth) {
        for (MapView mapView : allViews) {
          mapView.setX(100)
        mapView.requestLayout();
        mapView.forceLayout();
       }
        this.invalidate();

我检查了android文件,其中说onlayout()应该从布局中调用,此视图应该为每个子项分配一个大小和位置。带子项的派生类应该覆盖此方法并在每个子项上调用布局。

我认为我的观点代码与此没有太多关系,但我仍然会将它们粘贴在这里。

private void init() {
    // TODO Auto-generated method stub
    Bitmap b = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888);
     c = new Canvas(b);
     original = BitmapFactory.decodeResource(getResources(),R.drawable.placeholder);
     getScreenSize();
     final int canvasWidth = c.getWidth();
     final int canvasHeight = c.getHeight();
     Log.d(VIEW_LOG_TAG, "this new view's width is="+canvasWidth
             +"screen height is"+canvasHeight);     
}

@SuppressLint("NewApi")
private void getScreenSize() {
    WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    try{
        Point size = new Point();
        display.getSize(size);
        screenWidth = size.x;
        screenHeight = size.y;
        Log.d(VIEW_LOG_TAG, "screen width is="+screenWidth
                +"screen height is"+screenHeight);      
        }catch(NoSuchMethodError e){
            Log.d(VIEW_LOG_TAG, "not such method in getscreensize(", e);
        }
}

    @Override
    protected void onDraw(Canvas canvas) {
         if (placeHolder != null) {
             canvas.drawBitmap(placeHolder, 0, 0, null);
        } 
    }

    //      stretch the image to the suitable size with parent MeasureSpec Param.
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        parentHeight = MeasureSpec.getSize(heightMeasureSpec);
        MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.UNSPECIFIED);
    //     Log.d("this time", "parentSize is"+parentWidth+parentHeight);
           this.setMeasuredDimension(parentWidth, parentHeight);        //because just set the parent size, so indeed no effect here.
    }

public void setMap(Bitmap bitmap) {
    //placeHolder = bitmap;
    placeHolder =Bitmap.createScaledBitmap(bitmap, parentWidth, parentHeight, true);
    invalidate();
}   

除此之外,我使用

this.requestLayout();
this.forceLayout();

但他们没有意义。

我可以再次提出我的主要问题,如何调用这个onlayout()方法?

0 个答案:

没有答案