[英]Android programmatically added button has match parent width and height
[英]Make a drawable in Android created programmatically fits parent's width and has proportional height
所以我用这种方法从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;
@编辑
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"
>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.