在Android应用程序中自动更改背景

时间:2016-05-10 20:25:48

标签: android image background navigation drawer

我的应用程序有一个导航抽屉,我想向它添加背景图像,但我希望它们自动更改。 目前背景颜色基本上是红色的:

Here is a screenshot that shows the background color of the navigation drawer

当我创建项目并选择“导航抽屉活动”时,整个界面由谷歌的导航抽屉模板自动生成。 我想要做的是用背景图像替换背景颜色,但我想要定期更改图像(例如Bing的本周图像,谷歌的chromecast背景等)。

我有什么方法可以使用Bing当天的形象吗?

1 个答案:

答案 0 :(得分:0)

试试这个。

英语不是我的第一语言。

这可能对您有所帮助,或者您可能会想到实现您的要求。

它运作良好。但我不确定,这是实现这一目标的正确方法。

以下是我的示例代码:

MyApplication.java

 public class MyApplication extends Application {
        public static NavigationDrawerActivity navigationDrawerActivity;
        public static List<Drawable> drawables = new ArrayList<>();

        @Override
        public void onCreate() {
            super.onCreate();
        }

        public static NavigationDrawerActivity getNavigationDrawerActivity() {
            return navigationDrawerActivity;
        }

        public static void setNavigationDrawerActivity(NavigationDrawerActivity navigationDrawerActivity) {
            MyApplication.navigationDrawerActivity = navigationDrawerActivity;
        }

        public static List<Drawable> getDrawables() {
            return drawables;
        }

        public static void setDrawables(List<Drawable> drawables) {
            MyApplication.drawables = drawables;
        }
    }

NavigationDrawerActivity.java

    public class NavigationDrawerActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener {
        List<Drawable> drawables = new ArrayList<>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_navigation_drawer);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
            toggle.syncState();

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);

            View headerView = navigationView.getHeaderView(0);
            LinearLayout linearLayout = (LinearLayout) headerView.findViewById(R.id.header_layout);

            drawables.add(ContextCompat.getDrawable(this, R.drawable.bear));
            drawables.add(ContextCompat.getDrawable(this, R.drawable.falls));
            drawables.add(ContextCompat.getDrawable(this, R.drawable.corbis));

            linearLayout.setBackground(drawables.get(0));
             MyApplication.setDrawables(drawables);
MyApplication.setNavigationDrawerActivity(NavigationDrawerActivity.this);

            UpdateUI();

        }

        public void UpdateUI() {

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            View headerView = navigationView.getHeaderView(0);
            LinearLayout linearLayout = (LinearLayout) headerView.findViewById(R.id.header_layout);

            Drawable backgroundDrawable = linearLayout.getBackground();
            drawables = MyApplication.getDrawables();
            int index = drawables.indexOf(backgroundDrawable);
            if (index == -1 || index == 2) {
                index = 0;
            } else {
                index++;
            }
            linearLayout.setBackground(drawables.get(index++));
            linearLayout.invalidate();
            Intent intent = new Intent(NavigationDrawerActivity.this, UpdateUIReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(
                    NavigationDrawerActivity.this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            cancelNotification(100);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                    + (5 * 1000), pendingIntent);

            Toast.makeText(this, "Starting alarm in 5 seconds", Toast.LENGTH_SHORT).show();
        }

        public void cancelNotification(int requestCode) {
            try {
                Intent notificationIntent = new Intent(getApplicationContext(), UpdateUIReceiver.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
                alarmManager.cancel(pendingIntent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onBackPressed() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                super.onBackPressed();
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.navigation_drawer, 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;
            }

            return super.onOptionsItemSelected(item);
        }

        @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
            int id = item.getItemId();

            if (id == R.id.nav_camera) {
                // Handle the camera action
            } else if (id == R.id.nav_gallery) {

            } else if (id == R.id.nav_slideshow) {

            } else if (id == R.id.nav_manage) {

            } else if (id == R.id.nav_share) {

            } else if (id == R.id.nav_send) {

            }

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }

        public static class UpdateUIReceiver extends BroadcastReceiver {

            public UpdateUIReceiver() {
            }

            @Override
            public void onReceive(Context context, Intent intent) {
                if (MyApplication.getNavigationDrawerActivity() != null) {
                    NavigationDrawerActivity navigationDrawerActivity = MyApplication.getNavigationDrawerActivity();
                    navigationDrawerActivity.UpdateUI();
                }
            }
        }
    }

nav_header_navigation_drawer.xml

刚添加了parent linearlayout的ID

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/header_layout"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@android:drawable/sym_def_app_icon" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Android Studio"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android.studio@android.com" />

</LinearLayout>

的AndroidManifest.xml

  <receiver android:name=".NavigationDrawerActivity$UpdateUIReceiver" />