Android动画——放大、缩小、旋转、平移、组合

这里写图片描述

Animation定义属性的步骤和程序

使用步骤:
1、在res目录下创建anim文件,编写xml文件的动画效果
Alpha:
fromAlpha(开始的透明度)
toAlpha(结束时的透明度)
repeatCount(重复次数)
duration(使用时长,毫秒为单位)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha 
        android:fromAlpha="0.2"        
        android:toAlpha="1.0"
        android:duration="1000"
        android:repeatCount="1"
        />

</set>

Translate:
fromXDelta(开始平移的x坐标)
fillAfter(是否记录终止位置,如果为true则会停止在最终位置)
toXDelta(停止平移的X坐标位置)
interpolator(设置加速过程,加速的形式很多,比如先加后减、自由落体的效果等)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="1000"
        android:fillAfter="false"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXDelta="200"
        android:toYDelta="200"
         />

</set>

Rotate:
fromDegrees(开始旋转的角度)
toDegrees(停止旋转的角度)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate         
        android:fromDegrees="0"
        android:toDegrees="360"
        android:duration="1000"
        />

</set>

Scale :
pivotX/pivotY:以哪个点进行缩放

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

</set>

2、MainActivity中的调用:

 case R.id.button_animation:
                Animation animation= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animation);
                mImageView.startAnimation(animation);
                break;

总结:上面这种定义属性的方法和下面主函数里的AnimationSet产生的效果是一样的,都是几种效果一起实现。

 AnimationSet animationSet = new AnimationSet(false);
                AlphaAnimation animation1 = new AlphaAnimation(0.0f, 1.0f);
                TranslateAnimation animation2 = new TranslateAnimation(-mImageView.getMeasuredWidth(), 0, 0, 0);
                RotateAnimation animation3 = new RotateAnimation(0, 360);
                ScaleAnimation animation4 = new ScaleAnimation(1, 0.5f, 1, 0.5f);
                animation1.setDuration(3000);
                animation2.setDuration(3000);
                animation3.setDuration(3000);
                animation4.setDuration(3000);
                animationSet.addAnimation(animation1);
                animationSet.addAnimation(animation2);
                animationSet.addAnimation(animation3);
                animationSet.addAnimation(animation4);
                mImageView.startAnimation(animationSet);

主函数

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mButtonAlpha;
    private Button mButtonTranslation;
    private Button mButtonScaleReduce;
    private Button mButtonScaleIncrease;
    private Button mButtonXuan;
    private Button mButtonCollect;
    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButtonAlpha = (Button) findViewById(R.id.button_alpha);
        mButtonTranslation = (Button) findViewById(R.id.button_translation);
        mButtonScaleReduce = (Button) findViewById(R.id.button_scale_reduce);
        mButtonXuan = (Button) findViewById(R.id.button_xuanzhuan);
        mButtonScaleIncrease = (Button) findViewById(R.id.button_scale_increase);
        mButtonCollect = (Button) findViewById(R.id.button_collect);
        mImageView = (ImageView) findViewById(R.id.imageview);
        mButtonAlpha.setOnClickListener(this);
        mButtonTranslation.setOnClickListener(this);
        mButtonXuan.setOnClickListener(this);
        mButtonScaleReduce.setOnClickListener(this);
        mButtonScaleIncrease.setOnClickListener(this);
        mButtonCollect.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_alpha:
                AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
                animation.setDuration(3000);
                mImageView.startAnimation(animation);
                break;
            case R.id.button_translation:
                TranslateAnimation translateAnimation = new TranslateAnimation(-mImageView.getMeasuredWidth(), 0, 0, 0);
                translateAnimation.setDuration(3000);
                mImageView.startAnimation(translateAnimation);
                break;
            case R.id.button_xuanzhuan:
                RotateAnimation rotateAnimation = new RotateAnimation(0, 360);
                rotateAnimation.setDuration(3000);
                mImageView.startAnimation(rotateAnimation);
                break;
            case R.id.button_scale_reduce:
                ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5f, 1, 0.5f);
                scaleAnimation.setDuration(3000);
                mImageView.startAnimation(scaleAnimation);
                break;
            case R.id.button_scale_increase:
                ScaleAnimation scaleAnimation1 = new ScaleAnimation(0.5f, 1, 0.5f, 1);
                scaleAnimation1.setDuration(3000);
                mImageView.startAnimation(scaleAnimation1);
                break;
            case R.id.button_collect:
                AnimationSet animationSet = new AnimationSet(false);
                AlphaAnimation animation1 = new AlphaAnimation(0.0f, 1.0f);
                TranslateAnimation animation2 = new TranslateAnimation(-mImageView.getMeasuredWidth(), 0, 0, 0);
                RotateAnimation animation3 = new RotateAnimation(0, 360);
                ScaleAnimation animation4 = new ScaleAnimation(1, 0.5f, 1, 0.5f);
                animation1.setDuration(3000);
                animation2.setDuration(3000);
                animation3.setDuration(3000);
                animation4.setDuration(3000);
                animationSet.addAnimation(animation1);
                animationSet.addAnimation(animation2);
                animationSet.addAnimation(animation3);
                animationSet.addAnimation(animation4);
                mImageView.startAnimation(animationSet);
                break;

        }
    }
}

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="#ee82ee"
    android:orientation="vertical" tools:context=".MainActivity">

<Button
    android:text="透明"
    android:id="@+id/button_alpha"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
    <Button
        android:text="平移"
        android:id="@+id/button_translation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:text="旋转"
        android:id="@+id/button_xuanzhuan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:text="缩小"
        android:id="@+id/button_scale_reduce"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:text="放大"
        android:id="@+id/button_scale_increase"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:text="效果和在一起"
        android:id="@+id/button_collect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/zhaoliying"
        android:layout_gravity="center"/>

</LinearLayout>
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Android平台上,可以使用一些常用的手势操作来实现图片的平移、双指缩放和双击放大缩小。 首先,为了实现平移功能,可以使用触摸事件的ACTION_MOVE来处理手指在屏幕上滑动的操作。通过计算滑动距离,可以改变图片的位置从而实现平移效果。 其次,双指缩放可以使用触摸事件的ACTION_POINTER_DOWN和ACTION_POINTER_UP来追踪并处理触摸屏幕上第二个手指按下和松开的操作。在处理时,可以通过计算两个手指触摸点之间的距离来改变图片的缩放比例,从而实现双指缩放效果。 最后,双击放大缩小可以通过设置一个双击的监听器来实现。在双击监听器的回调方法中,可以根据当前的图片缩放状态来判断是进行放大操作还是缩小操作。并根据需要更改图片的缩放比例,实现双击放大缩小功能。 在实现上述功能时,可以使用Android提供的一些核心类来辅助开发,如View、MotionEvent、GestureDetector等。同时,还可以结合自定义View和动画效果来提升用户体验,使图片的平移缩放和双击动作更加流畅和自然。 总之,通过合理运用触摸事件、手势识别、双击监听器等技术,可以在Android上实现图片的平移、双指缩放和双击放大缩小功能,从而提升用户与应用程序的交互体验。 ### 回答2: 在Android开发中,可以通过使用ImageView控件来实现对图片的平移、双指缩放和双击放大缩小操作。 1. 平移:可以通过使用Matrix类对ImageView进行变换来实现图片的平移。首先,我们需要为ImageView设置一个OnTouchListener来监听用户的手势操作。在手势操作的回调方法中,可以通过获取手指按下和移动的位置差来计算出需要平移的距离,然后通过设置Matrix的平移矩阵来实现图片的平移效果。 2. 双指缩放:可以通过GestureDetector类来监听双指缩放手势。在手势操作的回调方法中,可以获取到两个手指的起始位置和当前位置,通过计算两个手指之间的距离差来判断缩放的比例。然后,再通过设置Matrix的缩放矩阵来实现图片的缩放效果。需要注意的是,在进行缩放操作时,还需要考虑到手指中心点的位置变化。 3. 双击放大缩小:也可以通过GestureDetector类来监听双击手势。在手势操作的回调方法中,可以判断是否发生了双击事件,并获取到双击的位置。当双击事件发生时,可以通过判断当前图片的缩放比例来决定是放大还是缩小。通过设置Matrix的缩放矩阵和平移矩阵来实现图片的放大缩小效果。 需要注意的是,为了实现图片的平移、双指缩放和双击放大缩小操作,我们需要对ImageView进行一系列的监听和处理操作。同时,为了方便管理和控制这些操作,可以将相关的代码封装成一个自定义的图片控件,并在该控件中进行处理。 ### 回答3: Android 提供了多种方法来实现图片的平移、双指缩放和双击放大缩小功能。 首先,要实现图片的平移,我们可以使用 GestureDetector 类。可以通过重写 onScroll() 方法来检测用户的滑动手势,然后将图片的位置进行相应的调整,从而实现平移效果。 其次,要实现双指缩放功能,我们可以借助 ScaleGestureDetector 类。通过重写 onScale() 方法,我们可以获取用户的缩放手势,并根据手势的缩放比例调整图片的尺寸,从而实现双指缩放的效果。 最后,要实现双击放大缩小功能,我们可以使用 GestureDetector 类。可以通过重写 onDoubleTap() 方法来检测用户的双击手势,然后根据当前图片的尺寸和双击事件的位置,来判断是进行放大还是缩小操作,最后将图片的尺寸进行相应的调整,实现双击放大缩小的效果。 需要注意的是,以上的功能实现是基于 Android 的原生 API,还有其他第三方库也提供了更便捷的方法来实现这些功能,例如 PhotoView 库,它提供了更多灵活的选项和交互效果,可以更加方便地实现图片的平移缩放和双击操作。 综上所述,Android 针对图片的平移、双指缩放和双击放大缩小,可以通过重写相应的手势监听方法或使用第三方库来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值