Docs: Sync browseable samples for lmp-docs

Synced to developers/samples/android commit
bc036ecdf44cd03163c206096172299f3940b057.

Change-Id: Ib68230d79ca300e7db906aff2ebfc2cb6c6968f7
This commit is contained in:
Trevor Johns
2014-12-09 18:47:59 -08:00
parent 1ccf8350d5
commit abededd8f7
503 changed files with 8907 additions and 2690 deletions

View File

@@ -16,13 +16,14 @@
package com.example.android.activityscenetransitionbasic;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.squareup.picasso.Picasso;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.transition.Transition;
import android.widget.ImageView;
import android.widget.TextView;
/**
@@ -40,23 +41,20 @@ public class DetailActivity extends Activity {
// View name of the header title. Used for activity scene transitions
public static final String VIEW_NAME_HEADER_TITLE = "detail:header:title";
private NetworkImageView mHeaderImageView;
private ImageView mHeaderImageView;
private TextView mHeaderTitle;
private ImageLoader mImageLoader;
private Item mItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
// Construct an ImageLoader instance so that we can load images from the network
mImageLoader = new ImageLoader(Volley.newRequestQueue(this), ImageMemoryCache.INSTANCE);
// Retrieve the correct Item instance, using the ID provided in the Intent
Item item = Item.getItem(getIntent().getIntExtra(EXTRA_PARAM_ID, 0));
mItem = Item.getItem(getIntent().getIntExtra(EXTRA_PARAM_ID, 0));
mHeaderImageView = (NetworkImageView) findViewById(R.id.imageview_header);
mHeaderImageView = (ImageView) findViewById(R.id.imageview_header);
mHeaderTitle = (TextView) findViewById(R.id.textview_title);
// BEGIN_INCLUDE(detail_set_view_name)
@@ -65,31 +63,97 @@ public class DetailActivity extends Activity {
* This could be done in the layout XML, but exposing it via static variables allows easy
* querying from other Activities
*/
mHeaderImageView.setTransitionName(VIEW_NAME_HEADER_IMAGE);
mHeaderTitle.setTransitionName(VIEW_NAME_HEADER_TITLE);
ViewCompat.setTransitionName(mHeaderImageView, VIEW_NAME_HEADER_IMAGE);
ViewCompat.setTransitionName(mHeaderTitle, VIEW_NAME_HEADER_TITLE);
// END_INCLUDE(detail_set_view_name)
loadItem(item);
loadItem();
}
private void loadItem(Item item) {
private void loadItem() {
// Set the title TextView to the item's name and author
mHeaderTitle.setText(getString(R.string.image_header, item.getName(), item.getAuthor()));
mHeaderTitle.setText(getString(R.string.image_header, mItem.getName(), mItem.getAuthor()));
final ImageMemoryCache cache = ImageMemoryCache.INSTANCE;
Bitmap thumbnailImage = cache.getBitmapFromUrl(item.getThumbnailUrl());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && addTransitionListener()) {
// If we're running on Lollipop and we have added a listener to the shared element
// transition, load the thumbnail. The listener will load the full-size image when
// the transition is complete.
loadThumbnail();
} else {
// If all other cases we should just load the full-size image now
loadFullSizeImage();
}
}
// Check to see if we already have the thumbnail sized image in the cache. If so, start
// loading the full size image and display the thumbnail as a placeholder.
if (thumbnailImage != null) {
mHeaderImageView.setImageUrl(item.getPhotoUrl(), mImageLoader);
mHeaderImageView.setImageBitmap(thumbnailImage);
return;
/**
* Load the item's thumbnail image into our {@link ImageView}.
*/
private void loadThumbnail() {
Picasso.with(mHeaderImageView.getContext())
.load(mItem.getThumbnailUrl())
.noFade()
.into(mHeaderImageView);
}
/**
* Load the item's full-size image into our {@link ImageView}.
*/
private void loadFullSizeImage() {
Picasso.with(mHeaderImageView.getContext())
.load(mItem.getPhotoUrl())
.noFade()
.noPlaceholder()
.into(mHeaderImageView);
}
/**
* Try and add a {@link Transition.TransitionListener} to the entering shared element
* {@link Transition}. We do this so that we can load the full-size image after the transition
* has completed.
*
* @return true if we were successful in adding a listener to the enter transition
*/
private boolean addTransitionListener() {
final Transition transition = getWindow().getSharedElementEnterTransition();
if (transition != null) {
// There is an entering shared element transition so add a listener to it
transition.addListener(new Transition.TransitionListener() {
@Override
public void onTransitionEnd(Transition transition) {
// As the transition has ended, we can now load the full-size image
loadFullSizeImage();
// Make sure we remove ourselves as a listener
transition.removeListener(this);
}
@Override
public void onTransitionStart(Transition transition) {
// No-op
}
@Override
public void onTransitionCancel(Transition transition) {
// Make sure we remove ourselves as a listener
transition.removeListener(this);
}
@Override
public void onTransitionPause(Transition transition) {
// No-op
}
@Override
public void onTransitionResume(Transition transition) {
// No-op
}
});
return true;
}
// If we get here then we do not have either the full size or the thumbnail in the cache.
// Here we just load the full size and make do.
mHeaderImageView.setImageUrl(item.getPhotoUrl(), mImageLoader);
// If we reach here then we have not added a listener
return false;
}
}

View File

@@ -1,96 +0,0 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.activityscenetransitionbasic;
import com.android.volley.toolbox.ImageLoader;
import android.graphics.Bitmap;
import android.util.LruCache;
import java.util.Map;
/**
* An image memory cache implementation for Volley which allows the retrieval of entires via a URL.
* Volley internally inserts items with a key which is a combination of a the size of the image,
* and the url.
*
* This class provide the method {@link #getBitmapFromUrl(String)} which allows the retrieval of
* a bitmap solely on the URL.
*/
public class ImageMemoryCache extends LruCache<String, Bitmap> implements ImageLoader.ImageCache {
/**
* Singleton instance which has it's maximum size set to be 1/8th of the allowed memory size.
*/
public static final ImageMemoryCache INSTANCE = new ImageMemoryCache(
(int) (Runtime.getRuntime().maxMemory() / 8));
// Cache the last created snapshot
private Map<String, Bitmap> mLastSnapshot;
private ImageMemoryCache(int maxSize) {
super(maxSize);
}
public Bitmap getBitmapFromUrl(String url) {
// If we do not have a snapshot to use, generate one
if (mLastSnapshot == null) {
mLastSnapshot = snapshot();
}
// Iterate through the snapshot to find any entries which match our url
for (Map.Entry<String, Bitmap> entry : mLastSnapshot.entrySet()) {
if (url.equals(extractUrl(entry.getKey()))) {
// We've found an entry with the same url, return the bitmap
return entry.getValue();
}
}
// We didn't find an entry, so return null
return null;
}
@Override
public Bitmap getBitmap(String key) {
return get(key);
}
@Override
public void putBitmap(String key, Bitmap bitmap) {
put(key, bitmap);
// An entry has been added, so invalidate the snapshot
mLastSnapshot = null;
}
@Override
protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
super.entryRemoved(evicted, key, oldValue, newValue);
// An entry has been removed, so invalidate the snapshot
mLastSnapshot = null;
}
private static String extractUrl(String key) {
return key.substring(key.indexOf("http"));
}
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getAllocationByteCount();
}
}

View File

@@ -16,20 +16,20 @@
package com.example.android.activityscenetransitionbasic;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.squareup.picasso.Picasso;
import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.util.Pair;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
/**
@@ -42,16 +42,11 @@ public class MainActivity extends Activity implements AdapterView.OnItemClickLis
private GridView mGridView;
private GridAdapter mAdapter;
private ImageLoader mImageLoader;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
// Retrieve the ImageLoader we are going to use for NetworkImageView
mImageLoader = new ImageLoader(Volley.newRequestQueue(this), ImageMemoryCache.INSTANCE);
// Setup the GridView and set the adapter
mGridView = (GridView) findViewById(R.id.grid);
mGridView.setOnItemClickListener(this);
@@ -74,23 +69,21 @@ public class MainActivity extends Activity implements AdapterView.OnItemClickLis
// BEGIN_INCLUDE(start_activity)
/**
* Now create an {@link android.app.ActivityOptions} instance using the
* {@link android.app.ActivityOptions#makeSceneTransitionAnimation(android.app.Activity, android.util.Pair[])} factory method.
* {@link ActivityOptionsCompat#makeSceneTransitionAnimation(Activity, Pair[])} factory
* method.
*/
ActivityOptions activityOptions = ActivityOptions.makeSceneTransitionAnimation(
ActivityOptionsCompat activityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(
this,
// Now we provide a list of Pair items which contain the view we can transitioning
// from, and the name of the view it is transitioning to, in the launched activity
new Pair<View, String>(
view.findViewById(R.id.imageview_item),
new Pair<View, String>(view.findViewById(R.id.imageview_item),
DetailActivity.VIEW_NAME_HEADER_IMAGE),
new Pair<View, String>(
view.findViewById(R.id.textview_name),
DetailActivity.VIEW_NAME_HEADER_TITLE)
);
new Pair<View, String>(view.findViewById(R.id.textview_name),
DetailActivity.VIEW_NAME_HEADER_TITLE));
// Now we can start the Activity, providing the activity options as a bundle
startActivity(intent, activityOptions.toBundle());
ActivityCompat.startActivity(this, intent, activityOptions.toBundle());
// END_INCLUDE(start_activity)
}
@@ -123,22 +116,13 @@ public class MainActivity extends Activity implements AdapterView.OnItemClickLis
final Item item = getItem(position);
// Load the thumbnail image
NetworkImageView image = (NetworkImageView) view.findViewById(R.id.imageview_item);
image.setImageUrl(item.getThumbnailUrl(), mImageLoader);
ImageView image = (ImageView) view.findViewById(R.id.imageview_item);
Picasso.with(image.getContext()).load(item.getThumbnailUrl()).into(image);
// Set the TextView's contents
TextView name = (TextView) view.findViewById(R.id.textview_name);
name.setText(item.getName());
// BEGIN_INCLUDE(grid_set_view_name)
/**
* As we're in an adapter we need to set each view's name dynamically, using the
* item's ID so that the names are unique.
*/
image.setTransitionName("grid:image:" + item.getId());
name.setTransitionName("grid:name:" + item.getId());
// END_INCLUDE(grid_set_view_name)
return view;
}
}