我如何填写一份Appwidget一个ImageView的同时mainiting宽高比? [英] How can I fill an ImageView in an Appwidget while mainiting the aspect ratio?

查看:207
本文介绍了我如何填写一份Appwidget一个ImageView的同时mainiting宽高比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含 ImageView的一个简单的小工具。我想从网上获取的图像,并显示它的的ImageView ,同时保持纵横比。

I have a simple widget containing an ImageView. I'd like to fetch an image from the web and display it in the ImageView while maintaining the aspect ratio.

下面是该插件的布局定义:

Here's the layout definition for the widget:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/quality_image"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/widget_icon"
        android:layout_marginBottom="@dimen/widget_spacing"
        android:scaleType="fitXY" />

</LinearLayout>

..这是有问题的图片:

..and this is the image in question:

我想的形象总是适合的ImageView 的高度。该位已非易事。如果的ImageView 的宽度小于形象,如果图像被横向只要宽高比保持不变裁剪我不介意。我设置widget的最大大小决不会比图像的大小,更使我们不必担心情况下,当的ImageView的高度是以上的图像。

I'd like the image to always fit the height of the ImageView. This bit has been easy. If the width of the ImageView is less than that of the image, I don't mind if the image gets cropped horizontally as long as the aspect ratio is maintained. I've set the maximum size of the widget to never be more than the size of the image so we don't have to worry about cases when the height of the ImageView is more than the image.

我非常失去了这一点,并有开始想知道如果这甚至有可能,因为似乎没有要的方式来访问控件的尺寸。有时,最琐碎的事情停滞不前你失望。

I'm terribly lost with this and have begin wondering if this is even possible since there doesn't seem to be way to access the widget dimensions. Sometimes the most trivial things bog you down.

推荐答案

假设这个问题的一部分是最大限度地给细胞的占用数widget的高度(每中的LinearLayout的FILL_PARENT并给予你打开裁剪宽度)。我会忽略了问题的一部分裁剪并提供维持指定长宽比的例子。

Assuming that part of this question is to maximize the height of the widget given the number of cells it occupies (per the fill_parent in the LinearLayout and given you’re open to cropping the width). I’ll ignore the cropping part of the question and provide an example that maintains a specified aspect ratio.

我发现,以保持上最大高度的AppWidget纵横比的最佳方法是创建一个固定纵横比的XML绘制对象,其缩小到窗口小部件的最大高度,再扎的两侧ImageView的它使用RelativeLayout的。例如:

The best way I’ve found to maintain aspect ratio on an AppWidget at maximum height is to create an XML Drawable of a fixed aspect ratio, scale it down to the max height of the widget, and then tie the sides of the ImageView to it using a RelativeLayout. Example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget_layout"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/aspectratio"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:src="@drawable/frame_aspect"
        android:scaleType="fitCenter" >
    </ImageView>

    <ImageView
        android:id="@+id/quality_image"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignLeft="@id/aspectratio"
        android:layout_alignRight="@id/aspectratio"
        android:layout_alignTop="@id/aspectratio"
        android:layout_alignBottom="@id/aspectratio"
        android:scaleType="fitCenter" >
</RelativeLayout>

下面是指定的长宽比为绘制(frame_aspect.xml)的布局。注 - 重要的是,这种形状是大于你的widget将永远不会。内置的scaleType的做好缩小,但不能在扩大这么好。

Here is the layout for the drawable (frame_aspect.xml) that specifies the aspect ratio. Note - it is important that this shape is larger than your widget will ever be. The built in scaleType’s do a good job scaling down, but are not so good at scaling up.

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape = "rectangle">

    <size 
        android:width="1000px"
        android:height="500px"/>
</shape>

现在,你有quality_image缩放到最大高度为给定的长宽比小部件,您可以在ImageView的与scaleType发挥,以确定是否有什么任何裁剪是最好的。

Now that you have a widget with quality_image scaled to the maximum height for a given aspect ratio, you can play with the scaleType on the ImageView to determine what if any cropping is best.

这篇关于我如何填写一份Appwidget一个ImageView的同时mainiting宽高比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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