0

我要做的就是将存储在 sdcard 上的图像放入片段内的 ImageView 中。

我将本教程用于导航抽屉布局: https ://developer.android.com/training/implementing-navigation/nav-drawer.html

而这个用于更深入的实施: https ://www.swipetips.com/implementing-actionbarsherlock-side-menu-navigation-drawer-in-android/

这是fragment1.xml:

<RelativeLayout 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" >

<ImageView 
    android:id="@+id/workingimageview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"/>
<!--
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/Fragment1"
    android:id="@+id/textview1" />
-->
</RelativeLayout>

这是包含内容和抽屉导航的 xml:

<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" >

<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- Slideout Menü -->
<ListView 
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111" />

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

这是片段 onCreateView 源代码:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle   
    savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.fragment1, container, false);

    /** Load temporary image */
    /** Check if folder exists first */

    if(checkForWorkingDirectory())
    {
        workingImageView = (ImageView) 
                    rootView.findViewById(R.id.workingimageview);
          workingImageView.setImageBitmap(scaleImage.decodeSampledBitmapFromResource(returnAbsoluteFilePath(),100,100));
    }


    return rootView;

    //return inflater.inflate(R.layout.fragment1, null);
}

checkForWorkingDirectory() 基本上是查找图像所在的目录,然后 returnAbsoluteFilePath() 说明一切:)

ImageView 根本不显示。但是,如果我注释掉 ImageView 并只显示 TextView,则文本显示得很好。

我也试过这个,因为当我加载图像时,我不知道 ImageView 的尺寸: http: //adanware.blogspot.de/2012/06/android-getting-measuring-fragment.html

根据教程,我使用了在片段内显示 ImageView 的最佳和正确方法是什么?

4

2 回答 2

0

在我的代码中看到相同的问题后,我在实际查看我的日志后发现了我的问题,我发现了以下错误。

位图太大,无法上传到纹理中(2340x4160,最大值=4096x4096)。

我通过扩展 ImageView 类并重载 setImageBitmap 和 onMeasure 方法解决了我的问题。

公共类 ScaledImageView 扩展 ImageView {

private Bitmap mBitmap;

public ScaledImageView(Context context) {
    this(context, null);
}

public ScaledImageView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public ScaledImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
public void setImageBitmap(Bitmap bm) {
    mBitmap = bm;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);

    if (mBitmap != null) {
        int w = mBitmap.getWidth();
        int h = mBitmap.getHeight();
        int scaledHeight = (int)(((float)parentWidth / (float)w) * h);
        super.setImageBitmap(Bitmap.createScaledBitmap(mBitmap, parentWidth, scaledHeight, true));
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

}

于 2015-09-03T17:35:31.980 回答
-5

错误出现在其他地方。我永远猜不到的地方。由于解决方案不相关,因此没有必要在此处发布。

于 2013-08-21T07:37:26.683 回答