Android常见问题总结(四)

上一篇博客传送门:Android常见问题总结(三)


16. 动画有哪几类,各有什么特点

如今Android的动画主要有三种,分别是:逐帧(Frame)动画,补间(Tween)动画,属性(Property)动画

逐帧(Frame)动画

逐帧动画是最容易理解的动画,它要求我们把动画过程的每张静态图片都准备好,然后依次显示,利用人眼“视觉暂留”的原理形成动画效果。

例子:肥波跳舞?

素材准备(共27帧):


fat_po.xml:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot = "false" >

    <item android:drawable="@drawable/fat_po_f01" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f02" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f03" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f04" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f05" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f06" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f07" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f08" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f09" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f10" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f11" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f12" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f13" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f14" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f15" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f16" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f17" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f18" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f19" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f20" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f21" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f22" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f23" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f24" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f25" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f26" android:duration="60" />
    <item android:drawable="@drawable/fat_po_f27" android:duration="60" />
    
</animation-list>

animation-list的oneshot属性用于设置动画是否只播放一次,true是,false表示循环播放。

Activity布局xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <ImageView 
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@animator/fat_po"/>

</LinearLayout>

把逐帧(Frame)动画设置为ImageView的背景。

Activity的Java代码:
public class MyActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ImageView imageView = (ImageView)findViewById(R.id.image_view);
		// 开始动画,默认为停止
		((AnimationDrawable)imageView.getBackground()).start();
	}
	
}

由于逐帧动画默认是停止,因此我们需要调用其start方法才能播放动画。

效果如下:
逐帧动画效果图


补间(Tween)动画

相比与逐帧动画要求我们把动画的每一帧都列出来,补间动画只需要我们指定动画开始、动画结束等“关键帧”,而其中动画变化的“中间帧”由系统计算补齐。

例子:花瓣开合

动画材料:


花瓣动画xml文件:

close.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    
    <scale android:fromXScale="1.0"
        android:toXScale="0.01"
        android:fromYScale="1.0"
        android:toYScale="0.01"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="true"
        android:duration="3000"/>
    
    <alpha 
        android:fromAlpha="1"
        android:toAlpha="0.05"
        android:duration="3000"/>

    <rotate 
        android:fromDegrees="0"
        android:toDegrees="1800"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="3000"/>
</set>

花瓣关闭xml文件,使用了scale(大小变化),alpha(透明度变化),rotate(旋转变化)三种动画,执行的速度是linear线性的

open.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    
    <scale android:fromXScale="0.01"
        android:toXScale="1.0"
        android:fromYScale="0.01"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="true"
        android:duration="3000"/>
    
    <alpha 
        android:fromAlpha="0.05"
        android:toAlpha="1"
        android:duration="3000"/>

    <rotate 
        android:fromDegrees="1800"
        android:toDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="3000"/>
</set>

与close动画类似。

界面的xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <ImageView 
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/flower"/>

</LinearLayout>

只有一个简单的imageView。

Activity的Java代码:
public class MyActivity extends Activity {
	
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (flag)
				imageView.startAnimation(close);
			else
				imageView.startAnimation(open);
			flag = !flag;
		};
	};
	
	private Animation open, close;
	private ImageView imageView;
	boolean flag = true; // 花瓣状态,true开,false合
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		imageView = (ImageView)findViewById(R.id.image_view);
		// 花瓣张开
		open = AnimationUtils.loadAnimation(this, R.anim.open);
		// 保留动画变化后的状态
		open.setFillAfter(true);
		// 花瓣关闭
		close = AnimationUtils.loadAnimation(this, R.anim.close);
		// 保留动画变化后的状态
		close.setFillAfter(true);
		// 设置重复任务
		new Timer().schedule(new TimerTask() {
			
			@Override
			public void run() {
				// 通过handler发送消息刷新UI
				handler.sendEmptyMessage(0);
			}
		}, 0, 3500);
	}
	
}

加载了close与open动画后,通过Timer的重复任务不断发送消息给handler,通过handler来播放动画。

效果如下:
补间动画flower

对于补间(Tween)动画我们只需要准备动画开始与结束的关键帧即可,其中的“中间帧”使用Android提供的多种变换(Scale,Alpha,Rotate,translate)生成即可。

属性(Property)动画

从某种角度来看,属性动画是增强版的补间动画,属性动画的强大主要体现在两个方面:
  1. 补间动画只能定义两个关键帧在“透明度”、“旋转”、“缩放”、“位移”4个方面的变化,但属性动画可以定义任何属性的变化
  2. 补间动画只能对UI组件执行动画,但属性动画几乎可以对任何对象执行动画
由于属性动画比较复杂,本文在此不展开讨论了,推荐两篇大神博客供大家参考:

17. Handler、Looper消息队列模型,各部分的作用。

简单提提Handler、Looper模型中各部分的作用,主要有以下三部分:
  • MessageQueue:消息队列,存储待处理的消息
  • Looper:封装了消息队列与Handler,线程绑定,使用loop方法循环处理消息
  • Handler:消息处理的辅助类,里面封装了消息的投递、处理和获取等一系列操作
详细的情况请参考看我以前的博文:

18. 怎样退出终止App

测试:
依次打开3个Activity(ActivityA,ActivityB,ActivityC),并在第3个Activity中终止App

System.exit(0)

重写Application方法:
public class MyApplication extends Application {

	public void exit() {
		System.exit(0);
	}
	
}

切记在AndroidManifest中修改使用的Application

在第三个Activity中调用该方法:
public class ActivityC extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity);
		Button button = (Button)findViewById(R.id.btn);
		button.setText("Finish app");
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 退出App
				((MyApplication)getApplication()).exit();
			}
		});
	}
}

失败,应用进程被杀死,然而过会应用重启了……并且ActivityA和ActivityB都被“复活了”,只杀死了ActivityC。

android.os.Process.killProcess(android.os.Process.myPid())

失败,与System.exit(0)一样,虽然杀死了进程,但过会就被重启了,并且ActivityA和ActivityB都被“复活了”。

ActivityManager am= (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
am.killBackgroundProcesses(this.getPackageName());

失败,毫无反应。

自定义BaseActivity维护Activity列表

自定义一个BaseActivity:
public class BaseActivity extends Activity {

	// 维护一个Activity软引用的列表
	private static List<SoftReference<Activity>> list = new ArrayList<SoftReference<Activity>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		list.add(new SoftReference<Activity>(this));
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		list.remove(new SoftReference<Activity>(this));
	}

	/**
	 * 关闭所有的Activity
	 */
	public void finishAll() {
		for (SoftReference<Activity> sr : list) {
			if (sr.get() != null) {
				sr.get().finish();
			}
		}
	}

}

对于ActivityA、ActivityB和ActivityC继承BaseActivity而不是Activity,在ActivityC中调用finishAll方法即可关闭所有Activity进而退出App。

finishAffinity()

直接关闭相同任务栈中的所用Activity,与上一个方法效果差不多,但是是Android自带的,方便多了。

综上所述,测试结果如下:
  1. System.exit(0):只能关闭当前Activity,关闭进程可能导致数据存储问题,不推荐
  2. android.os.Process.killProcess(android.os.Process.myPid()):同上

  3. ActivityManager am= (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
    am.killBackgroundProcesses(this.getPackageName()):测试无效

  4. 自定义BaseActivity维护Activity列表:可以关闭依次启动的所用Activity,进而退出整个App

  5. finishAffinity():可以关闭同一个任务栈中的所有Activity,Android自带方法,比较方便


19. Asset目录与res目录的区别

Asset目录和res目录均为Android中用来存放资源的目录,其中:
  • asset目录下存放的资源代表应用无法直接访问的原生资源,应用程序需要通过AssetManager以二进制流的形式来读取资源
  • res目录下的资源可通过R资源清单类访问,Android SDK会在编译时在R类中为他们创建对应的索引项

20. Android怎么加速启动Activity

个人认为,影响Activity启动时间的主要有两个地方:
  1. onCreate、onStart、onResume等回调方法的执行时间
  2. Activity对应的界面的inflate时间
对于第一点,我们应该尽量减少在这些回调方法中执行耗时操作(涉及数据库,图片等),如果一定要执行耗时操作,可以考虑新开子线程处理。
对于第二点,我们应该合理使用各种xml的优化标签,并界面上减少View的嵌套层数与绘制时间。(可参考 Android常见问题总结(三)中的问题14)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android OTA(Over-The-Air)升级是指通过网络传输直接在设备上进行系统升级的一种方法。下面简要介绍Android OTA升级的流程以及常见问题分析。 Android OTA升级流程通常包括以下几个步骤: 1. 系统准备:开发者根据新版本的要求,为设备准备升级所需的系统文件。这些文件包括操作系统文件、应用程序文件以及升级脚本等。 2. 设备请求:设备通过连接到网络,并向服务器请求最新的系统升级。服务器根据设备的型号和当前系统版本,判断是否需要进行升级。 3. 传输升级文件:如果服务器确认设备需要进行升级,那么就会将升级文件传输给设备。这通常是通过HTTP或FTP等协议进行数据传输。 4. 升级验证:设备接收到升级文件后,会进行文件校验,以确保文件的完整性和一致性。 5. 升级安装:设备在校验通过后,会进行系统升级安装。这通常会涉及到文件解压、系统分区扩展等操作。 6. 安装后处理:升级完成后,设备会重新启动并进行一些后期处理工作,例如数据迁移、应用优化等。 常见问题分析: 1. 升级失败:升级过程中可能出现各种原因导致升级失败,如网络中断、升级文件损坏、设备存储空间不足等。解决方法可以包括重新尝试升级、检查网络连接、清理设备存储空间等。 2. 兼容性问题:新版本的系统可能对设备硬件或软件要求更高,而一些老旧设备可能无法满足这些要求。这可能导致升级后设备性能下降或功能不可用。解决方法可以是提供适配的系统版本或更新设备硬件。 3. 数据丢失:升级过程中可能导致设备的数据丢失,包括联系人、短信、应用程序数据等。为了避免这种问题,可以提醒用户备份数据或提供数据迁移工具。 4. 升级时间过长:升级过程可能需要较长时间,尤其是在升级文件较大或设备性能较低的情况下。解决方法可以是优化升级文件的大小和传输速度,或提供快速升级选项。 总结Android OTA升级通过网络直接在设备上进行系统升级,流程包括准备、请求、传输、验证、安装和处理。常见问题包括升级失败、兼容性问题、数据丢失和升级时间过长等,需要通过合适的解决方法来处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值