Home | History | Annotate | Download | only in toongame
      1 /*
      2  * Copyright (C) 2013 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.toongame;
     18 
     19 import android.content.Context;
     20 import android.graphics.Canvas;
     21 import android.graphics.Matrix;
     22 import android.graphics.RectF;
     23 import android.util.AttributeSet;
     24 import android.view.View;
     25 import android.widget.TextView;
     26 
     27 /**
     28  * This custom TextView can be skewed to the left or right to enable anticipation and
     29  * follow-through effects
     30  */
     31 public class SkewableTextView extends TextView {
     32 
     33     private float mSkewX;
     34     RectF mTempRect = new RectF();
     35 
     36     public SkewableTextView(Context context, AttributeSet attrs, int defStyle) {
     37         super(context, attrs, defStyle);
     38     }
     39 
     40     public SkewableTextView(Context context, AttributeSet attrs) {
     41         super(context, attrs);
     42     }
     43 
     44     public SkewableTextView(Context context) {
     45         super(context);
     46     }
     47 
     48     @Override
     49     protected void onDraw(Canvas canvas) {
     50         if (mSkewX != 0) {
     51             canvas.translate(0, getHeight());
     52             canvas.skew(mSkewX, 0);
     53             canvas.translate(0,  -getHeight());
     54         }
     55         super.onDraw(canvas);
     56     }
     57 
     58     public float getSkewX() {
     59         return mSkewX;
     60     }
     61 
     62     public void setSkewX(float value) {
     63         if (value != mSkewX) {
     64             mSkewX = value;
     65             invalidate();             // force redraw with new skew value
     66             invalidateSkewedBounds(); // also invalidate appropriate area of parent
     67         }
     68     }
     69 
     70     /**
     71      * Need to invalidate proper area of parent for skewed bounds
     72      */
     73     private void invalidateSkewedBounds() {
     74         if (mSkewX != 0) {
     75             Matrix matrix = new Matrix();
     76             matrix.setSkew(-mSkewX, 0);
     77             mTempRect.set(0, 0, getRight(), getBottom());
     78             matrix.mapRect(mTempRect);
     79             mTempRect.offset(getLeft() + getTranslationX(), getTop() + getTranslationY());
     80             ((View) getParent()).invalidate((int) mTempRect.left, (int) mTempRect.top,
     81                     (int) (mTempRect.right +.5f), (int) (mTempRect.bottom + .5f));
     82         }
     83     }
     84 }
     85