Android可控图片旋转

布局


    
        
    
    

MainActivity类

package com.rotatedemo;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnTouchListener {
    private Button bt_search;
    private Button bt_lucency;
    private ImageView iv_line;
    private ObjectAnimator anim;
    boolean anim_state = false;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt_search = (Button) findViewById(R.id.bt_search);
        bt_lucency = (Button) findViewById(R.id.bt_lucency);
        iv_line = (ImageView) findViewById(R.id.iv_line);
        bt_search.setOnTouchListener(this);

        // 第二个参数"rotation"表明要执行旋转
        // 0f -> 360f,从旋转360度,也可以是负值,负值即为逆时针旋转,正值是顺时针旋转。
        anim = ObjectAnimator.ofFloat(iv_line, "rotation", 0f, 359f);
        // 动画的持续时间,执行多久?
        anim.setDuration(5000);
        anim.setRepeatCount(-1);// 设置动画重复次数,这里-1代表无限
        // anim.setRepeatMode(Animation.REVERSE);//设置动画循环模式。
        anim.setInterpolator(new LinearInterpolator());
    }

    /**
     * 延时一秒后可再次点击。
     */
    private void delayed() {
        bt_lucency.setVisibility(View.VISIBLE);
        Runnable runnable = new Runnable() {
            public void run() {
                bt_lucency.setVisibility(View.GONE);
            }
        };
        handler.postDelayed(runnable,1000);
    }

    @SuppressLint("NewApi")
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (v.getId() == R.id.bt_search) {
                delayed();
                if (anim_state) {
                    anim.resume();
                } else {
                    anim_state = true;
                    anim.start();
                }
                bt_search.setText("松开停止搜索");
                bt_search.setBackgroundResource(R.drawable.icon_ld_button_check);
                return true;
            }
        case MotionEvent.ACTION_UP:
            if (v.getId() == R.id.bt_search) {
                anim.pause();
                bt_search.setText("长按开始搜索");
                bt_search.setBackgroundResource(R.drawable.icon_ld_button_normal);
                return true;
            }
        }
        return false;
    }

    private Handler handler = new Handler() {
//      public void handleMessage(Message msg) {
//      }
    };

    protected void onDestroy() {
        anim.cancel();
    };
}

你可能感兴趣的:(Android可控图片旋转)