I followed your advice to create a MyImageView class:
public class MyImageView extends ImageView{
 private static final String TAG = MyImageView.class.getSimpleName();
 public MyImageView(Context context, AttributeSet attrs) {
  super(context, attrs);
 }
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  int width = MeasureSpec.getSize(widthMeasureSpec);
  int height = MeasureSpec.getSize(heightMeasureSpec);
  Log.d(TAG, "onMeasure: "+width+"*"+height);
 }
 @Override
 protected void onLayout(boolean changed, int left, int top, int right, int 
bottom) {
  super.onLayout(changed, left, top, right, bottom);
  Log.d(TAG, "onLayout: "+left+" "+top+" "+right+" "+bottom);
 }
}
And changed my layout to use MyImageView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android";
    xmlns:tools="http://schemas.android.com/tools";
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    
    <RelativeLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <!-- 600*100 -->
        <com.example.layouttest.MyImageView android:id="@+id/bar" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/bar"/>
        <!-- 160*80 center in parent-->
        <com.example.layouttest.MyImageView android:id="@+id/banner" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/banner"
            android:layout_centerInParent="true"/>
        <!-- 80*40 center in parent-->
        <com.example.layouttest.MyImageView android:id="@+id/banner2" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/banner2"
            android:layout_centerInParent="true"/>
    </RelativeLayout>
</LinearLayout>
 
In the log file, it printed:
D/MyImageView(3866): onMeasure: 480*724
D/MyImageView(3866): onMeasure: 480*724
D/MyImageView(3866): onMeasure: 480*724
D/MyImageView(3866): onMeasure: 480*724
D/MyImageView(3866): onMeasure: 240*724
D/MyImageView(3866): onMeasure: 120*724
D/MyImageView(3866): onLayout: 0 0 480 80
D/MyImageView(3866): onLayout: 120 0 360 120
D/MyImageView(3866): onLayout: 180 30 300 90
D/MyImageView(3866): onMeasure: 480*724
D/MyImageView(3866): onMeasure: 480*724
D/MyImageView(3866): onMeasure: 480*724
D/MyImageView(3866): onMeasure: 480*724
D/MyImageView(3866): onMeasure: 240*724
D/MyImageView(3866): onMeasure: 120*724
D/MyImageView(3866): onLayout: 0 0 480 80
D/MyImageView(3866): onLayout: 120 0 360 120
D/MyImageView(3866): onLayout: 180 30 300 90
Could you give me more detailed instructions on how to override these two 
methods and leverage the parameters to achieve what I would like to do?
 
To Piren: 
Thank you. Assigning a specific height to the parent works. 
The drawback is doing so is a little like hard code. 
I would like a way to let android resize them dynamically since not all 
sreen widths are the same.
 

lbendlin於 2013年1月26日星期六UTC+8上午3時57分31秒寫道:

> The quickest solution would be to create a custom imageview that extends 
> imageview, and then handle the onLayout and onMeasure yourself.
>
> On Friday, January 25, 2013 12:59:38 AM UTC-5, Greenhand wrote:
>>
>> Because if I changed the two "inner" image views to match_parent, they 
>> will expand the background imageview and its parent (RelativeLayout). It 
>> did not work.
>>  
>> What I would like to do is let the inner imageviews resized automatically 
>> to "exactly match" the background. If it is smaller, it will stretch to the 
>> height of the background. If it is bigger, it will shrink to the height of 
>> the background.
>>  
>> By the way, I think the "match_parent" attribute in Android is literally 
>> misleading because it will make its parent bigger to "match_parent".
>> 2013/1/25 Piren <[email protected]>
>>
>>> if every imageview's height is wrap_content, why are you expecting them 
>>> all to be the same (when all their sources are different sizes)? 
>>> AdjustViewBounds isnt a magical property :)
>>> the two "inner" imageviews should be match_parent.
>>>
>>> it's also a good idea to define scaleType since it has the most 
>>> relevancy when talking about keeping the image ratio.
>>>
>>>
>>>  
>>>
>>>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Reply via email to