调整图像大小以适合ImageButton

时间:2015-12-08 06:22:58

标签: android android-imagebutton

我真的很陌生,我正在尝试将画廊和相机中的个人资料照片加载到ImageButton中。我可以从图库添加图像,但图像不完全适合ImageButton。某些空间仍然会从图像按钮中遗漏。我希望图像自动适合图像按钮。

`def second(list1):
    if len(list1)!= len(set(list1)):
         result = sorted(list1)[2]
        return result
    elif len(list1) == len(set(list1)):
        result = sorted(list1)[1]
        return result`

活动类如下: -

  <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="com.sam.sport.MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LL1" >

        <LinearLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp" >

        <ImageButton
          android:id="@+id/profile_pic"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:src="@drawable/ic_launcher"
          android:scaleType="fitXY"
          android:adjustViewBounds="true" />

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

4 个答案:

答案 0 :(得分:0)

将其转换为Drawable,然后使用setBackgroundDrawable

setImageBitmap内容
Drawable drawable = new BitmapDrawable(rotateImage);
ib.setBackgroundDrawable(drawable);

答案 1 :(得分:0)

使用以下代码行来重新调整位图大小

 Bitmap BITMAP_IMAGE = Bitmap.createBitmap(IMAGE_VIEW.getWidth(),IMAGE_VIEW.getHeight(), Bitmap.Config.RGB_565);
 Bitmap resizedBitmap = Bitmap.createScaledBitmap(BITMAP_IMAGE, 100, 100, false); ////Here BITMAP_IMAGE is your bitmap which you want to resize

答案 2 :(得分:0)

获取imageButton高度和宽度

Bitmap bitmapScaled = Bitmap.createScaledBitmap(Image, ib_width, ib_height, true);
Drawable drawable = new BitmapDrawable(bitmapScaled);
ib.setBackgroundDrawable(drawable);

请记住OOM异常。

要获取imageButton的高度和宽度,请查看thisthis

答案 3 :(得分:0)

我有完美的解决方案。要调整imageview的大小,我使用了[Android ImageView adjusting parent's height and fitting width

中提供的以下代码

&#13;
&#13;
public class ResizableImageView extends ImageView {

    public ResizableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
         Drawable d = getDrawable();

         if(d!=null){
                 // ceil not round - avoid thin vertical gaps along the left/right edges
                 int width = MeasureSpec.getSize(widthMeasureSpec);
                 int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
                 setMeasuredDimension(width, height);
         }else{
                 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
         }
    }

}
&#13;
&#13;
&#13;

] 1