Skip to content

Commit

Permalink
animation + bitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
rengwuxian committed Oct 24, 2019
1 parent 65f41df commit 0febc92
Show file tree
Hide file tree
Showing 66 changed files with 1,510 additions and 1 deletion.
1 change: 1 addition & 0 deletions 11-animation/.gitignore
@@ -0,0 +1 @@
/build
35 changes: 35 additions & 0 deletions 11-animation/build.gradle
@@ -0,0 +1,35 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 28
buildToolsVersion "29.0.0"


defaultConfig {
applicationId "com.hencoder.a11_animation"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
21 changes: 21 additions & 0 deletions 11-animation/proguard-rules.pro
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
@@ -0,0 +1,27 @@
package com.hencoder.a11_animation;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

assertEquals("com.hencoder.a11_animation", appContext.getPackageName());
}
}
21 changes: 21 additions & 0 deletions 11-animation/src/main/AndroidManifest.xml
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hencoder.a11_animation">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
@@ -0,0 +1,106 @@
package com.hencoder.a11_animation;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.AnimatorSet;
import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TypeEvaluator;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;

import com.hencoder.a11_animation.view.CameraView;
import com.hencoder.a11_animation.view.CircleView;
import com.hencoder.a11_animation.view.PointView;
import com.hencoder.a11_animation.view.ProvinceView;

public class MainActivity extends AppCompatActivity {
ProvinceView view;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

view = findViewById(R.id.view);

/*ObjectAnimator animator = ObjectAnimator.ofFloat(view, "radius", Utils.dp2px(200));
animator.setStartDelay(2000);
animator.start();*/

/*ObjectAnimator bottomFlipAnimator = ObjectAnimator.ofFloat(view, "bottomFlip", 45);
bottomFlipAnimator.setDuration(1000);
bottomFlipAnimator.setStartDelay(1000);
ObjectAnimator topFlipAnimator = ObjectAnimator.ofFloat(view, "topFlip", -45);
topFlipAnimator.setDuration(1000);
topFlipAnimator.setStartDelay(1000);
ObjectAnimator flipRotationAnimator = ObjectAnimator.ofFloat(view, "flipRotation", 270);
flipRotationAnimator.setDuration(1000);
flipRotationAnimator.setStartDelay(1000);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(bottomFlipAnimator, flipRotationAnimator, topFlipAnimator);
animatorSet.setStartDelay(1000);
animatorSet.start();*/

/*PropertyValuesHolder bottomFlipHolder = PropertyValuesHolder.ofFloat("bottomFlip", 45);
PropertyValuesHolder topFlipHolder = PropertyValuesHolder.ofFloat("topFlip", -45);
PropertyValuesHolder flipRotationHolder = PropertyValuesHolder.ofFloat("flipRotation", 270);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, bottomFlipHolder, topFlipHolder, flipRotationHolder);
animator.setDuration(1000);
animator.setStartDelay(1000);
animator.start();*/

/*ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", Utils.dp2px(200));
animator.setStartDelay(2000);
animator.setDuration(2000);
animator.start();*/

/*float length = Utils.dp2px(300);
Keyframe keyframe1 = Keyframe.ofFloat(0, 0);
Keyframe keyframe2 = Keyframe.ofFloat(0.1f, 1.5f * length);
Keyframe keyframe3 = Keyframe.ofFloat(0.9f, 0.6f * length);
Keyframe keyframe4 = Keyframe.ofFloat(1f, 1f * length);
PropertyValuesHolder valuesHolder = PropertyValuesHolder.ofKeyframe("translationX",
keyframe1, keyframe2, keyframe3, keyframe4);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, valuesHolder);
animator.setStartDelay(2000);
animator.setDuration(2000);
animator.setInterpolator(new LinearInterpolator());
animator.start();*/

/*PointF targetPoint = new PointF(Utils.dp2px(300), Utils.dp2px(200));
ObjectAnimator animator = ObjectAnimator.ofObject(view, "point", new PointFEvaluator(), targetPoint);
animator.setStartDelay(2000);
animator.setDuration(2000);
animator.start();*/

ObjectAnimator animator = ObjectAnimator.ofObject(view, "province", new ProvinceUtil.ProvinceEvaluator(), "澳门特别行政区");
animator.setStartDelay(2000);
animator.setDuration(10000);
animator.start();

view.animate()
.translationX(200)
.rotation(180)
.withLayer()
.alpha(0.5f);
}

static class PointFEvaluator implements TypeEvaluator<PointF> {
@Override
public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
float x = startValue.x + (endValue.x - startValue.x) * fraction;
float y = startValue.y + (endValue.y - startValue.y) * fraction;
return new PointF(x, y);
}
}
}
@@ -0,0 +1,53 @@
package com.hencoder.a11_animation;

import android.animation.TypeEvaluator;

import java.util.Arrays;
import java.util.List;

public class ProvinceUtil {
public static List<String> provinces = Arrays.asList("北京市",
"天津市",
"上海市",
"重庆市",
"河北省",
"山西省",
"辽宁省",
"吉林省",
"黑龙江省",
"江苏省",
"浙江省",
"安徽省",
"福建省",
"江西省",
"山东省",
"河南省",
"湖北省",
"湖南省",
"广东省",
"海南省",
"四川省",
"贵州省",
"云南省",
"陕西省",
"甘肃省",
"青海省",
"台湾省",
"内蒙古自治区",
"广西壮族自治区",
"西藏自治区",
"宁夏回族自治区",
"新疆维吾尔自治区",
"香港特别行政区",
"澳门特别行政区");

static class ProvinceEvaluator implements TypeEvaluator<String> {
@Override
public String evaluate(float fraction, String startValue, String endValue) {
int startIndex = provinces.indexOf(startValue);
int endIndex = provinces.indexOf(endValue);
int currentIndex = (int) (startIndex + (endIndex - startIndex) * fraction);
return provinces.get(currentIndex);
}
}
}
26 changes: 26 additions & 0 deletions 11-animation/src/main/java/com/hencoder/a11_animation/Utils.java
@@ -0,0 +1,26 @@
package com.hencoder.a11_animation;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.TypedValue;

public class Utils {
public static float dp2px(float dp) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().getDisplayMetrics());
}

public static Bitmap getAvatar(Resources res, int width) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, R.drawable.avatar_rengwuxian, options);
options.inJustDecodeBounds = false;
options.inDensity = options.outWidth;
options.inTargetDensity = width;
return BitmapFactory.decodeResource(res, R.drawable.avatar_rengwuxian, options);
}

public static float getZForCamera() {
return - 4 * Resources.getSystem().getDisplayMetrics().density;
}
}
@@ -0,0 +1,93 @@
package com.hencoder.a11_animation.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.hencoder.a11_animation.Utils;

public class CameraView extends View {
private static final float IMAGE_WIDTH = Utils.dp2px(200);
private static final float IMAGE_OFFSET = Utils.dp2px(100);

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Bitmap bitmap;
Camera camera = new Camera();

float topFlip = 0;
float bottomFlip = 0;
float flipRotation = 0;

public CameraView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

{
bitmap = Utils.getAvatar(getResources(), (int) IMAGE_WIDTH);
camera.setLocation(0, 0, Utils.getZForCamera()); // -8 * 72
}

public float getTopFlip() {
return topFlip;
}

public void setTopFlip(float topFlip) {
this.topFlip = topFlip;
invalidate();
}

public float getBottomFlip() {
return bottomFlip;
}

public void setBottomFlip(float bottomFlip) {
this.bottomFlip = bottomFlip;
invalidate();
}

public float getFlipRotation() {
return flipRotation;
}

public void setFlipRotation(float flipRotation) {
this.flipRotation = flipRotation;
invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

canvas.save();
canvas.translate(IMAGE_OFFSET + IMAGE_WIDTH / 2f, IMAGE_OFFSET + IMAGE_WIDTH / 2f);
canvas.rotate(-flipRotation);
camera.save();
camera.rotateX(topFlip);
camera.applyToCanvas(canvas);
camera.restore();
canvas.clipRect(- IMAGE_WIDTH, - IMAGE_WIDTH, IMAGE_WIDTH, 0);
canvas.rotate(flipRotation);
canvas.translate(- (IMAGE_OFFSET + IMAGE_WIDTH / 2f), - (IMAGE_OFFSET + IMAGE_WIDTH / 2f));
canvas.drawBitmap(bitmap, IMAGE_OFFSET, IMAGE_OFFSET, paint);
canvas.restore();

canvas.save();
canvas.translate(IMAGE_OFFSET + IMAGE_WIDTH / 2f, IMAGE_OFFSET + IMAGE_WIDTH / 2f);
canvas.rotate(-flipRotation);
camera.save();
camera.rotateX(bottomFlip);
camera.applyToCanvas(canvas);
camera.restore();
canvas.clipRect(- IMAGE_WIDTH, 0, IMAGE_WIDTH, IMAGE_WIDTH);
canvas.rotate(flipRotation);
canvas.translate(- (IMAGE_OFFSET + IMAGE_WIDTH / 2f), - (IMAGE_OFFSET + IMAGE_WIDTH / 2f));
canvas.drawBitmap(bitmap, IMAGE_OFFSET, IMAGE_OFFSET, paint);
canvas.restore();
}
}

0 comments on commit 0febc92

Please sign in to comment.