首页 技术 正文
技术 2022年11月18日
0 收藏 652 点赞 4,119 浏览 10558 个字

学习内容:

1.了解Drawable类的作用

2.如何使用Drawable…

3.了解Tween动画…

4.如何创建和使用Tween动画…

1.Drawable类…

  Drawable类是对图像的一种抽象…我们可以通过getDrawable方法将图片绘制在屏幕上…Drawable类下有很多种类型…在这里我只对Bitmap和Bitmapfactory进行简单的介绍…通过一个例子来简单的介绍一下…这个例子的实现功能就是可以把我们手机内部的图像设置为当前壁纸,并且我们还可以对图片来进行预览…

  我们在布局文件内部放入五个Button和一个ImageView。。。。触发按钮会完成一些相应的操作…这里就不进行粘贴了,没什么必要…重点是在MainActivity中需要注意一些细节…

package com.example.maindrawable;import java.io.IOException;import android.os.Bundle;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;public class MainActivity extends Activity implements View.OnClickListener { private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.but_1).setOnClickListener(this);
findViewById(R.id.but_2).setOnClickListener(this);
findViewById(R.id.but_3).setOnClickListener(this);
findViewById(R.id.but_4).setOnClickListener(this);
findViewById(R.id.but_5).setOnClickListener(this);
iv=(ImageView)findViewById(R.id.ImageView_1);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.but_1:
//这里是获取SD卡里的图片信息...然后对其中的图片进行相应的操作...
iv.setBackgroundDrawable(Drawable.createFromPath("/mnt/sdcard/LOST.DIR/coffee.jpg"));
break;
case R.id.but_2:
iv.setBackgroundDrawable(Drawable.createFromPath("/mnt/sdcard/LOST.DIR/java.jpg"));
break;
case R.id.but_3:
try {
//设置coffee.jpg为桌面壁纸...不过这里会报异常,因此我们需要进行捕获..
setWallpaper(BitmapFactory.decodeFile("/mnt/sdcard/LOST.DIR/coffee.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.but_4:
try {
setWallpaper(BitmapFactory.decodeFile("/mnt/sdcard/LOST.DIR/java.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.but_5:
try {
//恢复为默认壁纸...
clearWallpaper();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}}

不过上面方法的使用时需要获取android.permission.SET_WALLPAPER权限的…设置壁纸的权限…因此我们需要在Androidmanifest.xml文件中进行相应的配置…

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.maindrawable"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" > </uses-sdk>
<uses-permission
android:name="android.permission.SET_WALLPAPER"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.maindrawable.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

我也就是简单的学习了一下Drawable类,在这里简单的介绍一下…

2.Tween动画…

  说到动画,想必我们都不陌生,Android中有两种动画,一种就是Tween动画,Tween动画被称之为补间动画,也可以被叫做渐变动画,Tween的特点就是我们只需要定义一个动画的开头和结尾的效果,其中的过程我们使用程序来完成…因此被称为补间动画,就是中间过程我们没有必要进行考虑…交给程序来完成,相比于帧动画来说,这种动画引用的资源更少,而且使用起来也更加的方便…

  Frame(帧动画)就是我们把很多张图片都覆盖在同一个屏幕上,然后对这些图片进行一帧一帧的播放,形成一种动态的效果…给人的感觉就像是一个动画在进行播放…由于人眼能分辨的帧数为24帧…因此只要帧数更大,那么也就形成了一种动态效果…因此这就是帧动画的本质,使用更高的帧速来完成图片的播放…从而形成一种动态的效果…这种动画引入的资源更多…因为是多张图片进行播放,因此需要引入更多的图片….学过Flash的会很容易理解这东西的本质….

  Tween动画的创建与使用…

Tween动画的创建需要使用到Animation类,这个类可以使控件完成一些效果,比如说旋转,移动,透明度,缩放等效果…创建Tween的三个步骤…

Animation scaleAnimation=new ScaleAnimation(0.1f,1.0f,0.1f,1.0f);//初始化操作...
scaleAnimation.setDuration(3000);//设置动画的执行时间...
iv.startAnimation(scaleAnimation);//开始动画...

一个简单动画的建立…实现一个图片的缩放效果…布局文件里就一个ImageView控件…没什么其他东西…

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <ImageView
android:id="@+id/imageview_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/psb"/></LinearLayout>

然后就是在MainActivity里来完成初始化,设置动画的持续时间,以及启动动画的一些相应操作….

package com.example.exam7_2;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
public class MainActivity extends Activity implements View.OnClickListener { ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView) findViewById(R.id.imageview_1);
findViewById(R.id.imageview_1).setOnClickListener(this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Animation scaleAnimation=new ScaleAnimation(0.1f,1.0f,0.1f,1.0f);
scaleAnimation.setDuration(3000);
iv.startAnimation(scaleAnimation);
}}

  这样就创建了一个简单的图片缩放动画…我们还可以创建更加复杂的动画,就是使用一个AnimationSet类来定义一个动画集合,然后把我们想要定义的动画全部都放入到这个集合当中去..这样就会使得一个控件完成多种动画的实现效果…这里就不完全进行粘贴了,只需要把onClick里面的代码替换成下面的代码就可以了…

ScaleAnimation(fromX,toX,fromY,toY)动画缩放效果… TranslateAnimation(fromX,toX,fromY,toY)平移效果…RotateAnimation(fromDegrees, toDegrees)旋转效果…AlphaAnimation(fromAlpha, toAlpha)透明度效果…

AnimationSet set=new AnimationSet(true);
Animation tran=new TranslateAnimation(0.1f, 1.0f, 0.1f, 1.0f);
Animation scale=new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f);
Animation rotate=new RotateAnimation(0f, 360f);
Animation alpha=new AlphaAnimation(0.1f, 1.0f);
set.addAnimation(tran);
set.addAnimation(scale);
set.addAnimation(rotate);
set.addAnimation(alpha);
set.setDuration(3000);
iv.startAnimation(set);

这里将四种动画的方式放入到一个集合中,使一个控件去显示这四种效果,而不是四个控件分别显示一种效果…这里设置的相关参数我会在下面进行讲解…这里只是在主函数内部进行配置相应的效果…其实还有另外一种方式来显示动画的效果…就是配置一个xml文件,在xml文件内部来配置相应的效果展示,然后在主函数中进行调用…这种方式是更加合理的,也推荐使用这种方式…可以实现动画效果的复用性,也方便管理…

xml文件里的配置元素…

可配置元素 描述
<set> 根节点,定义全部的动画元素
<alpha> 定义渐变动画效果
<scale> 定义缩放动画效果
<rotate> 定义旋转动画效果
<translate> 定义平移动画效果

有了配置元素,那么必然少不了配置属性….介绍一下公共的配置属性…

可配置属性 数据类型 描述
android:duration long 定义动画的持续时间,以毫秒为单位
android:fillAfter boolean 设置为true表示该动画转化在动画结束之后被应用
android:fillBefore boolean 当设置为true时,该动画的转化在动画开始之前被应用
android:interpolator String 动画插入器(内部有一些属性来完成一些效果)
android:repeatCount int 动画重复的次数
android:repeatMode String 动画重复的模式
android:startOffset long 动画之间的间隔
android:zAdjustment int 动画在zOrder下配置
android:interpolator String 指定动画的执行速率

       简单的介绍一下动画插入器的一些属性,这些属性可以完成一些特效的生成…Interpolator对象的配置属性…

可配置属性 描述
@android:anim/accelerate_decelerate_interpolator 先加速再减速
@android:anim/accelerate_interpolator 加速
@android:anim/anticipate_interpolator 先回退一小步然后加速前进
@android:anim/anticipate_overshoot_interpolator 在上一基础上超过终点一点再到终点
@android:anim/bounce_interpolator 最后阶段弹球效果
@android:anim/cycle_interpolator 周期运动
@android:anim/decelerate_interpolator 减速
@android:anim/linear_interpolator 匀速
@android:anim/overshoot_interpolator 快速到达终点并超过终点一点最后到终点

       这就是一些相应的属性,我也并没有完全的都试一遍,有兴趣的可以试一试…来一个小例子…

<!--这个文件需要定义在res/anim文件夹下面,我们需要新建一个anim文件夹,这个文件夹本身是并没有的,然后在这个文件夹下建立一个这样的xml配置文件...-->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="0.1"   //这个就是scaleAnimation的第一个参数fromX..组件从X轴缩小到0.1倍开始放大...
android:toXScale="1.0"   //这是toX,放大为图片正常大小..
android:fromYScale="0.1"   //这个就fromY,从Y轴缩小一倍开始放大..
android:toYScale="1.0"    //放大为原本大小...
android:duration="3000"/>  //动画的持续时间为3秒...
</set>

然后我们需要在MainActivity中进行调用…调用anim下配置文件的动画效果….

package com.example.exam7_2;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
public class MainActivity extends Activity implements View.OnClickListener { ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView) findViewById(R.id.imageview_1);
findViewById(R.id.imageview_1).setOnClickListener(this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Animation anim=AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);//表示当前主程序调用anim下的动画配置文件...
iv.startAnimation(anim);
}}

这样就算是彻底的完成了一个补间动画的建立和应用…

  帧动画的创建与应用….

  这动画的创建上和Tween有一些不同,不过本质上的东西是一样的,仍然是在xml文件里进行文件配置,然后在主函数中进行调用…帧动画使用的是start()和stop()方法来完成动画的播放与停止,而不是采用载入的形式,设置执行时间…它有特定的函数使动画被开始和终止…

  既然是帧动画,那么必然要引入多张图片…需要在res/anim文件夹下配置一个xml文件…例如frame.xml…我就放入了一张…这里的配置文件的根节点就变成了<animation-list>子元素就是<item>,这点在配置文件上还是和Tween动画有很大的不同的…

<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item
android:drawable="@drawable/a.jpg"
android:duration="200"/>
</animation-list>

  布局文件内部定义两个按钮,一个ImageView就行了…

在MainActivity中我们需要修改一些东西…帧动画是通过获取配置文件,然后通过start()方法启动就可以了…

package com.example.exam7_2;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.graphics.drawable.AnimationDrawable;
import android.view.animation.Animation;
import android.widget.ImageView;
public class MainActivity extends Activity implements View.OnClickListener { private ImageView iv;
private AnimationDrawable draw=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView) findViewById(R.id.imageview_1);
findViewById(R.id.imageview_1).setOnClickListener(this);
findViewById(R.id.start).setOnClickListener(this);
findViewById(R.id.stop).setOnClickListener(this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.start:
  this.iv.setBackgroundResource(R.anim.frame);//设置动画的资源位置...
  this.draw=(AnimationDrawable)iv.getBackground();//取得背景的Drawable对象...
  this.draw.setOneShot(false);//设置执行次数
  this.draw.start();//开始动画...
case R.id.stop:
  this.draw.stop();//停止动画...
}
}}

  这就是帧动画的创建于应用,给我的感觉能使用Tween动画就尽量少使用Frame动画,相比来说Tween动画更加的方便,实用…而且比较简单…

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918