android动画XML实现

第一步 :布局文件

<?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();
      
     }
    }
}

第四步:演示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值