阳光网驿-企业信息化交流平台【DTC零售连锁全渠道解决方案】

 找回密码
 注册

QQ登录

只需一步,快速开始

扫描二维码登录本站

手机号码,快捷登录

老司机
查看: 1101|回复: 0

[转帖] android图片由小慢慢变大的动画效果简单讲解

[复制链接]
  • TA的每日心情
    开心
    2012-3-7 10:15
  • 签到天数: 11 天

    [LV.3]偶尔看看II

    发表于 2012-1-6 10:17:18 | 显示全部楼层 |阅读模式
    • 一、概述
    •   RatingBar是SeekBar和ProgressBar的扩展,用星星来评级。使用的默认大小RatingBar时,用户可以触摸/拖动或使用键来设置评分,它有俩种样式(大、小),其中大的只适合指示,不适合于用户交互。
    •   二、实例
    •   1.布局文件
    •   < ?xml version="1.0" encoding="utf-8"?>
    •   < LinearLayout xmlns:andro
    •   androidrientation="vertical"
    •   android:paddingLeft="10dip"
    •   android:layout_width="match_parent"
    •   android:layout_height="match_parent">
    •   < RatingBar android:
    •   android:layout_width="wrap_content"
    •   android:layout_height="wrap_content"
    •   android:numStars="3"
    •   android:rating="2.5" />
    •   < RatingBar android:
    •   android:layout_width="wrap_content"
    •   android:layout_height="wrap_content"
    •   android:numStars="5"
    •   android:rating="2.25" />
    •   < LinearLayout
    •   android:layout_width="match_parent"
    •   android:layout_height="wrap_content"
    •   android:layout_marginTop="10dip">
    •   < TextView android:
    •   android:layout_width="wrap_content"
    •   android:layout_height="wrap_content" />
    •   < RatingBar android:
    •   style="?android:attr/ratingBarStyleSmall"
    •   android:layout_marginLeft="5dip"
    •   android:layout_width="wrap_content"
    •   android:layout_height="wrap_content"
    •   android:layout_gravity="center_vertical" />
    •   < /LinearLayout>
    •   < RatingBar android:
    •   style="?android:attr/ratingBarStyleIndicator"
    •   android:layout_marginLeft="5dip"
    •   android:layout_width="wrap_content"
    •   android:layout_height="wrap_content"
    •   android:layout_gravity="center_vertical" />  
    •   < /LinearLayout>
    •   2.Java代码
    •   package wjq.WidgetDemo;
    •   import android.app.Activity;
    •   import android.os.Bundle;
    •   import android.widget.RatingBar;
    •   import android.widget.TextView;
    •   import android.widget.RatingBar.OnRatingBarChangeListener;
    •   public class RatingBarDemo extends Activity implements
    •   OnRatingBarChangeListener {
    •   private RatingBar mSmallRatingBar;
    •   private RatingBar mIndicatorRatingBar;
    •   private TextView mRatingText;
    •   /*
    •   * (non-Javadoc)
    •   *
    •   * @see android.app.Activity#onCreate(android.os.Bundle)
    •   */
    •   @Override
    •   protected void onCreate(Bundle savedInstanceState) {
    •   // TODO Auto-generated method stub
    •   super.onCreate(savedInstanceState);
    •   setContentView(R.layout.ratingbarpage);
    •   mRatingText = (TextView) findViewById(R.id.rating);
    •   // We copy the most recently changed rating on to these indicator-only
    •   // rating bars
    •   mIndicatorRatingBar = (RatingBar) findViewById(R.id.indicator_ratingbar);
    •   mSmallRatingBar = (RatingBar) findViewById(R.id.small_ratingbar);
    •   // The different rating bars in the layout. Assign the listener to us.
    •   ((RatingBar)findViewById(R.id.ratingbar1)).setOnRatingBarChangeListener(this);
    •   ((RatingBar)findViewById(R.id.ratingbar2)).setOnRatingBarChangeListener(this);
    •   }
    •   @Override
    •   public void onRatingChanged(RatingBar ratingBar, float rating,
    •   boolean fromUser) {
    •   final int numStars = ratingBar.getNumStars();
    •   mRatingText.setText(
    •   " 受欢迎度" + rating + "/" + numStars);
    •   // Since this rating bar is updated to reflect any of the other rating
    •   // bars, we should update it to the current values.
    •   if (mIndicatorRatingBar.getNumStars() != numStars) {
    •   mIndicatorRatingBar.setNumStars(numStars);
    •   mSmallRatingBar.setNumStars(numStars);
    •   }
    •   if (mIndicatorRatingBar.getRating() != rating) {
    •   mIndicatorRatingBar.setRating(rating);
    •   mSmallRatingBar.setRating(rating);
    •   }
    •   final float ratingBarStepSize = rati群里的一个朋友提问:
    •   问大家个问题,图片由小慢慢变大的动画效果,在android 上怎么做?谁知道,告诉我。谢谢了
    •   这用到的是android的基础tweend animation ,首先在res目录下建一个anim目录,新建一个anim.xml文件,在这里面设置你想的scale动画效果,在activity中用AnimationUtils载入这个动画效果
    •   再用ImageView实例对象startAnimation即可。
    •   但这位朋友接着问
    •   这些理论知识我都知道。只是我写的老有问题。你能不能写个简单的例子,打包发给我。麻烦了。
    •   于是看了一下他写的代码:
    •   1. package com.test;
    •   2.
    •   3. import android.app.Activity;
    •   4. import android.graphics.drawable.AnimationDrawable;
    •   5. import android.os.Bundle;
    •   6. import android.view.animation.Animation;
    •   7. import android.view.animation.AnimationUtils;
    •   8. import android.widget.ImageView;
    •  9. import android.widget.LinearLayout;
    •   10.
    •   11. public class testDrawable extends Activity {
    •   12. LinearLayout mLinearLayout;
    •   13. AnimationDrawable rocketAnimation;
    •   14.
    •   15. protected void onCreate(Bundle savedInstanceState) {
    •   16. super.onCreate(savedInstanceState);
    •   17. setContentView(R.layout.main);
    •   18. ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
    •   19. spaceshipImage.setBackgroundResource(R.drawable.my_image);
    •   20. Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.drawable.hyperspace_jump);
    •   21. spaceshipImage.startAnimation(hyperspaceJumpAnimation);
    •   22.
    •   23. spaceshipImage.setBackgroundResource(R.anim.animation_drawable);
    •   24. rocketAnimation = (AnimationDrawable) spaceshipImage.getBackground();
    •   25. }
    •   26. }
    •   spaceshipImage.setBackgroundResource(R.drawable.my_image);
    •   虽然此处设置的是ImageView的背景图片不是前景图片(直接对背景图片设置透明属性是不起作用的),但这个动画效果针对的是整个ImageView进行缩放,用前景图片和背景图片都是可以的。
    •   又看了一下anim.xml
    •   1. < set xmlns:andro
    •   2. android:shareInterpolator="false">
    •   3. < scale
    •   4. android:interpolator= "@android:anim/accelerate_interpolator"
    •   5. android:fromXScale="0.0"
    •   6. android:toXScale="1.4"
    •   7. android:fromYScale="0.0"
    •   8. android:toYScale="1.4"
    •   9. android:pivotX="50%"
    •   10. android:pivotY="50%"
    •   11. android:fillAfter="false"
    •   12. android:startOffset="700"
    •   13. android:duration="2000"
    •   14. android:repeatCount="10"
    •   15. android:repeatMode="restart"
    •   16. />
    •   17. < /set>
    •   也没问题
    •   android:repeatCount="10"
    •   android:repeatMode="restart"
    •   重复这个动画10次
    •   又仔细看了一下代码,发现问题可能在这了,需要注意的两个问题是一是源代码中的第23、24这两句应去掉      spaceshipImage.setBackgroundResource(R.anim.animation_drawable);rocketAnimation = (AnimationDrawable) spaceshipImage.getBackground();如果是想对一个ImageView添加多个效果,在anim.xml中进行设置就可以了。 再就是,注意如果是直接导入的源码,需在BuildPath中添加相应SDK的android.jar文件以及图片文件要放在drawable目录中
    •   ngBar.getStepSize();
    •   if (mIndicatorRatingBar.getStepSize() != ratingBarStepSize) {
    •   mIndicatorRatingBar.setStepSize(ratingBarStepSize);
    •   mSmallRatingBar.setStepSize(ratingBarStepSize);
    •   }
    •   }
    •   }
    楼主热帖
    启用邀请码注册,提高发帖质量,建设交流社区
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

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