美文网首页
Android可控图片旋转

Android可控图片旋转

作者: laksg2009 | 来源:发表于2016-08-19 15:21 被阅读60次

    布局

    <RelativeLayout 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:background="@drawable/icon_background"
        tools:context="com.rotatedemo.MainActivity" >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/view_ico_searching" />
            <ImageView 
            android:id="@+id/iv_line"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
             android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ico_searching_turn"
            />
        
        <Button
            android:id="@+id/bt_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="30dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:textColor="@android:color/white"
            android:background="@drawable/icon_ld_button_normal"
            android:text="长按开始搜索" />
        <Button
            android:id="@+id/bt_lucency"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="30dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:textColor="@android:color/white"
            android:background="@drawable/icon_ld_button"
            android:visibility="gone"
            />
        
    
        
    </RelativeLayout>
    

    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可控图片旋转

          本文链接:https://www.haomeiwen.com/subject/vewvsttx.html