修改图像分辨率和大小以适合卡片视图固定

时间:2017-02-26 07:38:05

标签: android android-cardview

我有一个来自我的Android应用程序的错误消息,它使用卡片视图和回收视图,我认为分配图像的大小和分辨率的问题,这是错误消息:

  

D / skia:---为缩放位图分配失败       D / AndroidRuntime:关闭VM       W / dalvikvm:threadid = 1:线程退出未捕获的异常(组= 0x41775898)       E / AndroidRuntime:致命异常:主要                         java.lang.OutOfMemoryError                             在android.graphics.BitmapFactory.nativeDecodeAsset(本机方法)                             在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:586)                             在android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)                             在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)                             在android.content.res.Resources.loadDrawable(Resources.java:2988)                             在android.content.res.Resources.getDrawable(Resources.java:1558)                             在android.support.v7.widget.ResourcesWrapper.getDrawable(ResourcesWrapper.java:128)                             在android.support.v7.widget.TintResources.getDrawable(TintResources.java:45)                             在android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:374)                             在android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)                             在android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)                             在android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)                             在android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:73)                             在android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:81)                             at pioneers.safwat.animals.RecyclerAdapterbirdse.onBindViewHolder(RecyclerAdapterbirdse.java:71)                             在pioneers.safwat.animals.RecyclerAdapterbirdse.onBindViewHolder(RecyclerAdapterbirdse.java:11)                             在android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:6310)                             在android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:6343)                             在android.support.v7.widget.RecyclerView $ Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5289)                             在android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5552)                             在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5394)                             在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5390)                             在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2149)                             在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1533)                             在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1496)                             在android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:593)                             在android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3537)                             在android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3266)                             在android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3798)                             在android.view.View.layout(View.java:15213)                             在android.view.ViewGroup.layout(ViewGroup.java:4793)                             在android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1167)                             在android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:852)                             在android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:871)                             在android.view.View.layout(View.java:15213)                             在android.view.ViewGroup.layout(ViewGroup.java:4793)                             在android.widget.FrameLayout.onLayout(FrameLayout.java:448)                             在android.view.View.layout(View.java:15213)                             在android.view.ViewGroup.layout(ViewGroup.java:4793)                             在android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)                             在android.view.View.layout(View.java:15213)                             在android.view.ViewGroup.layout(ViewGroup.java:4793)                             在android.widget.FrameLayout.onLayout(FrameLayout.java:448)                             在android.view.View.layout(View.java:15213)                             在android.view.ViewGroup.layout(ViewGroup.java:4793)                             在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)                             在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)                             在android.widget.LinearLayout.onLayout(LinearLayout.java:1440)                             在android.view.View.layout(View.java:15213)                             在android.view.ViewGroup.layout(ViewGroup.java:4793)                             在android.widget.FrameLayout.onLayout(FrameLayout.java:448)                             在android.view.View.layout(View.java:15213)                             在android.vie       I /处理:发送信号。 PID:30359 SIG:9       申请已终止。

这是我的卡片布局XML文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/card_view"
    android:layout_margin="5dp"
    card_view:cardBackgroundColor="#81C784"
    card_view:cardCornerRadius="12dp"
    card_view:cardElevation="3dp"
    card_view:contentPadding="4dp"
    android:foreground="?selectableItemBackground"
    android:clickable="true" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/item_image"
            android:scaleType="fitXY"
            android:adjustViewBounds="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"/>

        <TextView
            android:layout_marginTop="8dp"
            android:id="@+id/item_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:textSize="16pt"
            android:paddingLeft="10dp"
            android:layout_below="@+id/item_image"
            />


    </RelativeLayout>

</android.support.v7.widget.CardView>

1 个答案:

答案 0 :(得分:0)

您可能正在内存中加载一个大位图,并且它会给出内存不足异常。

要避免java.lang.OutOfMemory异常,请在解码之前检查位图的尺寸。

您必须执行以下步骤来处理它 -

  • 估计在内存中加载完整图像的内存使用情况。
  • 您愿意承诺加载此图片的内存量 给出您的应用程序的任何其他内存要求。
  • 图像所在的目标ImageView或UI组件的尺寸 被装入。
  • 当前设备的屏幕尺寸和密度。

从这里了解如何执行上述步骤 - https://developer.android.com/topic/performance/graphics/load-bitmap.html
或者您使用图像加载库来完成所有这些工作 有许多图书馆:GlidePicassoFrescoUIL