手把手教你如何在安卓中使图片进行旋转

  • Post author:
  • Post category:其他


下面介绍一些关于在Android中,使图片进行旋转的一些方法,当然这里主要介绍旋转的,如果有其他的动画效果,可以类似的进行修改::


方法一:

1、首先在res文件夹下创建一个名字为anim的文件夹,名字不要写错

2、在anim里面创建一个xlm文件:img_animation.xml,这个名字随便写都可以,注意不要大写,里面的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="5000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:repeatMode="restart"
        android:toDegrees="360" />

    <!-- 参数的含义
    duration:时间</span>
    fromDegrees="0":  从几度开始转</span>t
    oDegrees="360" : 旋转多少度</span>
    pivotX="50%:旋转中心距离view的左顶点为50%距离,
    pivotY="50%: 距离view的上边缘为50%距离
    repeatCount="-1":重复次数,-1为一直重复
    repeatMode="restart":重复模式,restart从头开始重复

    -->

</set>

然后,再去你需要让哪个图片进行旋转就添加下面的方法:

backgroundLight = (ImageView) findViewById(R.id.medallight);
	Animation animation = AnimationUtils.loadAnimation(this, R.anim.imagerota); 
        LinearInterpolator lin = new LinearInterpolator();//设置动画匀速运动
        animation.setInterpolator(lin);
        backgroundLight.startAnimation(animation);

通过这样的方法,你就实现了图片旋转的效果了。


方法二:

用线程的方法去进行旋转。

package com.readboy.game.susuanlianximobile.others;

import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RotateView extends ImageView implements Runnable {

    private float mCurDegree = 0;//当前旋转角度
    public RotateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        new Thread(this).start();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
                            int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        //设定旋转中心
        setPivotX(getMeasuredWidth()/2);
        setPivotY(getMeasuredHeight()/2);
    }

    @Override
    public void run() {
        while(true){
            setRotation(mCurDegree);
            mCurDegree += 5;
            postInvalidate();
            SystemClock.sleep(200);
        }
    }
}

通过这样的方法,主要是由于重写了ImageView,所以在你要让某个图片进行旋转的时候,千万要记得在布局中,将那图片的ImageView改为这个类的名字。。。。切记!!!!!!!!!!!!!!!!

比如:

改变前:

<ImageView
          android:id="@+id/medallight"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/medallight"
          android:layout_centerInParent="true"

改变后为:

<com.readboy.game.susuanlianximobile.others.RotateView
          android:id="@+id/medallight"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/medallight"
          android:layout_centerInParent="true"
          />

前面的是包名,后面的是类名就可以了。


方法三::

直接用安卓里面的

RotateAnimation类,进行也可以。(这种比较简单)

 iv = (ImageView)findViewById(R.id.image);
        RotateAnimation animation = new RotateAnimation(0, 360);
        animation.setDuration(100000);//设定转一圈的时间
        animation.setRepeatCount(Animation.INFINITE);//设定无限循环
        animation.setRepeatMode(Animation.RESTART);
        iv.startAnimation(animation);

在安卓中,Animation这个类有很多用处的,可以去看看API进行更多的效果。。

上面就是几种比较好的方法了,谢谢网上的资源进行整合了!!!!欢迎交流!!!!!!!!!!




    <rotate
        android:duration="5000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:repeatMode="restart"
        android:toDegrees="360" />

    <!-- 参数的含义
    duration:时间</span>
    fromDegrees="0":  从几度开始转</span>t
    oDegrees="360" : 旋转多少度</span>
    pivotX="50%:旋转中心距离view的左顶点为50%距离,
    pivotY="50%: 距离view的上边缘为50%距离
    repeatCount="-1":重复次数,-1为一直重复
    repeatMode="restart":重复模式,restart从头开始重复

    -->

</set



版权声明:本文为Cs_hnu_scw原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。