3

I have a TextView in that I am setting image in drawableLeft

<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"/>

and I want to just know what should I have to write in java code to dynamically replace new image so that image sould not exceed the TextView and looking image good in drawable left image.

What should I have to use in scalefactor?

int scaleFactor = Math.min();

below is java code

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;
HitOdessit
  • 7,198
  • 4
  • 36
  • 59
Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150

1 Answers1

0

You are asking for a way to calculate the exact height of a TextView after layout so that you can adjust the size of a Bitmap for the drawableLeft attribute. The problem is compounded by several issues:

  1. If the text wraps to multi-line the height can change dramatically.
  2. Depending on the device hardware screen density, the rendered size of the Bitmap will be changed, irregardless of the exact size of the Bitmap you scale/render, so the screen density will have to be taken into account when calculating scaleFactor.
  3. Finally, scaleFactor does not provide an exact size image request. It only limits the size of the Bitmap to the smallest possible image that is still the same or bigger than your request in order to save memory. You will still have to resize the image to the exact height you have calculated.

The drawableLeft method cannot overcome the problems described above and I think there is a better way to achieve your intended layout without having to resize with Java code.

I believe that you should replace your TextView with a horizontally oriented LinearLayout containing an ImageView and a TextView. Set the height of the TextView to "WRAP_CONTENT" and set the scaleType of the ImageView to "center", like this:

android:scaleType="center"

The LinearLayout will have the height of the text in the TextView and the ImageView's scaleType will force the Bitmap to be automatically resized during the layout. Here the reference for the available scaleTypes: ImageView.ScaleType.

Of course, you will have to adjust the layout parameters in XML for the LinearLayout, ImageView, and TextView so they are centered, justified, and oriented in the exact manner you want. But, at least you'll only have do it once.

As it appears that you will be loading a photo into the ImageView from your applications resources, you may know that the image is not very large so you can just open the Bitmap directly, or use inSampleSize = scaleFactor = 1. Otherwise, if the image is particularly large or your get an OutOfMemoryError exception, then calculate scaleFactor as follows:

int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
    if (width > height) {
        inSampleSize = Math.round((float) height / (float) reqHeight);
    } else {
        inSampleSize = Math.round((float) width / (float) reqWidth);
    }
}
David Manpearl
  • 12,362
  • 8
  • 55
  • 72