Home | History | Annotate | Download | only in clippingbasic
      1 /*
      2  * Copyright 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 package com.example.android.clippingbasic;
     17 
     18 import com.example.android.common.logger.Log;
     19 
     20 import android.graphics.Outline;
     21 import android.support.v4.app.Fragment;
     22 import android.os.Bundle;
     23 import android.view.LayoutInflater;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.view.ViewOutlineProvider;
     27 import android.widget.Button;
     28 import android.widget.TextView;
     29 
     30 /**
     31  * This sample shows how to clip a {@link View} using an {@link Outline}.
     32  */
     33 public class ClippingBasicFragment extends Fragment {
     34 
     35     private final static String TAG = "ClippingBasicFragment";
     36 
     37     /* Store the click count so that we can show a different text on every click. */
     38     private int mClickCount = 0;
     39 
     40     /* The {@Link Outline} used to clip the image with. */
     41     private ViewOutlineProvider mOutlineProvider;
     42 
     43     /* An array of texts. */
     44     private String[] mSampleTexts;
     45 
     46     /* A reference to a {@Link TextView} that shows different text strings when clicked. */
     47     private TextView mTextView;
     48 
     49     @Override
     50     public void onCreate(Bundle savedInstanceState) {
     51         super.onCreate(savedInstanceState);
     52         setHasOptionsMenu(true);
     53         mOutlineProvider = new ClipOutlineProvider();
     54         mSampleTexts = getResources().getStringArray(R.array.sample_texts);
     55     }
     56 
     57     @Override
     58     public View onCreateView(
     59             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     60         return inflater.inflate(R.layout.clipping_basic_fragment, container, false);
     61     }
     62 
     63     @Override
     64     public void onViewCreated(final View view, Bundle savedInstanceState) {
     65         super.onViewCreated(view, savedInstanceState);
     66 
     67         /* Set the initial text for the TextView. */
     68         mTextView = (TextView) view.findViewById(R.id.text_view);
     69         changeText();
     70 
     71 
     72         final View clippedView = view.findViewById(R.id.frame);
     73 
     74         /* Sets the OutlineProvider for the View. */
     75         clippedView.setOutlineProvider(mOutlineProvider);
     76 
     77         /* When the button is clicked, the text is clipped or un-clipped. */
     78         view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
     79             @Override
     80             public void onClick(View bt) {
     81                 // Toggle whether the View is clipped to the outline
     82                 if (clippedView.getClipToOutline()) {
     83                     /* The Outline is set for the View, but disable clipping. */
     84                     clippedView.setClipToOutline(false);
     85 
     86                     Log.d(TAG, String.format("Clipping to outline is disabled"));
     87                     ((Button) bt).setText(R.string.clip_button);
     88                 } else {
     89                     /* Enables clipping on the View. */
     90                     clippedView.setClipToOutline(true);
     91 
     92                     Log.d(TAG, String.format("Clipping to outline is enabled"));
     93                     ((Button) bt).setText(R.string.unclip_button);
     94                 }
     95             }
     96         });
     97 
     98         /* When the text is clicked, a new string is shown. */
     99         view.findViewById(R.id.text_view).setOnClickListener(new View.OnClickListener() {
    100             @Override
    101             public void onClick(View view) {
    102                 mClickCount++;
    103 
    104                 // Update the text in the TextView
    105                 changeText();
    106 
    107                 // Invalidate the outline just in case the TextView changed size
    108                 clippedView.invalidateOutline();
    109             }
    110         });
    111     }
    112 
    113     private void changeText() {
    114         // Compute the position of the string in the array using the number of strings
    115         //  and the number of clicks.
    116         String newText = mSampleTexts[mClickCount % mSampleTexts.length];
    117 
    118         /* Once the text is selected, change the TextView */
    119         mTextView.setText(newText);
    120         Log.d(TAG, String.format("Text was changed."));
    121 
    122 
    123     }
    124 
    125     /**
    126      * A {@link ViewOutlineProvider} which clips the view with a rounded rectangle which is inset
    127      * by 10%
    128      */
    129     private class ClipOutlineProvider extends ViewOutlineProvider {
    130 
    131         @Override
    132         public void getOutline(View view, Outline outline) {
    133             final int margin = Math.min(view.getWidth(), view.getHeight()) / 10;
    134             outline.setRoundRect(margin, margin, view.getWidth() - margin,
    135                     view.getHeight() - margin, margin / 2);
    136         }
    137 
    138     }
    139 }