在我的Android应用程序中显示CollapsingToolbarLayout
时遇到问题。有人请告诉我哪里出错了......运行应用程序后,我得到了这个日志
adroidRuntime: FATAL EXCEPTION: main
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:378)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:408)
at com.android.epepea.epepea.GPS.dynamicToolBarColor(GPS.java:108)
at com.android.epepea.epepea.GPS.onCreate(GPS.java:91)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
用于显示CollapsingToolbarLayout
的活动如下所示:
package com.android.epepea.epepea;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import java.util.ArrayList;
public class GPS extends AppCompatActivity {
private UserData userData;
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ActionBarDrawerToggle toggle;
private DrawerLayout drawerLayout;
private LinearLayout linearLayout;
private TextView userName;
private TextView userEmail;
private ArrayList<Items> items = new ArrayList<>();
private CollapsingToolbarLayout collapsingToolbarLayout;
private ImageView header;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
recyclerView = (RecyclerView) findViewById(R.id.login_recycler);
layoutManager = new LinearLayoutManager(this);
toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
drawerLayout.addDrawerListener(toggle);
// native objects;
userName = (TextView) findViewById(R.id.text_view_name);
userEmail = (TextView) findViewById(R.id.text_view_email);
linearLayout = (LinearLayout) findViewById(R.id.navigation);
adapter = new LoginUserAdpter(this, items, getSupportFragmentManager(), drawerLayout, linearLayout);
setUpRecyclerView();
// classes
userData = new UserData(this);
String userNameSet = userData.getFirstName() + " " + userData.getSecondName();
userName.setText(userNameSet);
userEmail.setText(userData.getEmail());
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbarLayout.setTitle("Epepea Gps");
//header = (ImageView) findViewById(R.id.collapsing_image);
dynamicToolBarColor();
toolBarTextAppearance();
final Intent intent = new Intent(this, Maps.class);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_oepn_maps);
assert fab != null;
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(intent);
}
});
}
private void dynamicToolBarColor() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_background_123);
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
collapsingToolbarLayout.setContentScrimColor(palette.getMutedColor(R.attr.colorPrimaryDark));
collapsingToolbarLayout.setStatusBarScrimColor(palette.getMutedColor(R.attr.colorPrimaryDark));
}
});
}
private void toolBarTextAppearance() {
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.AppTheme);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.AppTheme_AppBarOverlay);
}
private void unbindDrawables(View view) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}
// Call this method from onDestroy()
public void onDestroy() {
super.onDestroy();
unbindDrawables(findViewById(R.id.drawer));
System.gc();
}
private void setUpRecyclerView() {
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
String[] menuText = this.getResources().getStringArray(R.array.menu_text_array);
int menuImages[] = {
R.drawable.ic_stay_current_portrait_white_24dp,
R.drawable.ic_language_white_24dp,
R.drawable.ic_contact_mail_white_24dp,
R.drawable.ic_build_white_24dp,
R.drawable.ic_person_outline_white_24dp,
R.drawable.ic_receipt_white_24dp,
R.drawable.ic_exit_to_app_white_24dp
};
for (int i = 0; i < menuText.length && i < menuImages.length; i++) {
items.add(new Items(
menuText[i],
menuImages[i]
));
}
recyclerView.setAdapter(adapter);
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(linearLayout)) {
drawerLayout.closeDrawer(linearLayout);
} else {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Confirm Close")
.setMessage("Are you sure you want to close app?")
.setCancelable(false)
.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
exitApp();
}
})
.setNegativeButton("Cancel", null)
.show();
}
}
public void exitApp() {
super.finish();
}
@Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
toggle.syncState();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == android.R.id.home) {
if (drawerLayout.isDrawerOpen(linearLayout))
drawerLayout.closeDrawer(linearLayout);
else
drawerLayout.openDrawer(linearLayout);
}
return super.onOptionsItemSelected(item);
}
}
我的Xml布局如下所示:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.android.epepea.epepea.GPS">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="250dp"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/collapsing_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ic_background_123"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_gps" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_oepn_maps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_pin_drop_white_36dp"
app:layout_anchor="@id/app_bar_layout"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
<LinearLayout
android:id="@+id/navigation"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/system_color"
android:orientation="vertical">
<!-- some content comes here -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_background_nice_kitkat"
android:padding="15dp">
<TextView
android:id="@+id/text_view_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/image_profile"
android:layout_marginBottom="10dp"
android:gravity="center"
android:padding="7dp"
android:text="Benson Karue"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#fff"
android:textStyle="bold" />
<ImageView
android:id="@+id/image_profile"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:src="@drawable/ic_account_circle_white_48dp" />
<TextView
android:id="@+id/text_view_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/image_profile"
android:layout_marginTop="10dp"
android:gravity="center"
android:padding="7dp"
android:text="bensonkarue30@gmail.com"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#000"
android:textStyle="bold" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginBottom="15dp"
android:background="#e1e1e1" />
<android.support.v7.widget.RecyclerView
android:id="@+id/login_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
如果我有人帮助我,我会非常感激。问候。
答案 0 :(得分:0)
在这一行:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_background_123);
您正在解码高分辨率图像。为了使用库管理它,您可以使用以下方法之一:
或者如果你想通过毕加索生成调色板,请先阅读this article
Picassopalette 是另一个可以用来生成picasso调色板的库。
或者您可以使用此代码进行滑行:
Glide.with(this)
.load(your image path,id,...)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
Palette.Builder palette = Palette.from(resource);
palette.generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
}
});
}
});