最近的几篇博文反响还不错,也会继续的写下去的,关于这些特效的专辑,大多数也是借鉴大神的,最近由于工作的关系,会深入的了解一下Android BLE与硬件设备的串口通讯相关的内容,也会时不时的分享出来,当然,大家是一起学习,我毕竟也是初学者,今天讲的是小火箭的动画效果,用到了基础动画做了一些偷梁换柱的事情,明天还是后天,再更新一个心型起泡飞舞的特效,就不会这么持续的更新特效专辑这一系列了,毕竟《Only》这个软件也正在开发当中,最重要的还是交互,不是特效,特效多了反而会鸡肋,并且机型适配方面可能导致一些不确定的因素也是有的,所以,这几天可能会多分享一些蓝牙相关的东西了,当然,会持续的更新Android实用案例这个系列,很多的Android技术等你一起来玩。
我们先来看看今天的效果图吧:
感觉是不是挺好玩的?其实,我其实就是用了三张图片,用黑色背景我们看看这三张图片
好的,知道了这三张图片,我们在res文件下新建一个anim文件夹,然后把他们三个的动画加上
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha android:duration="800" android:fromAlpha="0" android:startOffset="300" android:toAlpha="1.0" >
</alpha>
<translate android:duration="800" android:fromYDelta="100%" android:interpolator="@android:anim/accelerate_interpolator" android:startOffset="300" android:toYDelta="0%" />
<alpha android:duration="500" android:fromAlpha="1.0" android:startOffset="2050" android:toAlpha="0" >
</alpha>
<translate android:duration="650" android:fromYDelta="0%" android:startOffset="2050" android:toYDelta="100%" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate android:duration="200" android:fromYDelta="100%" android:startOffset="2050" android:toYDelta="0%" />
<alpha android:duration="450" android:fromAlpha="1.0" android:startOffset="2250" android:toAlpha="0" >
</alpha>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate android:duration="500" android:fromYDelta="100%" android:toYDelta="-5%p" />
<translate android:duration="500" android:fromYDelta="0%p" android:startOffset="500" android:toYDelta="4%p" />
<translate android:duration="500" android:fromYDelta="0%p" android:startOffset="1000" android:toYDelta="-1.5%p" />
<translate android:duration="300" android:fromYDelta="0%p" android:startOffset="1750" android:toYDelta="8%p" />
<translate android:duration="400" android:fromYDelta="0%p" android:interpolator="@android:anim/accelerate_interpolator" android:startOffset="2050" android:toYDelta="-108%p" />
</set>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" >
<ImageView android:id="@+id/cloud" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/cloud" android:visibility="invisible" />
<ImageView android:id="@+id/launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:src="@drawable/launcher" android:visibility="invisible" />
<ImageView android:id="@+id/rocket" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:paddingLeft="8dp" android:src="@drawable/rocket" android:visibility="invisible" />
<Button android:id="@+id/getup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="启动火箭" android:textColor="#ffffff" android:textSize="20dp" />
</RelativeLayout>
三个动画,然后就是布局,一个按钮启动,然后三张图片显示出来并且出来一个动画,是不是思路越来越清晰了?
package com.lgl.recht;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Handler mHandler = new Handler();
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.getup);
btn.setOnClickListener(this);
}
private void launcherTheRocket() {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
final View rocket = findViewById(R.id.rocket);
//初始化
Animation rocketAnimation = AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.rocket);
//设置动画监听
rocketAnimation
.setAnimationListener(new VisibilityAnimationListener(
rocket));
//开启
rocket.startAnimation(rocketAnimation);
final View cloud = findViewById(R.id.cloud);
Animation cloudAnimation = AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.cloud);
cloudAnimation
.setAnimationListener(new VisibilityAnimationListener(
cloud));
cloud.startAnimation(cloudAnimation);
final View launcher = findViewById(R.id.launcher);
Animation launcherAnimation = AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.launcher);
launcherAnimation
.setAnimationListener(new VisibilityAnimationListener(
launcher));
launcher.startAnimation(launcherAnimation);
}
}, 150);
}
public class VisibilityAnimationListener implements AnimationListener {
private View mVisibilityView;
public VisibilityAnimationListener(View view) {
mVisibilityView = view;
}
public void setVisibilityView(View view) {
mVisibilityView = view;
}
//动画开始
@Override
public void onAnimationStart(Animation animation) {
Log.i("START", "...");
if (mVisibilityView != null) {
mVisibilityView.setVisibility(View.VISIBLE);
// mVisibilityView.setVisibility(View.GONE);
}
}
//动画结束
@Override
public void onAnimationEnd(Animation animation) {
Log.i("END", "...");
if (mVisibilityView != null) {
mVisibilityView.setVisibility(View.GONE);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
@Override
public void onClick(View v) {
//启动
launcherTheRocket();
}
}
好的,我们可以来运行一下了
这里我们并没有添加清理缓存的功能,我们在下面实现,这里提供一个工具类
package com.lgl.data;
import java.io.File;
import java.math.BigDecimal;
import android.content.Context;
import android.os.Environment;
/** * 本应用数据清除管理器 */
public class DataCleanManager {
public static String getTotalCacheSize(Context context) throws Exception {
long cacheSize = getFolderSize(context.getCacheDir());
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
cacheSize += getFolderSize(context.getExternalCacheDir());
}
return getFormatSize(cacheSize);
}
public static void clearAllCache(Context context) {
deleteDir(context.getCacheDir());
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
deleteDir(context.getExternalCacheDir());
}
}
private static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
// 获取文件
// Context.getExternalFilesDir() --> SDCard/Android/data/你的应用的包名/files/
// 目录,一般放一些长时间保存的数据
// Context.getExternalCacheDir() -->
// SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据
public static long getFolderSize(File file) throws Exception {
long size = 0;
try {
File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {
// 如果下面还有文件
if (fileList[i].isDirectory()) {
size = size + getFolderSize(fileList[i]);
} else {
size = size + fileList[i].length();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return size;
}
/** * 格式化单位 * * @param size */
public static String getFormatSize(double size) {
double kiloByte = size / 1024;
if (kiloByte < 1) {
return size + "Byte";
}
double megaByte = kiloByte / 1024;
if (megaByte < 1) {
BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
return result1.setScale(2, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "KB";
}
double gigaByte = megaByte / 1024;
if (gigaByte < 1) {
BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
return result2.setScale(2, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "MB";
}
double teraBytes = gigaByte / 1024;
if (teraBytes < 1) {
BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
return result3.setScale(2, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "GB";
}
BigDecimal result4 = new BigDecimal(teraBytes);
return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
+ "TB";
}
}
private DataCleanManager dm;
try {
//缓存大小
tv.setText(dm.getTotalCacheSize(this));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//清理缓存
dm.clearAllCache(MainActivity.this);