java - 异常的 Android UI 问题 : android:scaleType="fitXY and match_parent will not increase the size of an ImageView -

标签 java android android-imageview android-xml

我有一个缩略图,其行为非常奇怪。我已将其参数设置为 match_parent 以及 android:scaleType="fitXY" 并且似乎都对缩略图没有任何影响(所需的行为是填充到屏幕边缘)。

XML:list_item_user_video.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.example.project.ui.widget.UrlImageView
        android:id="@+id/userVideoThumbImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="false"
        android:clickable="false"
        android:contentDescription="YouTube video thumbnail"
        android:focusable="false"
        android:scaleType="fitXY"
        android:focusableInTouchMode="false"
        android:gravity="center"
        android:src="@drawable/ic_launcher" />

Home.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/content_frame"
        android:focusable="true"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="220dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/darkgrey"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:textStyle="bold" />

    <RelativeLayout
        android:id="@+id/rl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#AAFFFFFF" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <com.idg.omv.ui.widget.VideosListView
                android:id="@+id/videosListView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal" >

            <RelativeLayout
                android:layout_width="50dip"
                android:layout_height="50dip"
                android:layout_alignParentBottom="true" >

                <ImageButton
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="left"
                    android:background="@color/darkgrey"
                    android:scaleType="centerCrop"
                    android:src="@drawable/home_up_btn" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/footer"
                android:layout_width="match_parent"
                android:layout_height="50dip"
                android:layout_alignParentBottom="true" >

                <android.support.v4.view.ViewPager
                    android:id="@+id/view_pager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

                <ImageButton
                    android:layout_width="30dip"
                    android:layout_height="30dip"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="15dp"
                    android:focusable="false"
                    android:src="@drawable/scroll_lt_arrow" />

                <ImageButton
                    android:layout_width="30dip"
                    android:layout_height="30dip"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="15dp"
                    android:focusable="false"
                    android:src="@drawable/scroll_rt_arrow" />
            </RelativeLayout>
        </LinearLayout>
    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

JAVA:

public class UrlImageView extends LinearLayout {

    private Context mContext;
    private Drawable mDrawable;
    private ProgressBar mSpinner;
    private ImageView mImage;

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

    public UrlImageView(Context context) {
        super(context);
        init(context);
    }

    /**
     * First time loading of the LoaderImageView
     * Sets up the LayoutParams of the view, you can change these to
     * get the required effects you want
     */
    private void init(final Context context) {
        mContext = context;

        mImage = new ImageView(mContext);
        mImage.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        mImage.setVisibility(View.GONE);
        mImage.setScaleType(ScaleType.FIT_XY);
        mSpinner = new ProgressBar(mContext);
        mSpinner.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        mSpinner.setIndeterminate(true);

        addView(mSpinner);
        addView(mImage);
    }

    /**
     * Set's the view's drawable, this uses the internet to retrieve the image
     * don't forget to add the correct permissions to your manifest
     * 
     * @param imageUrl the url of the image you wish to load
     */
    public void setImageDrawable(final String imageUrl) {
        mDrawable = null;
        mSpinner.setVisibility(View.VISIBLE);
        mImage.setVisibility(View.GONE);
        mImage.setScaleType(ScaleType.FIT_XY);
        new Thread() {
            public void run() {
                try {
                    mDrawable = getDrawableFromUrl(imageUrl);
                    imageLoadedHandler.sendEmptyMessage(RESULT_OK);
                } catch (MalformedURLException e) {
                    imageLoadedHandler.sendEmptyMessage(RESULT_CANCELED);
                } catch (IOException e) {
                    imageLoadedHandler.sendEmptyMessage(RESULT_CANCELED);
                }
            };
        }.start();
    }

    /**
     * Callback that is received once the image has been downloaded
     */
    private final Handler imageLoadedHandler = new Handler(new Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
            case RESULT_OK:
                mImage.setImageDrawable(mDrawable);
                mImage.setVisibility(View.VISIBLE);
                mImage.setScaleType(ScaleType.FIT_XY);
                mSpinner.setVisibility(View.GONE);
                break;
            case RESULT_CANCELED:
            default:
                // Could change image here to a 'failed' image
                // otherwise will just keep on spinning
                break;
            }
            return true;
        }
    });

    /**
     * Pass in an image url to get a drawable object
     * 
     * @return a drawable object
     * @throws IOException
     * @throws MalformedURLException
     */
    private static Drawable getDrawableFromUrl(final String url) throws IOException, MalformedURLException {
        return Drawable.createFromStream(((java.io.InputStream) new java.net.URL(url).getContent()), "name");
    }

}

最佳答案

您是否尝试过使用 WRAP_CONTENT 而不是 MATCH_PARENT?为什么要将图像一直拉伸(stretch)到边缘?这看起来会很糟糕。

关于java - 异常的 Android UI 问题 : android:scaleType="fitXY and match_parent will not increase the size of an ImageView -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20746893/

相关文章:

java - 引用 ImageView 子类获取 ClassCastException

android - ListView 中的 ImageView 大小问题

JAVA - For循环和If else语句

Android opengl 问题(奇怪)

android - 我想要 SimpleAdapter 中的 Imageview 吗?

android - 嵌套的preferences.xml

android - @Inject 设置不注入(inject)属性

java - Spring集成和改变数据路由

java - Spring MVC 3 validator 不显示错误消息

java - 仅为特定用户向标签添加属性