我在布局中使用了三个图像,其中两个在布局中设置,一个在运行时从drawable加载。在2.1,2.3,3.0,3.1中它工作正常,但我在2.2和2.3.3时出错。
这是我的XML文件。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:id="@+id/TopBar"
android:layout_alignParentTop="true" android:layout_above="@+id/BottomBar"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:id="@+id/ClientSide"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:layout_marginTop="5dip"
android:orientation="vertical" android:layout_alignParentLeft="true"
android:background="#00000000">
<TextView android:id="@+id/EnterName" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/enterclientname"
android:textColor="#FFFFFF" android:textSize="22sp"
android:textStyle="bold" android:layout_marginLeft="3dip">
</TextView>
<TextView android:id="@+id/Date" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/date"
android:textColor="#FFFFFF" android:textSize="10sp"
android:textStyle="bold" android:layout_marginLeft="3dip">
</TextView>
</LinearLayout>
<LinearLayout android:id="@+id/ServerSide"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:layout_marginTop="5dip"
android:orientation="vertical" android:layout_alignParentRight="true"
android:background="#00000000">
<TextView android:id="@+id/ProvidedBy" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/providedby"
android:textColor="#FFFFFF" android:textSize="10sp"
android:textStyle="bold" android:layout_marginRight="3dip">
</TextView>
<TextView android:id="@+id/CenterName" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/centername"
android:textColor="#FFFFFF" android:textSize="10sp"
android:textStyle="bold" android:layout_marginRight="3dip">
</TextView>
<TextView android:id="@+id/CenterAddress"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/centeraddress" android:textColor="#FFFFFF"
android:textSize="10sp" android:textStyle="bold"
android:layout_marginRight="3dip">
</TextView>
</LinearLayout>
<ImageView android:id="@+id/PersonAura"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:src="@drawable/person" android:scaleType="fitXY"
android:adjustViewBounds="true" android:layout_centerInParent="true" />
<ImageView android:id="@+id/OuterAura" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:src="@drawable/outeraura"
android:scaleType="fitXY" android:adjustViewBounds="true"
android:layout_centerInParent="true" />
<ImageView android:id="@+id/InnerAura" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:scaleType="fitXY"
android:adjustViewBounds="true" android:layout_centerInParent="true" />
</RelativeLayout>
<LinearLayout android:id="@+id/BottomBar"
android:layout_alignParentBottom="true" android:gravity="center"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/bar" android:layout_centerVertical="true">
<Button android:id="@+id/Scan" android:layout_width="wrap_content"
android:layout_height="wrap_content" style="@style/Button"
android:textSize="13sp" android:text="@string/scan"
android:textColor="#FFFFFF" android:textStyle="bold"
android:layout_weight="1" android:layout_marginLeft="1dip">
</Button>
<Button android:id="@+id/Tune" android:layout_width="wrap_content"
android:layout_height="wrap_content" style="@style/Button"
android:textSize="13sp" android:text="@string/tune"
android:textColor="#FFFFFF" android:textStyle="bold"
android:layout_weight="1" android:layout_marginLeft="1dip"
android:layout_marginRight="1dip">
</Button>
<Button android:id="@+id/Save" android:layout_width="wrap_content"
android:layout_height="wrap_content" style="@style/Button"
android:textSize="13sp" android:text="@string/saveb"
android:textColor="#FFFFFF" android:textStyle="bold"
android:layout_weight="1" android:layout_marginLeft="1dip"
android:layout_marginRight="1dip">
</Button>
<Button android:id="@+id/Share" android:layout_width="wrap_content"
android:layout_height="wrap_content" style="@style/Button"
android:textSize="13sp" android:text="@string/share"
android:textColor="#FFFFFF" android:textStyle="bold"
android:layout_weight="1" android:layout_marginLeft="1dip"
android:layout_marginRight="1dip">
</Button>
<Button android:id="@+id/Settings" android:layout_width="wrap_content"
android:layout_height="wrap_content" style="@style/Button"
android:textSize="13sp" android:text="@string/settings"
android:textColor="#FFFFFF" android:textStyle="bold"
android:layout_weight="1" android:layout_marginRight="1dip">
</Button>
</LinearLayout>
</RelativeLayout>
这是我的Java文件。
public class Aura extends Activity
{
protected static final String TAG = "TAG";
TextView mClientName, mDate, mProvidedBy, mCenterName, mCenterAddress;
ImageView mOutterAuraImage, mInnerAuraImage, mPersonAuraImage;
String mGetClientNameString;
Button mScan, mTune, mSave, mShare, mSettings;
EditText mSavedClientName;
Bitmap mBackImage, mTopImage, mBackground, mInnerImage, mNewSaving;
Bitmap mTopModifiedImage, mNewKetanImage, mSaveTakeViewBitmap, mTakeViewBitmap;
Canvas mComboImage;
FileOutputStream mFileOutputStream;
BitmapDrawable mBitmapDrawable, mTakeViewBitmapDrawable;
boolean mFileSendSet = false, mFirstSaveCalled = false;
float mContrast;
private ColorMatrix mColorMatrix;
Drawable mDrawable;
@Override
public void onCreate(Bundle mSavedInstanceState)
{
super.onCreate(mSavedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.aura);
Log.v(TAG, "Brand: " + android.os.Build.BRAND);
Log.v(TAG, "Device: " + android.os.Build.DEVICE);
Log.v(TAG, "Display: " + android.os.Build.DISPLAY);
Log.v(TAG, "ID: " + android.os.Build.ID);
Log.v(TAG, "Model: " + android.os.Build.MODEL);
Log.v(TAG, "Manufacturer: " + android.os.Build.MANUFACTURER);
mClientName = (TextView) findViewById(R.id.EnterName);
mDate = (TextView) findViewById(R.id.Date);
mProvidedBy = (TextView) findViewById(R.id.ProvidedBy);
mCenterName = (TextView) findViewById(R.id.CenterName);
mCenterAddress = (TextView) findViewById(R.id.CenterAddress);
mScan = (Button) findViewById(R.id.Scan);
mTune = (Button) findViewById(R.id.Tune);
mSave = (Button) findViewById(R.id.Save);
mShare = (Button) findViewById(R.id.Share);
mSettings = (Button) findViewById(R.id.Settings);
mOutterAuraImage = (ImageView) this.findViewById(R.id.OuterAura);
mInnerAuraImage = (ImageView) this.findViewById(R.id.InnerAura);
mPersonAuraImage = (ImageView) this.findViewById(R.id.PersonAura);
mShare.setOnClickListener(new View.OnClickListener()
{
public void onClick(View mView)
{
try
{
Random mRandomGenerator = new Random();
int mInnerGeneratedRandom = mRandomGenerator.nextInt(255);
Log.v(TAG, "mInnerGeneratedRandom" + mInnerGeneratedRandom);
mColorMatrix = new ColorMatrix();
Drawable mDrawable = getResources().getDrawable(R.drawable.heart);
mDrawable.setColorFilter( Color.MAGENTA, Mode.MULTIPLY );
mInnerAuraImage.setImageDrawable(mDrawable);
int mOutterGeneratedRandom = mRandomGenerator.nextInt(255);
Log.v(TAG, "mOutterGeneratedRandom" + mOutterGeneratedRandom);
mContrast = mOutterGeneratedRandom / 180.f;
setContrastScaleOnly(mColorMatrix, mContrast);
mOutterAuraImage.setColorFilter(new ColorMatrixColorFilter(mColorMatrix));
int mPersonGeneratedRandom = mRandomGenerator.nextInt(255);
Log.v(TAG, "mPersonGeneratedRandom" + mPersonGeneratedRandom);
mContrast = mPersonGeneratedRandom / 180.f;
setContrast(mColorMatrix, mContrast);
mPersonAuraImage.setColorFilter(new ColorMatrixColorFilter(mColorMatrix));
}
catch(OutOfMemoryError eOutOfMemoryError)
{
Log.v(TAG, "OutOfMemoryError: " + eOutOfMemoryError.toString());
System.gc();
//((BitmapDrawable)mInnerAuraImage.getDrawable()).getBitmap().recycle();
unbindDrawables(mDrawable);
}
}
});
}// onCreate
private void unbindDrawables(Drawable nDrawable)
{
if(mDrawable != null)
{
mDrawable.setCallback(null);
}
}
}
我在
收到错误Drawable mDrawable = getResources().getDrawable(R.drawable.heart);
mDrawable.setColorFilter( Color.MAGENTA, Mode.MULTIPLY );
mInnerAuraImage.setImageDrawable(mDrawable);
有人会帮助我为什么会在2.2中发生这种情况吗?我对这个Drawable怎么办?
我的Logcat输出错误:
06-01 18:59:15.563: ERROR/AndroidRuntime(508): FATAL EXCEPTION: main
06-01 18:59:15.563: ERROR/AndroidRuntime(508): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kihealer.aura/com.kihealer.aura.Aura}: android.view.InflateException: Binary XML file line #50: Error inflating class <unknown>
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.os.Looper.loop(Looper.java:123)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at java.lang.reflect.Method.invokeNative(Native Method)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at java.lang.reflect.Method.invoke(Method.java:507)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at dalvik.system.NativeStart.main(Native Method)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): Caused by: android.view.InflateException: Binary XML file line #50: Error inflating class <unknown>
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.app.Activity.setContentView(Activity.java:1657)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at com.kihealer.aura.Aura.onCreate(Aura.java:83)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): ... 11 more
06-01 18:59:15.563: ERROR/AndroidRuntime(508): Caused by: java.lang.reflect.InvocationTargetException
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at java.lang.reflect.Constructor.constructNative(Native Method)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): ... 23 more
06-01 18:59:15.563: ERROR/AndroidRuntime(508): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.graphics.Bitmap.nativeCreate(Native Method)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.content.res.Resources.loadDrawable(Resources.java:1709)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.widget.ImageView.<init>(ImageView.java:118)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): at android.widget.ImageView.<init>(ImageView.java:108)
06-01 18:59:15.563: ERROR/AndroidRuntime(508): ... 26 more
06-01 18:59:15.655: WARN/ActivityManager(74): Force finishing activity com.kihealer.aura/.Aura
感谢。
答案 0 :(得分:0)
你是否为2.2和2.3.3使用不同的模拟器,每个模拟器都有小的ram?
引起:java.lang.OutOfMemoryError:位图大小超过VM预算 听起来应用程序内存不足?
图像有多大?
答案 1 :(得分:0)
看起来布局中的一个图像导致OutOfMemory异常,可能是@ drawable / outeraura。
我会检查您分配给模拟器的RAM量,还会查看可绘制图像的大小。如果它们很大,那么通过缩小它们以匹配目标屏幕密度,您将获得速度和内存的改进。