2012-10-15 42 views
1

我有一个MainActivity,并且在MainActivity内部有一个自定义的TouchView对象,它扩展了ImageView。没有XML布局在我的项目中使用,我在MainActivity的OnCreate()以下行:我的ImageView大小已被更改

... 
    myTouchView = new TouchView(); 
    myTouchView.layout(0,0,bitmap.getWidth(),bitmap.getHeight());//bitmap W*H: 3264*2448 
    MainActivity.this.setContentView(myTouchView); 
    ... 

然后我TouchView中的的onDraw()设置一个断点,并在那里我总是得到的宽度和高度我在layout()上设置的不是3264 * 2448的TouchView对象。这意味着我的TouchView的大小在onDraw()之前已经改变了。我得到的宽度和高度是480 * 320(实际上是屏幕尺寸)。

我怎样才能得到正确的size(3264*2448)?

谢谢!

+0

看看你是否对xml感兴趣,它在xml中更容易 –

+0

myTouchView =(TouchView)findViewById(...);但是我得到的这个对象并不是我需要的,因为我想将一些值传递给构造函数中的对象,但是这个不能得到它们。 – user1745958

回答

0

我已经自己杀了这个问题。我重写onMeasure()像这样:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ 
int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
int parentHeight = MeasureSpec.getSize(heightMeasureSpec); 
if(parentWidth == bmp.getWidth() && parentHeight == bmp.getHeight()) 
    super.onMeasure(idthMeasureSpec, heightMeasureSpec); 
else 
    this.setMeasuredDimension(parentWidth/2, parentHeight); 
} 

适合我,任何潜在的错误。

+0

Framelayout.LayoutParams params = new Framelayout.LayoutParams params = new Framelayout.LayoutParams(bmp.getWidth(),bmp.getHeight()); setContentView(myTouchView,params); 这也适用。 – user1745958