1. 程式人生 > >Android程式碼中動態設定圖片的大小(自動縮放),位置

Android程式碼中動態設定圖片的大小(自動縮放),位置

<RelativeLayout 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="${relativePackage}.${activityClass}" >
    <ImageView
        android:id="@+id/image01"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitXY" android:src="@drawable/aabb" />

</RelativeLayout>

public class MainActivity extends Activity {private ImageView image;@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);image = (ImageView)findViewById(R.id.image01);       //設定圖片的位置MarginLayoutParams margin9 = new MarginLayoutParams(image.getLayoutParams());margin9.setMargins(4001000);//在左邊距400畫素,頂邊距10畫素的位置顯示圖片RelativeLayout.LayoutParams layoutParams9 = 
new RelativeLayout.LayoutParams(margin9);layoutParams9.height = 600;//設定圖片的高度layoutParams9.width = 800//設定圖片的寬度image.setLayoutParams(layoutParams9);}}