不寻常的Android UI问题:android:scaleType =“fitXY和match_parent不会增加ImageView的大小 -

时间:2013-12-23 15:54:34

标签: 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");
    }

}

1 个答案:

答案 0 :(得分:0)

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