<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/scaletype_center" /> <ImageView android:id="@+id/im1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/duo2" android:scaleType="center" android:background="#f00" android:contentDescription="@string/scaletype_center" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/scaletype_fitCenter" android:layout_marginTop="20dp" android:background="#f00" /> <ImageView android:id="@+id/im2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/duo2" android:scaleType="fitCenter" android:background="#900" android:contentDescription="@string/scaletype_fitCenter" /> </LinearLayout>
MainActivity.java
import android.app.Activity; import android.os.Bundle; import android.widget.ImageView; import android.widget.LinearLayout; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imv1 = (ImageView) this.findViewById(R.id.im1); ImageView imv2 = (ImageView) this.findViewById(R.id.im2); /** * 设置imv1图片宽高,不能适用大小 */ imv1.setLayoutParams(new LinearLayout.LayoutParams(50,50)); /** * 设置imv1图片宽高,自动调整大小 */ imv2.setLayoutParams(new LinearLayout.LayoutParams(50,50)); setTitle("height:"+imv1.getLayoutParams().width+"\nwidth"+imv1.getLayoutParams().width) ; } }