在 Android 中以编程方式创建可绘制对象,适合父级的宽度并具有成比例的高度



所以我用这种方法从URL加载图像

public Drawable loadImage(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (Exception e) {
        System.out.println(e.getMessage()+" oh noo ");
        return null;
    }
}

但我希望图像的宽度与其父级相同(即高度和宽度为 100% 的根元素(。我找不到仅使用 XML 的方法。它总是在边缘留下填充物。我发现这是因为图像的屏幕比例不同。例如,屏幕为 16:9,图像为 4:3。Android将制作填充物来填补这一差异。

您知道在加载图像时如何以编程方式执行此操作吗?而且即使设备旋转,我也需要此图像保持与屏幕宽度一样大(所以我认为我需要计算它的增益(

我正在使用此代码来获取屏幕的大小

public Size getScreenSize(){
    Size screenSize = new Size();
    DisplayMetrics metrics = new DisplayMetrics();
    this.activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    screenSize.height = metrics.heightPixels;
    screenSize.width = metrics.widthPixels;
    return screenSize;
}
public class Size{
    public double height;
    public double width;
}

@EDIT1

这就是我在视图中插入可绘制对象的方式。视图已在活动的 XML 中声明

ImageView picture = (ImageView) findViewById(R.id.picture);
picture.setImageDrawable(loadImage(url));

@EDIT2

这就是我编写活动布局及其样式的方式

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <!-- style="@style/globalHeader" -->
    <LinearLayout
        style="@style/globalHeader"  
        >
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#fff"
            android:textSize="18sp"
            android:textStyle="bold"        
            />
        <TextView
            android:id="@+id/subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#fff"
            android:textSize="14sp"
            />
    </LinearLayout>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
            android:id="@+id/picture"
        android:adjustViewBounds="true"
            android:scaleType="centerCrop"
        android:background="#f00"
        >
    </ImageView>
    <TextView
        android:id="@+id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        />
    <TextView
        android:id="@+id/details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

这就是我想要实现的外观

https://docs.google.com/a/uniriotec.br/file/d/0B2abPynX9PhkODhRVEkwcUphY28/edit?pli=1

我认为这就是

您要查找的那个。只需重新缩放从互联网上获取的可绘制对象的图像。

Bitmap bit = BitmapFactory.decodeResource(getApplicationContext()
                .getResources(), drawable);
        if (bit != null) {
            Bitmap resizedBitmap = Bitmap.createScaledBitmap(bit, width,
                    height, true);
            imageView.setBitmap(resizedBitmap);
            }

宽度和高度计算如下

    DisplayMetrics disp = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(disp);
    int width = disp.widthPixels;
    int height = disp.heightPixels;

@EDIT

private Bitmap loadImage(String URL)
{        
    Bitmap bitmap = null;
    InputStream in = null;        
    try {
        in = OpenHttpConnection(URL);
        bitmap = BitmapFactory.decodeStream(in);
        in.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return bitmap;                
}
private InputStream OpenHttpConnection(String urlString)
        throws IOException
        {
            InputStream in = null;
            int response = -1;
            URL url = new URL(urlString);
            URLConnection conn = url.openConnection();
            if (!(conn instanceof HttpURLConnection))                    
                throw new IOException("Not an HTTP connection");
            try{
                HttpURLConnection httpConn = (HttpURLConnection) conn;
                httpConn.setAllowUserInteraction(false);
                httpConn.setInstanceFollowRedirects(true);
                httpConn.setRequestMethod("GET");
                httpConn.connect();
                response = httpConn.getResponseCode();                
                if (response == HttpURLConnection.HTTP_OK) 
                {
                    in = httpConn.getInputStream();                                
                }                    
            }
            catch (Exception ex)
            {
                throw new IOException("Error connecting");            
            }
            return in;    
}

直接获取位图图像并重新缩放并使用它。

不确定它在设备旋转时的表现如何,但我相信您实际上可以从 xml 布局中做到这一点,将 ImageView.ScaleType 设置为 CENTER_CROP 应该可以解决问题。

CENTER_CROP :均匀缩放图像(保持图像的纵横比(,以便图像的两个尺寸(宽度和高度(都等于或大于视图的相应尺寸(减去填充(。

<ImageView
    android:id="@+id/picture"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop" />

试试这个

<ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:id="@+id/picture"
        android:scaleType="fitXY"
        android:background="#f00"
        >

最新更新