android图形图像


Tween动画和Frame动画


1、Android平台提供了两类动画,分别是Tween动画和Frame动画。


Tween通过场景里的对象不断的进行图片的变换,比如平移、渐变、缩放、
旋转等来产生动画效果;
Frame动画叫做顺序播放实现做好的图像和电影类似。另外加上gif动画,
因为如果直接使用Bitmap或其他方法直接调用gif图片资源的话,显示的
是静态的,如果要做成动态的,就需要一些其他的方法来实现。

Tween动画分类:

Alpha:渐变透明度动画
Scale:渐变尺寸伸缩动画
Translate:画面转换位置移动动画
Rotate:画面转移旋转动画

有两种方式(java,xml):
java:

AlphaAnimation anim = new AlphaAnimation(0,1);
anim.setDuration(3000);
imageView.startAnimation(anim);

xml:
res/anim

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="2000"
    >
</alpha>
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);
imageView.startAnimation(animation);

2)scale
ScaleAnimation(float fromX, float toX, float fromY,
float toY, int pivotXType, floatXValue,
int pivotYType, float pivotYValue)
    功能:创建一个渐变尺寸伸缩动画
    参数:fromX,toX分别是起始和结束时x坐标上的伸缩尺寸。
    fromY,toY分别是起始和结束时ye坐标上的伸缩尺寸。
    pivotXValue,pivotYValue分别为伸缩动画相对于x,y坐标开始的位置,
    pivotXType,pivotYType分别为x,y的伸缩模式。
2.1)、直接在程序中实现的方式java代码:

Animation scale = new ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);?
    scale.setDuration(5000);?//设置动画持续时间?
    img.startAnimation(scale); //开始动画

ps:

相对于(0,0)位置

Animation anim = new ScaleAnimation(0, 1, 0, 1);

相对与自身中心位置

Animation anim = new ScaleAnimation(1.0f,0.0f,1.0f,0.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

是否保持动画后的状态,true表示保持

anim.setFillAfter(true);

消失在(300,300)的位置

Animation anim = new ScaleAnimation(1.0f,0.0f,1.0f,0.0f,Animation.ABSOLUTE,300,Animation.ABSOLUTE,300);

*****重点理解
00……………………………………………………………………………………………………………………………..

2.2).xml

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromXScale="0.0"
    android:toXScale="1.0"
    android:fromYScale="0.0"
    android:toYScale="1.0"
    android:pivotX="300"
    android:pivotY="300"
    >
</scale>

以指定位置(300,300)为中心点

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromXScale="0.0"
    android:toXScale="1.0"
    android:fromYScale="0.0"
    android:toYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    >
</scale>

通过%来表示相对于自身什么位置

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromXScale="0.0"
    android:toXScale="1.0"
    android:fromYScale="0.0"
    android:toYScale="1.0"
    android:pivotX="50%p"
    android:pivotY="100%p"
    >
    </scale>

通过p表示相对于父控件什么位置

android:interpolator="@android:anim/accelerate_decelerate_interpolator"

指定速度变化过程


3)translate
TranslateAnimation(float fromXDelta, float toXDelta, float YDelta, float toYDelta)
    功能:创建一个移动画面位置的动画
    参数:fromXDelta,fromYDelta分别是其实坐标;toXDelta,toYDelta分别是结束坐标
1、直接在程序中实现java代码:

Animation translate = new TranslateAnimation(10, 100, 10, 100);
translate.setDuration(3000);?//设置动画持续时间?
img.startAnimation(translate); //开始动画?

2、在XML中创建动画
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="10"
android:toXDelta="100"
android:fromYDelta="10"
android:toYDelta="100"
android:duration="5000"/>

    java调用

Animation translate = AnimationUtils.loadAnimation(TweenActivity.this, R.anim.translate_anim);?
    img.startAnimation(translate); //开始动画

4)rotate
Rotate(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType,float pivotYValue)
    功能:创建一个旋转画面的动画
    参数:fromDegrees为开始的角度;toDegrees为结束的角度。pivotXValue、pivotYType分别为x,y的伸缩模式。pivotXValue,pivotYValue分别为伸缩动画相对于x,y的坐标开始位置
1、直接在程序中创建动画java代码:

Animation rotate = new RotateAnimation(0f,+360f,?
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);?
rotate.setDuration(3000);
img.startAnimation(rotate);

5)AnimationSet
是一个动画集合
java:

AnimationSet as = new AnimationSet(true);
as.setDuration(2000);//给每一个Animation指定持续时间
as.setInterpolator(new LinearInterpolator());//指定AnimationSet中所有的Animation都是匀速
Animation rotateAnim = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//rotateAnim.setDuration(2000);
rotateAnim.setStartOffset(1000);//在下边startAnimation之后,过多长时间开始执行动画
AlphaAnimation alphaAnim = new AlphaAnimation(0, 1);
//alphaAnim.setDuration(2000);
as.addAnimation(rotateAnim);
as.addAnimation(alphaAnim);
imageView.startAnimation(as);

xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="true"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:duration="2000"
    >
    <rotate 
        android:fromDegrees="0"
        android:toDegrees="-360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="2000"
        />
    <scale
        android:fromXScale="1"
        android:toXScale="3"
        android:fromYScale="1"
        android:toYScale="3"
        android:pivotX="0"
        android:pivotY="0"
        />
</set>
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.set);
imageView.startAnimation(anim);


ps:1.监听Animation状态的事件AnimationListener

anim.setAnimationListener(new AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
        
    }
    @Override
    public void onAnimationRepeat(Animation animation) {
        
    }
    @Override
    public void onAnimationEnd(Animation animation) {
        
    }
});
ps.2.启动Animation两种方式
imageView.startAnimation(anim);               
               
imageView.setAnimation(anim);
anim.start();
以上两者等价
anim.cancle();

2.Frame动画


2.1 java的实现:


方式一:

private int[] images = {R.drawable.pic1,
    R.drawable.pic2,R.drawable.pic3,
    R.drawable.pic4};
final AnimationDrawable animDrawable = new AnimationDrawable();

for(int i=0;i<4;i++){
    animDrawable.addFrame(getResources().getDrawable(images[i]), 80);
}
image.setBackgroundDrawable(animDrawable);
animDrawable.setOneShot(false);
btn1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        animDrawable.start();
//                animDrawable.stop();
    }
});

方式二:
要求是文件命名要遵循一定规律,方便递归查找。
for(int i=0;i<4;i++){
    //参数1,文件名
    //参数2,资源类型
    //参数3,是那个package的。
    int resId = getResources().getIdentifier("pic"+(i+1), "drawable", "com.anjoyo.animation2");
    animDrawable.addFrame(getResources().getDrawable(resId), 500);
}
image.setBackgroundDrawable(animDrawable);
animDrawable.start();

2.2xml的实现:
定义:

<?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/pic1" android:duration="500"/>
    <item android:drawable="@drawable/pic2" android:duration="500"/>
    <item android:drawable="@drawable/pic3" android:duration="500"/>
    <item android:drawable="@drawable/pic4" android:duration="500"/>
</animation-list>
使用1:
AnimationDrawable d = (AnimationDrawable) getResources().getDrawable(R.anim.frame);
image.setBackgroundDrawable(d);
d.start();
使用2,在xml中使用:
<ImageView
    android:id="@+id/image"
    android:layout_width="300dp"
    android:layout_height="400dp"
    android:background="@anim/frame"
    />

AnimationDrawable d = (AnimationDrawable) image.getBackground();
d.start();

posted on 2013-01-03 15:23  @与非  阅读(2116)  评论(0编辑  收藏  举报