Cherrypick from Change-Id: I4ae984cf776604cede32b98db6b1eb3cb6c59c7e
First set of demo apps for DevBytes videos Change-Id: I696b184aaa8058f01c0369a173aae09e38a8cb08
This commit is contained in:
67
samples/devbytes/animation/Bouncer/AndroidManifest.xml
Normal file
67
samples/devbytes/animation/Bouncer/AndroidManifest.xml
Normal file
@@ -0,0 +1,67 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.example.android.bouncer"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0" >
|
||||
|
||||
<uses-sdk
|
||||
android:minSdkVersion="14"
|
||||
android:targetSdkVersion="15" />
|
||||
|
||||
<application
|
||||
android:icon="@drawable/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/AppTheme" >
|
||||
<activity
|
||||
android:name=".Bouncer"
|
||||
android:label="@string/title_activity_bouncer" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity
|
||||
android:name=".Bouncer1"
|
||||
android:label="@string/title_activity_bouncer1" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity
|
||||
android:name=".Bouncer2"
|
||||
android:label="@string/title_activity_bouncer2" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity
|
||||
android:name="com.example.android.bouncer.Bouncer3"
|
||||
android:label="@string/title_activity_bouncer3" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 9.2 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 5.1 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 5.2 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
@@ -0,0 +1,28 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:orientation="vertical"
|
||||
android:background="#007F00"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" >
|
||||
|
||||
<view android:id="@+id/myview"
|
||||
class="com.example.android.bouncer.Bouncer$MyView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".Bouncer" />
|
||||
|
||||
</LinearLayout>
|
||||
@@ -0,0 +1,28 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:orientation="vertical"
|
||||
android:background="#007F00"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" >
|
||||
|
||||
<view android:id="@+id/myview"
|
||||
class="com.example.android.bouncer.Bouncer1$MyView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".Bouncer" />
|
||||
|
||||
</LinearLayout>
|
||||
@@ -0,0 +1,28 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:orientation="vertical"
|
||||
android:background="#007F00"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" >
|
||||
|
||||
<view android:id="@+id/myview"
|
||||
class="com.example.android.bouncer.Bouncer2$MyView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".Bouncer" />
|
||||
|
||||
</LinearLayout>
|
||||
@@ -0,0 +1,28 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:orientation="vertical"
|
||||
android:background="#007F00"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" >
|
||||
|
||||
<view android:id="@+id/myview"
|
||||
class="com.example.android.bouncer.Bouncer3$MyView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".Bouncer" />
|
||||
|
||||
</LinearLayout>
|
||||
19
samples/devbytes/animation/Bouncer/res/values-v11/styles.xml
Normal file
19
samples/devbytes/animation/Bouncer/res/values-v11/styles.xml
Normal file
@@ -0,0 +1,19 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<resources>
|
||||
|
||||
<style name="AppTheme" parent="android:Theme.Holo.Light" />
|
||||
|
||||
</resources>
|
||||
19
samples/devbytes/animation/Bouncer/res/values-v14/styles.xml
Normal file
19
samples/devbytes/animation/Bouncer/res/values-v14/styles.xml
Normal file
@@ -0,0 +1,19 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<resources>
|
||||
|
||||
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar" />
|
||||
|
||||
</resources>
|
||||
23
samples/devbytes/animation/Bouncer/res/values/strings.xml
Normal file
23
samples/devbytes/animation/Bouncer/res/values/strings.xml
Normal file
@@ -0,0 +1,23 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<resources>
|
||||
|
||||
<string name="app_name">Bouncer</string>
|
||||
<string name="title_activity_bouncer">Bouncer</string>
|
||||
<string name="title_activity_bouncer1">Bouncer1</string>
|
||||
<string name="title_activity_bouncer2">Bouncer2</string>
|
||||
<string name="title_activity_bouncer3">Bouncer3</string>
|
||||
|
||||
</resources>
|
||||
19
samples/devbytes/animation/Bouncer/res/values/styles.xml
Normal file
19
samples/devbytes/animation/Bouncer/res/values/styles.xml
Normal file
@@ -0,0 +1,19 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<resources>
|
||||
|
||||
<style name="AppTheme" parent="android:Theme.Light" />
|
||||
|
||||
</resources>
|
||||
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.bouncer;
|
||||
|
||||
import android.animation.ValueAnimator;
|
||||
import android.animation.ValueAnimator.AnimatorUpdateListener;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.os.Bundle;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
|
||||
/**
|
||||
* This example shows simple uses of ValueAnimator, ObjectAnimator, and interpolators
|
||||
* to control how a shape is moved around on the screen.
|
||||
*
|
||||
* The Bouncer1, Bouncer2, and Vouncer3 files are exactly like this one except for variations
|
||||
* in how the animation is set up and run.
|
||||
*
|
||||
* Watch the associated video for this demo on the DevBytes channel of developer.android.com
|
||||
* or on YouTube at https://www.youtube.com/watch?v=vCTcmPIKgpM.
|
||||
*/
|
||||
public class Bouncer extends Activity {
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_bouncer);
|
||||
}
|
||||
|
||||
/**
|
||||
* A custom view is used to paint the green background and the shape.
|
||||
*/
|
||||
static class MyView extends View {
|
||||
|
||||
Bitmap mBitmap;
|
||||
Paint paint = new Paint();
|
||||
int mShapeX, mShapeY;
|
||||
int mShapeW, mShapeH;
|
||||
|
||||
public MyView(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
setupShape();
|
||||
}
|
||||
|
||||
public MyView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
setupShape();
|
||||
}
|
||||
|
||||
public MyView(Context context) {
|
||||
super(context);
|
||||
setupShape();
|
||||
}
|
||||
|
||||
private void setupShape() {
|
||||
mBitmap = BitmapFactory.decodeResource(getResources(),
|
||||
R.drawable.electricsheep);
|
||||
mShapeW = mBitmap.getWidth();
|
||||
mShapeH = mBitmap.getHeight();
|
||||
setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
startAnimation();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Moving the shape in x or y causes an invalidation of the area it used to occupy
|
||||
* plus the area it now occupies.
|
||||
*/
|
||||
public void setShapeX(int shapeX) {
|
||||
int minX = mShapeX;
|
||||
int maxX = mShapeX + mShapeW;
|
||||
mShapeX = shapeX;
|
||||
minX = Math.min(mShapeX, minX);
|
||||
maxX = Math.max(mShapeX + mShapeW, maxX);
|
||||
invalidate(minX, mShapeY, maxX, mShapeY + mShapeH);
|
||||
}
|
||||
|
||||
/**
|
||||
* Moving the shape in x or y causes an invalidation of the area it used to occupy
|
||||
* plus the area it now occupies.
|
||||
*/
|
||||
public void setShapeY(int shapeY) {
|
||||
int minY = mShapeY;
|
||||
int maxY = mShapeY + mShapeH;
|
||||
mShapeY = shapeY;
|
||||
minY = Math.min(mShapeY, minY);
|
||||
maxY = Math.max(mShapeY + mShapeH, maxY);
|
||||
invalidate(mShapeX, minY, mShapeX + mShapeW, maxY);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
||||
mShapeX = (w - mBitmap.getWidth()) / 2;
|
||||
mShapeY = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
canvas.drawBitmap(mBitmap, mShapeX, mShapeY, paint);
|
||||
}
|
||||
|
||||
void startAnimation() {
|
||||
// This version uses ValueAnimator, which requires adding an update
|
||||
// listener and manually updating the shape position on each frame.
|
||||
ValueAnimator anim = getValueAnimator();
|
||||
anim.start();
|
||||
}
|
||||
|
||||
ValueAnimator getValueAnimator() {
|
||||
ValueAnimator anim = ValueAnimator.ofFloat(0, 1);
|
||||
anim.addUpdateListener(new AnimatorUpdateListener() {
|
||||
@Override
|
||||
public void onAnimationUpdate(ValueAnimator animation) {
|
||||
setShapeY((int) (animation.getAnimatedFraction() *
|
||||
(getHeight() - mShapeH)));
|
||||
}
|
||||
});
|
||||
return anim;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.bouncer;
|
||||
|
||||
import android.animation.ValueAnimator;
|
||||
import android.animation.ValueAnimator.AnimatorUpdateListener;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.os.Bundle;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
|
||||
/**
|
||||
* See the comments in Bouncer for the overall functionality of this app. Changes for this
|
||||
* variation are down in the animation setup code.
|
||||
*/
|
||||
public class Bouncer1 extends Activity {
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_bouncer1);
|
||||
}
|
||||
|
||||
static class MyView extends View {
|
||||
|
||||
Bitmap mBitmap;
|
||||
Paint paint = new Paint();
|
||||
int mShapeX, mShapeY;
|
||||
int mShapeW, mShapeH;
|
||||
|
||||
public MyView(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
setupShape();
|
||||
}
|
||||
|
||||
public MyView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
setupShape();
|
||||
}
|
||||
|
||||
public MyView(Context context) {
|
||||
super(context);
|
||||
setupShape();
|
||||
}
|
||||
|
||||
public void setShapeX(int shapeX) {
|
||||
int minX = mShapeX;
|
||||
int maxX = mShapeX + mShapeW;
|
||||
mShapeX = shapeX;
|
||||
minX = Math.min(mShapeX, minX);
|
||||
maxX = Math.max(mShapeX + mShapeW, maxX);
|
||||
invalidate(minX, mShapeY, maxX, mShapeY + mShapeH);
|
||||
}
|
||||
|
||||
public void setShapeY(int shapeY) {
|
||||
int minY = mShapeY;
|
||||
int maxY = mShapeY + mShapeH;
|
||||
mShapeY = shapeY;
|
||||
minY = Math.min(mShapeY, minY);
|
||||
maxY = Math.max(mShapeY + mShapeH, maxY);
|
||||
invalidate(mShapeX, minY, mShapeX + mShapeW, maxY);
|
||||
}
|
||||
|
||||
private void setupShape() {
|
||||
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.electricsheep);
|
||||
mShapeW = mBitmap.getWidth();
|
||||
mShapeH = mBitmap.getHeight();
|
||||
setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
startAnimation();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
||||
mShapeX = (w - mBitmap.getWidth()) / 2;
|
||||
mShapeY = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
canvas.drawBitmap(mBitmap, mShapeX, mShapeY, paint);
|
||||
}
|
||||
|
||||
void startAnimation() {
|
||||
ValueAnimator anim = getValueAnimator();
|
||||
// In this variation, we put the shape into an infinite bounce, where it keeps moving
|
||||
// up and down. Note that it's still not actually "bouncing" because it just uses
|
||||
// default time interpolation.
|
||||
anim.setRepeatCount(ValueAnimator.INFINITE);
|
||||
anim.setRepeatMode(ValueAnimator.REVERSE);
|
||||
anim.start();
|
||||
}
|
||||
|
||||
ValueAnimator getValueAnimator() {
|
||||
ValueAnimator anim = ValueAnimator.ofFloat(0, 1);
|
||||
anim.addUpdateListener(new AnimatorUpdateListener() {
|
||||
@Override
|
||||
public void onAnimationUpdate(ValueAnimator animation) {
|
||||
setShapeY((int) (animation.getAnimatedFraction() * (getHeight() - mShapeH)));
|
||||
}
|
||||
});
|
||||
return anim;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.bouncer;
|
||||
|
||||
import android.animation.ValueAnimator;
|
||||
import android.animation.ValueAnimator.AnimatorUpdateListener;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.os.Bundle;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.view.animation.AccelerateInterpolator;
|
||||
|
||||
/**
|
||||
* See the comments in Bouncer for the overall functionality of this app. Changes for this
|
||||
* variation are down in the animation setup code.
|
||||
*/
|
||||
public class Bouncer2 extends Activity {
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_bouncer2);
|
||||
}
|
||||
|
||||
static class MyView extends View {
|
||||
|
||||
Bitmap mBitmap;
|
||||
Paint paint = new Paint();
|
||||
int mShapeX, mShapeY;
|
||||
int mShapeW, mShapeH;
|
||||
|
||||
public MyView(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
setupShape();
|
||||
}
|
||||
|
||||
public MyView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
setupShape();
|
||||
}
|
||||
|
||||
public MyView(Context context) {
|
||||
super(context);
|
||||
setupShape();
|
||||
}
|
||||
|
||||
public void setShapeX(int shapeX) {
|
||||
int minX = mShapeX;
|
||||
int maxX = mShapeX + mShapeW;
|
||||
mShapeX = shapeX;
|
||||
minX = Math.min(mShapeX, minX);
|
||||
maxX = Math.max(mShapeX + mShapeW, maxX);
|
||||
invalidate(minX, mShapeY, maxX, mShapeY + mShapeH);
|
||||
}
|
||||
|
||||
public void setShapeY(int shapeY) {
|
||||
int minY = mShapeY;
|
||||
int maxY = mShapeY + mShapeH;
|
||||
mShapeY = shapeY;
|
||||
minY = Math.min(mShapeY, minY);
|
||||
maxY = Math.max(mShapeY + mShapeH, maxY);
|
||||
invalidate(mShapeX, minY, mShapeX + mShapeW, maxY);
|
||||
}
|
||||
|
||||
private void setupShape() {
|
||||
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.electricsheep);
|
||||
mShapeW = mBitmap.getWidth();
|
||||
mShapeH = mBitmap.getHeight();
|
||||
setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
startAnimation();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
||||
mShapeX = (w - mBitmap.getWidth()) / 2;
|
||||
mShapeY = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
canvas.drawBitmap(mBitmap, mShapeX, mShapeY, paint);
|
||||
}
|
||||
|
||||
void startAnimation() {
|
||||
ValueAnimator anim = getValueAnimator();
|
||||
anim.setRepeatCount(ValueAnimator.INFINITE);
|
||||
anim.setRepeatMode(ValueAnimator.REVERSE);
|
||||
// This variation finally has the shape bouncing, due to the use of an
|
||||
// AccelerateInterpolator, which speeds up as the animation proceed. Note that
|
||||
// when the animation reverses, the interpolator acts in reverse, decelerating
|
||||
// movement.
|
||||
anim.setInterpolator(new AccelerateInterpolator());
|
||||
anim.start();
|
||||
}
|
||||
|
||||
ValueAnimator getValueAnimator() {
|
||||
ValueAnimator anim = ValueAnimator.ofFloat(0, 1);
|
||||
anim.addUpdateListener(new AnimatorUpdateListener() {
|
||||
@Override
|
||||
public void onAnimationUpdate(ValueAnimator animation) {
|
||||
setShapeY((int) (animation.getAnimatedFraction() * (getHeight() - mShapeH)));
|
||||
}
|
||||
});
|
||||
return anim;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.bouncer;
|
||||
|
||||
import android.animation.ObjectAnimator;
|
||||
import android.animation.ValueAnimator;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.os.Bundle;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.view.animation.AccelerateInterpolator;
|
||||
|
||||
/**
|
||||
* See the comments in Bouncer for the overall functionality of this app. Changes for this
|
||||
* variation are down in the animation setup code.
|
||||
*/
|
||||
public class Bouncer3 extends Activity {
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_bouncer3);
|
||||
}
|
||||
|
||||
static class MyView extends View {
|
||||
|
||||
Bitmap mBitmap;
|
||||
Paint paint = new Paint();
|
||||
int mShapeX, mShapeY;
|
||||
int mShapeW, mShapeH;
|
||||
|
||||
public MyView(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
setupShape();
|
||||
}
|
||||
|
||||
public MyView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
setupShape();
|
||||
}
|
||||
|
||||
public MyView(Context context) {
|
||||
super(context);
|
||||
setupShape();
|
||||
}
|
||||
|
||||
public void setShapeX(int shapeX) {
|
||||
int minX = mShapeX;
|
||||
int maxX = mShapeX + mShapeW;
|
||||
mShapeX = shapeX;
|
||||
minX = Math.min(mShapeX, minX);
|
||||
maxX = Math.max(mShapeX + mShapeW, maxX);
|
||||
invalidate(minX, mShapeY, maxX, mShapeY + mShapeH);
|
||||
}
|
||||
|
||||
public void setShapeY(int shapeY) {
|
||||
int minY = mShapeY;
|
||||
int maxY = mShapeY + mShapeH;
|
||||
mShapeY = shapeY;
|
||||
minY = Math.min(mShapeY, minY);
|
||||
maxY = Math.max(mShapeY + mShapeH, maxY);
|
||||
invalidate(mShapeX, minY, mShapeX + mShapeW, maxY);
|
||||
}
|
||||
|
||||
private void setupShape() {
|
||||
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.electricsheep);
|
||||
mShapeW = mBitmap.getWidth();
|
||||
mShapeH = mBitmap.getHeight();
|
||||
setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
startAnimation();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
||||
mShapeX = (w - mBitmap.getWidth()) / 2;
|
||||
mShapeY = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
canvas.drawBitmap(mBitmap, mShapeX, mShapeY, paint);
|
||||
}
|
||||
|
||||
void startAnimation() {
|
||||
// This variation uses an ObjectAnimator. The functionality is exactly the same as
|
||||
// in Bouncer2, but this time the boilerplate code is greatly reduced because we
|
||||
// tell ObjectAnimator to automatically animate the target object for us, so we no
|
||||
// longer need to listen for frame updates and do that work ourselves.
|
||||
ObjectAnimator anim = getObjectAnimator();
|
||||
anim.setRepeatCount(ValueAnimator.INFINITE);
|
||||
anim.setRepeatMode(ValueAnimator.REVERSE);
|
||||
anim.setInterpolator(new AccelerateInterpolator());
|
||||
anim.start();
|
||||
}
|
||||
|
||||
ObjectAnimator getObjectAnimator() {
|
||||
return ObjectAnimator.ofInt(this, "shapeY", 0, (getHeight() - mShapeH));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user