Home | History | Annotate | Download | only in hwui
      1 /*
      2  * Copyright (C) 2012 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.android.test.hwui;
     18 
     19 import android.animation.ObjectAnimator;
     20 import android.animation.ValueAnimator;
     21 import android.app.Activity;
     22 import android.content.Context;
     23 import android.graphics.Canvas;
     24 import android.graphics.Color;
     25 import android.graphics.Paint;
     26 import android.os.Bundle;
     27 import android.text.Spannable;
     28 import android.text.SpannableStringBuilder;
     29 import android.text.style.BackgroundColorSpan;
     30 import android.text.style.ForegroundColorSpan;
     31 import android.text.style.ImageSpan;
     32 import android.text.style.SuggestionSpan;
     33 import android.text.style.UnderlineSpan;
     34 import android.view.View;
     35 import android.widget.Button;
     36 import android.widget.EditText;
     37 import android.widget.LinearLayout;
     38 import android.widget.TextView;
     39 
     40 public class ViewPropertyAlphaActivity extends Activity {
     41 
     42     MyView myViewAlphaDefault, myViewAlphaHandled;
     43 
     44     @Override
     45     protected void onCreate(Bundle savedInstanceState) {
     46         super.onCreate(savedInstanceState);
     47 
     48         setContentView(R.layout.view_properties);
     49 
     50         getWindow().getDecorView().postDelayed(new Runnable() {
     51             @Override
     52             public void run() {
     53                 startAnim(R.id.button);
     54                 startAnim(R.id.textview);
     55                 startAnim(R.id.spantext);
     56                 startAnim(R.id.edittext);
     57                 startAnim(R.id.selectedtext);
     58                 startAnim(R.id.textviewbackground);
     59                 startAnim(R.id.layout);
     60                 startAnim(R.id.imageview);
     61                 startAnim(myViewAlphaDefault);
     62                 startAnim(myViewAlphaHandled);
     63                 EditText selectedText = (EditText) findViewById(R.id.selectedtext);
     64                 selectedText.setSelection(3, 8);
     65             }
     66         }, 2000);
     67 
     68         Button invalidator = (Button) findViewById(R.id.invalidateButton);
     69         invalidator.setOnClickListener(new View.OnClickListener() {
     70             @Override
     71             public void onClick(View v) {
     72                 findViewById(R.id.textview).invalidate();
     73                 findViewById(R.id.spantext).invalidate();
     74             }
     75         });
     76 
     77         TextView textView = (TextView) findViewById(R.id.spantext);
     78         if (textView != null) {
     79             SpannableStringBuilder text =
     80                     new SpannableStringBuilder("Now this is a short text message with spans");
     81 
     82             text.setSpan(new BackgroundColorSpan(Color.RED), 0, 3,
     83                     Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     84             text.setSpan(new ForegroundColorSpan(Color.BLUE), 4, 9,
     85                     Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     86             text.setSpan(new SuggestionSpan(this, new String[]{"longer"}, 3), 11, 16,
     87                     Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     88             text.setSpan(new UnderlineSpan(), 17, 20,
     89                     Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     90             text.setSpan(new ImageSpan(this, R.drawable.icon), 21, 22,
     91                     Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     92 
     93             textView.setText(text);
     94         }
     95 
     96         LinearLayout container = (LinearLayout) findViewById(R.id.container);
     97         myViewAlphaDefault = new MyView(this, false);
     98         myViewAlphaDefault.setLayoutParams(new LinearLayout.LayoutParams(75, 75));
     99         container.addView(myViewAlphaDefault);
    100         myViewAlphaHandled = new MyView(this, true);
    101         myViewAlphaHandled.setLayoutParams(new LinearLayout.LayoutParams(75, 75));
    102         container.addView(myViewAlphaHandled);
    103     }
    104 
    105     private void startAnim(View target) {
    106         ObjectAnimator anim = ObjectAnimator.ofFloat(target, View.ALPHA, 0);
    107         anim.setRepeatCount(ValueAnimator.INFINITE);
    108         anim.setRepeatMode(ValueAnimator.REVERSE);
    109         anim.setDuration(1000);
    110         anim.start();
    111     }
    112     private void startAnim(int id) {
    113         startAnim(findViewById(id));
    114     }
    115 
    116     private static class MyView extends View {
    117         private int mMyAlpha = 255;
    118         private boolean mHandleAlpha;
    119         private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    120 
    121         private MyView(Context context, boolean handleAlpha) {
    122             super(context);
    123             mHandleAlpha = handleAlpha;
    124             mPaint.setColor(Color.RED);
    125         }
    126 
    127         @Override
    128         protected void onDraw(Canvas canvas) {
    129             if (mHandleAlpha) {
    130                 mPaint.setAlpha(mMyAlpha);
    131             }
    132             canvas.drawCircle(30, 30, 30, mPaint);
    133         }
    134 
    135         @Override
    136         protected boolean onSetAlpha(int alpha) {
    137             if (mHandleAlpha) {
    138                 mMyAlpha = alpha;
    139                 return true;
    140             }
    141             return super.onSetAlpha(alpha);
    142         }
    143     }
    144 
    145 }
    146