鱼C论坛

 找回密码
 立即注册
查看: 2681|回复: 15

[原创]android学习之动画效果

[复制链接]
发表于 2014-11-24 22:48:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.布局文件:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.            android:orientation="vertical"
  6.     tools:context=".MainActivity" >

  7.     <LinearLayout
  8.         android:layout_width="match_parent"
  9.         android:layout_height="wrap_content"
  10.         android:orientation="horizontal"
  11.         >
  12.         <Button
  13.             android:layout_width="wrap_content"
  14.             android:layout_height="wrap_content"
  15.             android:text="透明度"
  16.             android:onClick="alpha"
  17.             />
  18.          <Button
  19.             android:layout_width="wrap_content"
  20.             android:layout_height="wrap_content"
  21.             android:text="缩放"
  22.             android:onClick="scale"
  23.             />
  24.           <Button
  25.             android:layout_width="wrap_content"
  26.             android:layout_height="wrap_content"
  27.             android:text="旋转"
  28.             android:onClick="rotate"
  29.             />
  30.            <Button
  31.             android:layout_width="wrap_content"
  32.             android:layout_height="wrap_content"
  33.             android:text="平移"
  34.             android:onClick="translate"
  35.             />
  36.             <Button
  37.             android:layout_width="wrap_content"
  38.             android:layout_height="wrap_content"
  39.             android:text="综合"
  40.             android:onClick="all"
  41.             />
  42.     </LinearLayout>
  43.    
  44.     <LinearLayout
  45.         android:layout_width="match_parent"
  46.         android:layout_height="match_parent"
  47.         android:gravity="center"
  48.         >
  49.         <ImageView
  50.             android:id="@+id/iv"
  51.             android:layout_width="200dp"
  52.             android:layout_height="200dp"
  53.             android:src="@drawable/mm"
  54.             />
  55.     </LinearLayout>
  56. </LinearLayout>
复制代码

2.activity程序的实现:

  1. package com.example.animation_image_test;

  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.view.View;
  6. import android.view.animation.AlphaAnimation;
  7. import android.view.animation.Animation;
  8. import android.view.animation.AnimationSet;
  9. import android.view.animation.RotateAnimation;
  10. import android.view.animation.ScaleAnimation;
  11. import android.view.animation.TranslateAnimation;
  12. import android.widget.ImageView;

  13. public class MainActivity extends Activity {

  14.         private ImageView iv;
  15.         
  16.         @Override
  17.         protected void onCreate(Bundle savedInstanceState) {
  18.                 super.onCreate(savedInstanceState);
  19.                 setContentView(R.layout.activity_main);
  20.                
  21.                 this.iv = (ImageView) this.findViewById(R.id.iv);
  22.                
  23.         }
  24.         
  25.         public void alpha(View v)  //透明度
  26.         {
  27.                 AlphaAnimation an = new AlphaAnimation(0, 1);
  28.                 an.setDuration(3000);
  29.                 iv.startAnimation(an);
  30.         }
  31.         
  32.         public void scale(View v)  //缩放
  33.         {
  34.                 ScaleAnimation an = new ScaleAnimation(0, 2,2,1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  35.                 an.setDuration(3000);
  36.                 iv.startAnimation(an);
  37.         }
  38.         
  39.         public void rotate(View v)  //旋转
  40.         {
  41.                 RotateAnimation an=new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,  Animation.RELATIVE_TO_SELF, 0.5f);
  42.                 an.setDuration(3000);
  43.                 iv.startAnimation(an);
  44.         }
  45.         
  46.         public void translate(View v)  //平移
  47.         {
  48.                 TranslateAnimation an=new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0,  Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.2f);
  49.             an.setDuration(3000);
  50.             iv.startAnimation(an);
  51.         }
  52.         
  53.         public void all(View v)  //综合
  54.         {
  55.                 AlphaAnimation an=new AlphaAnimation(0, 1);
  56.                 an.setDuration(3000);
  57.                 ScaleAnimation sc=new ScaleAnimation(0, 3, 1, 1,  Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  58.                 sc.setDuration(3000);
  59.                 RotateAnimation ra=new RotateAnimation(0, 270, Animation.RELATIVE_TO_SELF, 0.5f,  Animation.RELATIVE_TO_SELF, 0.5f);
  60.                 ra.setDuration(3000);
  61.             TranslateAnimation ta=new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0,  Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.2f);
  62.             ta.setDuration(3000);
  63.            
  64.             AnimationSet animation=new AnimationSet(true);
  65.             //将效果加入到AnimationSet里面
  66.             animation.addAnimation(an);
  67.             animation.addAnimation(sc);
  68.             animation.addAnimation(ra);
  69.             animation.addAnimation(ta);
  70.             iv.startAnimation(animation);  //执行动画效果
  71.         }
  72. }
复制代码

QQ截图20141124224708.png QQ截图20141124224735.png QQ截图20141124224752.png QQ截图20141124224811.png

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
拈花小仙 + 5 + 5 + 3 感谢楼主无私奉献!

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-11-24 23:22:42 | 显示全部楼层
支持我家玄玄~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-11-25 08:39:33 | 显示全部楼层
:lol::lol:
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-11-25 09:57:12 | 显示全部楼层
楼主是自学android么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-11-25 10:36:05 | 显示全部楼层
强烈支持楼主ing....
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-11-25 10:46:57 | 显示全部楼层
玄玄厉害,高!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-11-25 12:05:02 | 显示全部楼层
厉害!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-11-25 12:35:49 | 显示全部楼层
一直想做移动端的开发,虽然大家都选择了安卓,但是我依然追寻WP:loveliness:
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-11-25 13:27:54 | 显示全部楼层
我只是路过打酱油的。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-25 15:25:25 | 显示全部楼层
qingchen 发表于 2014-11-25 09:57
楼主是自学android么

呵呵! java基础与C是自学的,android的话是培训老师教的!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-25 15:25:39 | 显示全部楼层

呵呵!过奖了!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-25 15:29:44 | 显示全部楼层
我疯狂我成功 发表于 2014-11-25 12:35
一直想做移动端的开发,虽然大家都选择了安卓,但是我依然追寻WP

哦! 那好啊!WP也好着呢!现在比较主流的就是iphone,android,wp了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-11-26 08:31:08 | 显示全部楼层
青玄 发表于 2014-11-25 15:25
呵呵! java基础与C是自学的,android的话是培训老师教的!

我也在学android  但是还在学基础
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-11-26 16:14:26 | 显示全部楼层
青玄 发表于 2014-11-25 15:25
呵呵! java基础与C是自学的,android的话是培训老师教的!
  1. <p><div class="blockcode"><blockquote><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:id="@+id/LinearLayout1"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:orientation="vertical"
  7.     android:paddingBottom="@dimen/activity_vertical_margin"
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"
  9.     android:paddingRight="@dimen/activity_horizontal_margin"
  10.     android:paddingTop="@dimen/activity_vertical_margin"
  11.     tools:context=".ImgActivity" >

  12.     <LinearLayout
  13.         android:layout_width="match_parent"
  14.         android:layout_height="wrap_content"
  15.         android:layout_gravity="center_horizontal"
  16.         android:layout_marginBottom="5dp">

  17.         <Button
  18.             android:id="@+id/img_tigaotumingdu"
  19.             android:layout_width="wrap_content"
  20.             android:layout_height="wrap_content"
  21.             android:text="@string/activity_img_tigaotoumingdu"
  22.             android:textSize="16sp" />

  23.         <Button
  24.             android:id="@+id/img_jiangditoumingdu"
  25.             android:layout_width="wrap_content"
  26.             android:layout_height="wrap_content"
  27.             android:text="@string/activity_img_jingditoumingdu"
  28.             android:textSize="16sp" />

  29.         <Button
  30.             android:id="@+id/img_next"
  31.             android:layout_width="wrap_content"
  32.             android:layout_height="wrap_content"
  33.             android:text="@string/activity_img_nextbtn"
  34.             android:textSize="16sp"/>
  35.     </LinearLayout>

  36.     <ImageView
  37.         android:id="@+id/imageView1"
  38.         android:layout_width="match_parent"
  39.         android:layout_height="0dp"
  40.         android:layout_weight="1"
  41.         android:src="@drawable/bj1"
  42.         android:scaleType="fitCenter"
  43.         />

  44.     <LinearLayout
  45.         android:layout_width="match_parent"
  46.         android:layout_height="wrap_content"
  47.         android:layout_gravity="center_horizontal"
  48.         android:layout_marginTop="5dp">

  49.         <Button
  50.             android:id="@+id/img_dengbisuofang"
  51.             android:layout_width="wrap_content"
  52.             android:layout_height="wrap_content"
  53.             android:text="@string/activity_img_dengbisuofang"
  54.             android:textSize="16sp"/>

  55.         <Button
  56.             android:id="@+id/img_caiqiesuofang"
  57.             android:layout_width="wrap_content"
  58.             android:layout_height="wrap_content"
  59.             android:text="@string/activity_img_caiqiesuofang"
  60.             android:textSize="16sp"/>

  61.         <Button
  62.             android:id="@+id/img_bianxingsuofang"
  63.             android:layout_width="wrap_content"
  64.             android:layout_height="wrap_content"
  65.             android:text="@string/activity_img_bianxingsuofang"
  66.             android:textSize="16sp"/>
  67.     </LinearLayout>

  68. </LinearLayout>
复制代码

package com.ljc.cn;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;

public class ImgActivity extends Activity {
        private Button imgNext;
        private ImageView imgView;
        private Button imgPlus;
        private Button imgMinus;
        private Button imgFitCenter;
        private Button imgCenterCrop;
        private Button imgFitXY;
        private static int alpha = 180;
        private static int i = 1;
       
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_img);
               
                imgNext = (Button)findViewById(R.id.img_next);
                imgView = (ImageView)findViewById(R.id.imageView1);
                imgPlus = (Button)findViewById(R.id.img_tigaotumingdu);
                imgMinus = (Button)findViewById(R.id.img_jiangditoumingdu);
                imgFitCenter = (Button)findViewById(R.id.img_dengbisuofang);
                imgCenterCrop = (Button)findViewById(R.id.img_caiqiesuofang);
                imgFitXY = (Button)findViewById(R.id.img_bianxingsuofang);
               
                //方法一:为提高透明度和降低透明度都添加一个监听器
//                imgPlus.setOnClickListener(new OnClickListener() {
//                        public void onClick(View arg0) {
//                                alpha += 20;
//                                if (alpha >= 255) {
//                                        Toast.makeText(ImgActivity.this, "透明度不能再提高了!", Toast.LENGTH_SHORT).show();
//                                }
//                        }
//                });
//                imgMinus.setOnClickListener(new OnClickListener() {
//                        public void onClick(View arg0) {
//                                alpha -= 20;
//                                if (alpha <= 0) {
//                                        Toast.makeText(ImgActivity.this, "透明度不能再降低了!", Toast.LENGTH_SHORT).show();       
//                                }
//                        }
//                });
               
//                方法二:为为提高透明度和降低透明度添加一个通用监听器
                OnClickListener listener1 = new OnClickListener() {
                        public void onClick(View arg0) {
                                if (arg0 == imgPlus) {
                                        alpha += 20;
                                }
                                if (arg0 == imgMinus) {
                                        alpha -= 20;
                                }
                                if (alpha >=255) {
                                        alpha = 255;
                                        Toast.makeText(ImgActivity.this, "透明度不能再提高了!", Toast.LENGTH_SHORT).show();
                                }
                                if (alpha <= 0) {
                                        alpha = 0;
                                        Toast.makeText(ImgActivity.this, "透明度不能再降低了!", Toast.LENGTH_SHORT).show();
                                }
                                imgView.setAlpha(alpha);
                        }
                };
                imgPlus.setOnClickListener(listener1);
                imgMinus.setOnClickListener(listener1);
               
               
                //下一张按钮事件的实现
                final int picId[] = {R.drawable.bj1,R.drawable.bj2,R.drawable.bj3,R.drawable.img2,R.drawable.img3};
               
                imgNext.setOnClickListener(new OnClickListener() {
                        public void onClick(View arg0) {
                                int num = picId.length;
                                if (i < num) {
                                        imgView.setImageResource(picId[i]);
                                        i++;
                                        if (i>=num) {
                                                i = 0;
                                        }
                                }
                               
                        }
                });
               
               
                //方法一:为每一个按钮添加监听器
//                imgFitCenter.setOnClickListener(new OnClickListener() {
//                        public void onClick(View arg0) {
//                                imgView.setScaleType(ScaleType.FIT_CENTER);
//                        }
//                });
//                imgCenterCrop.setOnClickListener(new OnClickListener() {
//                        public void onClick(View arg0) {
//                                imgView.setScaleType(ScaleType.CENTER_CROP);
//                        }
//                });
//                imgFitXY.setOnClickListener(new OnClickListener() {
//                        public void onClick(View arg0) {
//                                imgView.setScaleType(ScaleType.FIT_XY);
//                        }
//                });
               
                //方法二:为三个按钮添加同一个监听器
                OnClickListener listener = new OnClickListener() {
                        public void onClick(View arg0) {
                                switch (arg0.getId()) {                //获取单击按钮的id选择相对应的事件
                                case R.id.img_dengbisuofang:
                                        imgView.setScaleType(ScaleType.FIT_CENTER);
                                        break;
                                case R.id.img_caiqiesuofang:
                                        imgView.setScaleType(ScaleType.CENTER_CROP);
                                        break;
                                case R.id.img_bianxingsuofang:
                                        imgView.setScaleType(ScaleType.FIT_XY);
                                        break;
                                default:
                                        imgView.setScaleType(ScaleType.FIT_CENTER);
                                        break;
                                }
                        }
                };
                //为三个按钮设置新建的监听器
                imgFitCenter.setOnClickListener(listener);       
                imgCenterCrop.setOnClickListener(listener);
                imgFitXY.setOnClickListener(listener);
               
               
        }

       
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.img, menu);
                return true;
        }

}


QQ20141126155948.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-27 09:19:30 | 显示全部楼层
qingchen 发表于 2014-11-26 16:14
package com.ljc.cn;

import android.os.Bundle;

不错啊! 恩恩!多谢分享!以后还要多多交流呢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-27 09:19:31 | 显示全部楼层
qingchen 发表于 2014-11-26 16:14
package com.ljc.cn;

import android.os.Bundle;

不错啊! 恩恩!多谢分享!以后还要多多交流呢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-28 05:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表