我有一個TextView
我在drawableLeft
調整圖片大小爲根據textivew大小
<TextView
android:id="@+id/imgChooseImage"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:background="@drawable/slim_spinner_normal"
android:drawableLeft="@drawable/ic_launcher"/>
設定圖像和我只想知道我應該在java代碼編寫動態替換新的圖像,該圖像不能超過TextView
並且在可繪製的左側圖像中看圖像良好。
scalefactor
需要使用什麼?下面
int scaleFactor = Math.min();
是java代碼
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
// If set to true, the decoder will return null (no bitmap), but
// the out... fields will still be set, allowing the caller to
// query the bitmap without having to allocate the memory for
// its pixels.
bmOptions.inJustDecodeBounds = true;
int photoW = hListView.getWidth();
int photoH = hListView.getHeight();
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/100, photoH/100);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), Const.template[arg2],bmOptions);
Drawable draw = new BitmapDrawable(getResources(), bitmap);
/* place image to textview */
TextView txtView = (TextView) findViewById(R.id.imgChooseImage);
txtView.setCompoundDrawablesWithIntrinsicBounds(draw, null,null, null);
position = arg2;