Android开发之50个常见实用技巧——添加悦目的动画效果
Hack.5 使用TextSwitcher和ImageSwitcher实现平滑过渡
实现步骤:
1、通过findViewById()方法获取TextSwitcher对象的引用Swithcer,当然也可直接在代码中构造持续对象;
2、通过Switcher.setFactory()方法指定TextSwitcher的ViewFactory;
3、通过Switcher的.setInAnimation()方法设置换人动画效果;
4、通过Switcher.setOutAnimation 方法设置换出动画效果。
代码如下:
private TextSwitcher mTextSwitcher; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
mTextSwitcher = (TextSwitcher) findViewById(R.id.your_textview);
mTextSwitcher.setFactory(new ViewFactory() {
@Override
public View makeView() {
TextView t = new TextView(YourActivity.this);
t.setGravity(Gravity.CENTER); return t;
}
});
mTextSwitcher.setInAnimation(in);
21 mTextSwitcher.setOutAnimation(out);
}
Hack.6 为ViewGroup的子视图添加悦目的动画效果
步骤:
1、获取ListView的引用;
2、创建默认动画集合对象,AnimationSet;
3、创建透明度渐变动画,移位动画,分别为AlphaAnimation,TranslateAnimation;
4、创建LayoutAnimationController对象并设置子视图,动画效果持续时间;
5、将LayoutAnimationController对象设置到ListView中;
关键代码如下:
mListView = (ListView) findViewById(R.id.my_listview_id); AnimationSet set = new AnimationSet(true); Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(50);
set.addAnimation(animation); animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
-1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
animation.setDuration(100);
set.addAnimation(animation); LayoutAnimationController controller =
new LayoutAnimationController(set, 0.5f); mListView.setLayoutAnimation(controller);
Hcak.7 在Canvas上显示动画
关于Canvas:
可以把 Canvas 视为 Surface 的替身或者接口,图形便是绘制在 Surface 上的。Canvas 封装了所有绘图调用。通过 Canvas,绘制
到 Surface 上的内容首先存储到与之关联的 Bitmap 中,该 Bitmap最终会呈现到窗口上。
在onDraw()方法中调用。。。
关键代码如下:
Activity代码:
public class MainActivity extends Activity {
private DrawView mDrawView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Display display = getWindowManager().getDefaultDispla y();
mDrawView = new DrawView(this);
mDrawView.height = display.getHeight();
mDrawView.width = display.getWidth();
setContentView(mDrawView);
}
}
DrawView类:
public class DrawView extends View {
private Rectangle mRectangle;
public int width;
public int height;
public DrawView(Context context) {
super(context);
mRectangle = new Rectangle(context, this);
mRectangle.setARGB(255, 255, 0, 0);
mRectangle.setSpeedX(3);
mRectangle.setSpeedY(3);
}
@Override
protected void onDraw(Canvas canvas) {
mRectangle.move();
mRectangle.onDraw(canvas);
invalidate();
}
}
分析:
1、创建一个Rectangle实例,代表一个方块;Rectangle类内部实现了将自身绘制到Canvas上的逻辑,
并且已经包含了正确变换其位置的代码逻辑。
2、当调用onDraw()方法时,通过move()方法,方块的位置就会改变
3、通过onDraw()方法绘制到Canvas上;
4、在invalidate()中,强制重绘视图。
其实,就是通过循环调用Rectangle的move()和onDraw()方法实现一个动画效果。
Hack.8 附加Ken Burns 特效的幻灯片
要创建Ken Burns特效,需要预设一些动画
案例分析:
将动画随机应用到ImageView,当一个动画显示完毕,即开始显示另一个动画和图片;
主布局使用FrameLayout,把ImageView置于该布局;
代码如下:
略。。。
旧动画API的特点:
1、只支持视图对象的动画效果
2、仅限于移动、旋转、缩放、渐变等效果
3、只改变视图移动时的视觉效果,并未改变其真实位置属性
最新文章
- jquery中select的应用
- poj 2528 Mayor's posters(线段树+离散化)
- ACM题目————字串数
- linux shell-syntax error near unexpected token错误
- CSS3伪类实现动画旋转效果
- JavaScript 框架------------AngularJS(上)
- 24点小游戏app宣传文案
- jquery ajax 中实现给变量赋值
- BZOJ 1059 [ZJOI2007]矩阵游戏 (二分图最大匹配)
- Mybatis类型转换介绍
- html的css选择器
- Java 异步处理 三种实现
- sort+函数指针、sort+比较器对象、qsort速度比较
- Bzoj1492: [NOI2007]货币兑换Cash(不单调的斜率优化)
- VMware快照
- ZOJ 3332 Strange Country II (竞赛图构造哈密顿通路)
- django终端打印Sql语句
- idea远程debug:tomcat
- 把文件类型转化为byte[]
- Ubuntu的防火墙配置-ufw-iptables
热门文章
- jsonp多次请求报错 not a function的解决方法
- art.dialog 与 ajax 异步请求
- 安装mod_deflate模块启用apache的GZIP压缩
- Instructions Set JAVA_HOME System-Wide
- osg学习笔记2, 命令行参数解析器ArgumentParser
- 转载:Source Insight查看ARM汇编源程序 &;&; 高亮显示程序 &;&; Source Insight打开project窗口出错
- Manacher 算法-----o(n)回文串算法
- 欧几里得旅行商问题 java与c++实现
- Uva10207 The Unreal Tournament
- hdu 4111 Alice and Bob