<!-- This is an example layout containing thumbnail image buttons that, when pressed, zoom in to show more detail. All of the zooming and animation logic is in the ZoomActivity class. --> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:orientation="horizontal">
<!-- These buttons don't have any decorations (3D bevel, etc.), but it's still important to show feedback on touch or focus. The custom "ToughHighlightImageButton" ImageButton subclass helps achieve this by drawing the standard system "pressed" and "focused" overlay upon user interaction. -->
<!-- This initially-hidden ImageView will hold the expanded/zoomed version of the images above. Without transformations applied, it takes up the entire screen. To achieve the "zoom" animation, this view's bounds are animated from the bounds of the thumbnail buttons above, to its final laid-out bounds. The implementation of this animation is in the ZoomActivity class. --> <ImageView android:id="@+id/expanded_image" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="invisible" android:contentDescription="@string/description_zoom_touch_close" />
publicclassTouchHighlightImageButtonextendsImageButton{ /** * The highlight drawable. This generally a {@link android.graphics.drawable.StateListDrawable} * that's transparent in the default state, and contains a semi-transparent overlay * for the focused and pressed states. */ private Drawable mForegroundDrawable;
/** * The cached bounds of the view. */ private Rect mCachedBounds = new Rect(); publicTouchHighlightImageButton(Context context, AttributeSet attrs){ super(context, attrs); init(); } /** * General view initialization used common to all constructors of the view. */ privatevoidinit(){ // Reset default ImageButton background and padding. setBackgroundColor(0); setPadding(0, 0, 0, 0);
// Retrieve the drawable resource assigned to the android.R.attr.selectableItemBackground // theme attribute from the current theme. TypedArray a = getContext() .obtainStyledAttributes(newint[]{android.R.attr.selectableItemBackground}); mForegroundDrawable = a.getDrawable(0); mForegroundDrawable.setCallback(this); a.recycle(); } @Override protectedvoiddrawableStateChanged(){ super.drawableStateChanged();
// Update the state of the highlight drawable to match // the state of the button. if (mForegroundDrawable.isStateful()) { mForegroundDrawable.setState(getDrawableState()); }
// Trigger a redraw. invalidate(); }
@Override protectedvoidonDraw(Canvas canvas){ // First draw the image. super.onDraw(canvas);
// Then draw the highlight on top of it. If the button is neither focused // nor pressed, the drawable will be transparent, so just the image // will be drawn. mForegroundDrawable.setBounds(mCachedBounds); mForegroundDrawable.draw(canvas); }
@Override protectedvoidonSizeChanged(int w, int h, int oldw, int oldh){ super.onSizeChanged(w, h, oldw, oldh);