调整图片大小为依据textivew大小 [英] Resize picture into according to textivew's size

查看:254
本文介绍了调整图片大小为依据textivew大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 drawableLeft

的TextView 中,我设置图像

 <的TextView
   机器人:ID =@ + ID / imgChooseImage
   机器人:layout_width =FILL_PARENT
   机器人:layout_height =0dp
   机器人:layout_weight =3
   机器人:背景=@绘制/ slim_spinner_normal
   机器人:drawableLeft =@绘制/ ic_launcher/>

和我只想知道我应该在java的code编写动态替换新的图像,使图像前人的精力不超过的TextView 和寻找形象好在左绘制图像。

我应该在比例因子

要使用

  INT比例因子= Math.min();

下面是用java code

  BitmapFactory.Options bmOptions =新BitmapFactory.Options();
//如果设置为true,去codeR将返回null(无位),但
//将出...领域仍将设置,允许呼叫者
//查询该位图,而不必分配的内存
//它的像素。
bmOptions.inJustDe codeBounds = TRUE;
INT photoW = hListView.getWidth();
INT photoH = hListView.getHeight();//确定多少缩小图像
INT比例因子= Math.min(photoW / 100,photoH / 100);//德code中的图像文件转换成位图大小以填充视图
bmOptions.inJustDe codeBounds = FALSE;
bmOptions.inSampleSize =比例因子;
bmOptions.inPurgeable =真;
位图位图= BitmapFactory.de codeResource(getResources(),Const.template [ARG2],bmOptions);可绘制平局=新BitmapDrawable(getResources(),位图);/ *的地方形象的TextView * /
TextView的txtView =(的TextView)findViewById(R.id.imgChooseImage);
txtView.setCompoundDrawablesWithIntrinsicBounds(平局,NULL,NULL,NULL);
位置= ARG2;


解决方案

您所要求的一种方式布局之后计算的TextView 的确切高度,这样就可以调整为 drawableLeft 属性的位图的大小。
问题是由几个问题复杂化:


  1. 如果文本换到多行的高度可以显着改变。

  2. 根据设备硬件屏幕密度,呈现的尺寸
    位图的将被改变,irregardless的确切大小的
    位图在缩放/渲染,所以屏幕密度将不得不采取
    计算进去时,比例因子

  3. 最后,比例因子并没有提供一个确切的尺寸图像请求。
    它仅限制了位图的大小,以尽可能小的图像
    这仍然是相同或比你的要求,以节省更大
    记忆。你还必须将图像调整到精确的高度
    你已经计算出来的。

drawableLeft 方法无法克服上述问题,我认为这是一个更好的办法,而不必使用Java code来调整以达到您的预期布局。

我相信你应该更换你的TextView一个水平方向包含的ImageView 和<$ C 的LinearLayout $ C>的TextView 。 TextView的高度设置为WRAP_CONTENT并设置 scaleType 的ImageView的以中心,像这样的

 的android:scaleType =中心

在的LinearLayout将在TextView的文本和的ImageView scaleType 将强制位图的高度来布局过程中自动调整大小。这里可用scaleTypes参考: ImageView.ScaleType

当然,你将不得不在XML为的LinearLayout,ImageView的和TextView的布局调整参数,使它们居中对齐,并在您需要的确切方式为主。但是,至少你只有做一次。

因为它似乎你将加载照片到您的应用程序资源的ImageView,你可能知道该图像是不是非常大,因此你可以直接打开位图,或使用 inSampleSize =比例因子= 1 。否则,如果该图像是特别大或获得的OutOfMemoryError 例外,然后计算比例因子如下:

  INT inSampleSize = 1;
如果(高度&GT; reqHeight ||宽度GT; reqWidth){
    如果(宽&GT;高度){
        inSampleSize = Math.round((浮点)高度/(浮点)reqHeight);
    }其他{
        inSampleSize = Math.round((浮点)宽/(浮点)reqWidth);
    }
}

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;

解决方案

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);
    }
}

这篇关于调整图片大小为依据textivew大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆