Android Center Crop Image不在设备上工作,但在Eclipse中工作

时间:2014-12-16 10:36:56

标签: android android-layout imageview

我有一个列表视图,每个列表项都有一个图像背景。我试图通过从给定的URL下载图像来设置src。我需要裁剪ImageView视图,以便图像占据整个屏幕(宽度和高度)。

这是我的列表项的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list_item"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

    <LinearLayout
        android:id="@+id/ll_question_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginBottom="40dp"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:orientation="vertical" >

        <me.frankly.view.BariolBoldTextView
            android:id="@+id/question_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:gravity="center"
            android:textColor="@color/white_pure"
            android:textSize="22sp" />

        <RelativeLayout
            android:id="@+id/author_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp" >

            <me.frankly.view.BariolTextView
                android:id="@+id/leading_text_author"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/leading_text_author"
                android:textColor="@color/white_pure"
                android:textSize="17sp" />

            <me.frankly.view.BariolTextView
                android:id="@+id/question_author_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/leading_text_author"
                android:paddingLeft="5dp"
                android:textColor="@color/comment_username"
                android:textSize="17sp" />
        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

这是主要的活动布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/default_background" >

    <include
        android:id="@+id/answer_question_action_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        layout="@layout/action_bar_backbtn_text" />

    <View
        android:id="@+id/view_answer_question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/answer_question_action_bar"
        android:layout_alignTop="@id/answer_question_action_bar" >
    </View>

    <ListView
        android:id="@+id/asked_question_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/view_answer_question"
        android:cacheColorHint="#00000000"
        android:divider="@null"
        android:dividerHeight="0dp" >
    </ListView>

</RelativeLayout>

这是在适配器的getView中设置图像的片段,ImageLoader来自UIL:

ImageLoader.getInstance().loadImage(askersCollageUrl, new ImageLoadingListener() {

                    @Override
                    public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {

                        if (holder.askersCollageUrl.equals(arg0)) {
                        Drawable d = new BitmapDrawable(mcontext.getResources(), arg2);

                            holder.collageImageView.setImageDrawable(d) ; 


                        }
                    }

                //Omitting other methods heere

                });

这是预期的: enter image description here

这就是我得到的: enter image description here

1 个答案:

答案 0 :(得分:0)

hi make功能用于裁剪图像

private void performCrop(Uri imageUri) {
        try {
            // call the standard crop action intent (the user device may not
            // support it)
            Intent cropIntent = new Intent("com.android.camera.action.CROP");
            // indicate image type and Uri
            cropIntent.setDataAndType(imageUri, "image/*");
            // set crop properties
            cropIntent.putExtra("crop", "true");
            // indicate aspect of desired crop
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
            // indicate output X and Y

            if (act.equals("RegisterActivity")) {
                cropIntent.putExtra("outputX", 71);
                cropIntent.putExtra("outputY", 58);

            } else if (act.equals("ChangeProfileActivity")) {
                cropIntent.putExtra("outputX", 242);
                cropIntent.putExtra("outputY", 142);

            } else if (act.equals("ChangeProfilePicActivity")) {
                cropIntent.putExtra("outputX", 242);
                cropIntent.putExtra("outputY", 328);
            }



            // retrieve data on return
            cropIntent.putExtra("return-data", true);
            // start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, PIC_CROP);
        } catch (ActivityNotFoundException anfe) {
            // display an error message
            String errorMessage = "Whoops - your device doesn't support the crop action!";
            Toast toast = Toast
                    .makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }