第一步 :布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="动画 测试"
/>
<ImageView
android:id="@+id/myimageview"
android:src="@drawable/me"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="60dp"
/>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onMyClick"
android:text="平移动画效果"
/>
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onMyClick"
android:text="渐变动画效果"
/>
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onMyClick"
android:text="旋转动画效果"
/>
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onMyClick"
android:text="缩放动画效果"
/>
</LinearLayout>
第二步:在res下创建文件夹res及xml文件myanim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 渐变效果 -->
<alpha
android:fromAlpha="1"
android:toAlpha="0"
android:duration="3000"
></alpha>
<!--旋转效果-->
<rotate
android:fromDegrees="0"
android:toDegrees="-45"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="400" />
<!-- 缩放效果 -->
<scale
android:fromXScale="1.0"
android:toXScale="3.0"
android:fromYScale="1.0"
android:toYScale="3.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000" />
<!-- 平移效果 -->
<translate
android:fromXDelta="0"
android:toXDelta="20"
android:fromYDelta="0"
android:toYDelta="20" />
</set>
第三步:java文件
public class Day29AnimationsTest1Activity extends Activity {
private ImageView imageview=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageview=(ImageView)findViewById(R.id.myimageview);
//缩放图片
Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.me);
int width=bitmap.getWidth();
int height=bitmap.getHeight();
Matrix matrix=new Matrix();
matrix.setScale(1*0.3f, 1*0.3f);
Bitmap bm=Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
imageview.setImageBitmap(bm);
}
//绑定监听
public void onMyClick(View v){
if(v.getId()==R.id.button1){
//控制缩放动画效果
Animation animation=AnimationUtils.loadAnimation(this, R.anim.myanim);
imageview.setAnimation(animation);
animation.start();
}else if(v.getId()==R.id.button2){
//控制渐变动画效果
Animation animation=AnimationUtils.loadAnimation(this, R.anim.myanim);
imageview.setAnimation(animation);
animation.start();
}else if(v.getId()==R.id.button3){
//控制缩放动画效果
Animation animation=AnimationUtils.loadAnimation(this, R.anim.myanim);
imageview.setAnimation(animation);
animation.start();
}else if(v.getId()==R.id.button4){
//控制缩放动画效果
Animation animation=AnimationUtils.loadAnimation(this, R.anim.myanim);
imageview.setAnimation(animation);
animation.start();
}
}
}
第四步:演示