• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.activityscenetransitionbasic;
18 
19 import com.android.volley.toolbox.ImageLoader;
20 import com.android.volley.toolbox.NetworkImageView;
21 import com.android.volley.toolbox.Volley;
22 
23 import android.app.Activity;
24 import android.graphics.Bitmap;
25 import android.os.Bundle;
26 import android.widget.TextView;
27 
28 /**
29  * Our secondary Activity which is launched from {@link MainActivity}. Has a simple detail UI
30  * which has a large banner image, title and body text.
31  */
32 public class DetailActivity extends Activity {
33 
34     // Extra name for the ID parameter
35     public static final String EXTRA_PARAM_ID = "detail:_id";
36 
37     // View name of the header image. Used for activity scene transitions
38     public static final String VIEW_NAME_HEADER_IMAGE = "detail:header:image";
39 
40     // View name of the header title. Used for activity scene transitions
41     public static final String VIEW_NAME_HEADER_TITLE = "detail:header:title";
42 
43     private NetworkImageView mHeaderImageView;
44     private TextView mHeaderTitle;
45 
46     private ImageLoader mImageLoader;
47 
48     @Override
onCreate(Bundle savedInstanceState)49     protected void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51         setContentView(R.layout.details);
52 
53         // Construct an ImageLoader instance so that we can load images from the network
54         mImageLoader = new ImageLoader(Volley.newRequestQueue(this), ImageMemoryCache.INSTANCE);
55 
56         // Retrieve the correct Item instance, using the ID provided in the Intent
57         Item item = Item.getItem(getIntent().getIntExtra(EXTRA_PARAM_ID, 0));
58 
59         mHeaderImageView = (NetworkImageView) findViewById(R.id.imageview_header);
60         mHeaderTitle = (TextView) findViewById(R.id.textview_title);
61 
62         // BEGIN_INCLUDE(detail_set_view_name)
63         /**
64          * Set the name of the view's which will be transition to, using the static values above.
65          * This could be done in the layout XML, but exposing it via static variables allows easy
66          * querying from other Activities
67          */
68         mHeaderImageView.setViewName(VIEW_NAME_HEADER_IMAGE);
69         mHeaderTitle.setViewName(VIEW_NAME_HEADER_TITLE);
70         // END_INCLUDE(detail_set_view_name)
71 
72         loadItem(item);
73     }
74 
loadItem(Item item)75     private void loadItem(Item item) {
76         // Set the title TextView to the item's name and author
77         mHeaderTitle.setText(getString(R.string.image_header, item.getName(), item.getAuthor()));
78 
79         final ImageMemoryCache cache = ImageMemoryCache.INSTANCE;
80         Bitmap thumbnailImage = cache.getBitmapFromUrl(item.getThumbnailUrl());
81 
82         // Check to see if we already have the thumbnail sized image in the cache. If so, start
83         // loading the full size image and display the thumbnail as a placeholder.
84         if (thumbnailImage != null) {
85             mHeaderImageView.setImageUrl(item.getPhotoUrl(), mImageLoader);
86             mHeaderImageView.setImageBitmap(thumbnailImage);
87             return;
88         }
89 
90         // If we get here then we do not have either the full size or the thumbnail in the cache.
91         // Here we just load the full size and make do.
92         mHeaderImageView.setImageUrl(item.getPhotoUrl(), mImageLoader);
93     }
94 
95 }
96