Home | History | Annotate | Download | only in hwui
      1 /*
      2  * Copyright (C) 2016 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.app.Activity;
     21 import android.content.Context;
     22 import android.graphics.Canvas;
     23 import android.graphics.Color;
     24 import android.os.Bundle;
     25 import android.view.Gravity;
     26 import android.view.SurfaceHolder;
     27 import android.view.SurfaceHolder.Callback;
     28 import android.view.SurfaceView;
     29 import android.view.animation.LinearInterpolator;
     30 import android.widget.FrameLayout;
     31 
     32 public class MovingSurfaceViewActivity extends Activity implements Callback {
     33     SurfaceView mSurfaceView;
     34     ObjectAnimator mAnimator;
     35 
     36     class MySurfaceView extends SurfaceView {
     37         boolean mSlow;
     38         boolean mScaled;
     39         int mToggle = 0;
     40 
     41         public MySurfaceView(Context context) {
     42             super(context);
     43             setOnClickListener(v -> {
     44                 mToggle = (mToggle + 1) % 4;
     45                 mSlow = (mToggle & 0x2) != 0;
     46                 mScaled = (mToggle & 0x1) != 0;
     47 
     48                 mSurfaceView.setScaleX(mScaled ? 1.6f : 1f);
     49                 mSurfaceView.setScaleY(mScaled ? 0.8f : 1f);
     50 
     51                 setTitle("Slow=" + mSlow + ", scaled=" + mScaled);
     52                 invalidate();
     53             });
     54             setWillNotDraw(false);
     55         }
     56 
     57         @Override
     58         public void draw(Canvas canvas) {
     59             super.draw(canvas);
     60             if (mSlow) {
     61                 try {
     62                     Thread.sleep(16);
     63                 } catch (InterruptedException e) {}
     64             }
     65         }
     66 
     67         public void setMyTranslationY(float ty) {
     68             setTranslationY(ty);
     69             if (mSlow) {
     70                 invalidate();
     71             }
     72         }
     73 
     74         public float getMyTranslationY() {
     75             return getTranslationY();
     76         }
     77     }
     78 
     79     @Override
     80     protected void onCreate(Bundle savedInstanceState) {
     81         super.onCreate(savedInstanceState);
     82 
     83         FrameLayout content = new FrameLayout(this);
     84 
     85         mSurfaceView = new MySurfaceView(this);
     86         mSurfaceView.getHolder().addCallback(this);
     87 
     88         final float density = getResources().getDisplayMetrics().density;
     89         int size = (int) (200 * density);
     90 
     91         content.addView(mSurfaceView, new FrameLayout.LayoutParams(
     92                 size, size, Gravity.CENTER_HORIZONTAL | Gravity.TOP));
     93         mAnimator = ObjectAnimator.ofFloat(mSurfaceView, "myTranslationY",
     94                 0, size);
     95         mAnimator.setRepeatMode(ObjectAnimator.REVERSE);
     96         mAnimator.setRepeatCount(ObjectAnimator.INFINITE);
     97         mAnimator.setDuration(200);
     98         mAnimator.setInterpolator(new LinearInterpolator());
     99         setContentView(content);
    100     }
    101 
    102     @Override
    103     public void surfaceCreated(SurfaceHolder holder) {
    104     }
    105 
    106     @Override
    107     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    108         Canvas canvas = holder.lockCanvas();
    109         canvas.drawColor(Color.WHITE);
    110         holder.unlockCanvasAndPost(canvas);
    111     }
    112 
    113     @Override
    114     public void surfaceDestroyed(SurfaceHolder holder) {
    115     }
    116 
    117     @Override
    118     protected void onResume() {
    119         super.onResume();
    120         mAnimator.start();
    121     }
    122 
    123     @Override
    124     protected void onPause() {
    125         mAnimator.pause();
    126         super.onPause();
    127     }
    128 }
    129