布局
<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="長(zhǎng)按開(kāi)始搜索" />
<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);
// 第二個(gè)參數(shù)"rotation"表明要執(zhí)行旋轉(zhuǎn)
// 0f -> 360f,從旋轉(zhuǎn)360度,也可以是負(fù)值们妥,負(fù)值即為逆時(shí)針旋轉(zhuǎn),正值是順時(shí)針旋轉(zhuǎn)淮腾。
anim = ObjectAnimator.ofFloat(iv_line, "rotation", 0f, 359f);
// 動(dòng)畫(huà)的持續(xù)時(shí)間,執(zhí)行多久屉佳?
anim.setDuration(5000);
anim.setRepeatCount(-1);// 設(shè)置動(dòng)畫(huà)重復(fù)次數(shù)谷朝,這里-1代表無(wú)限
// anim.setRepeatMode(Animation.REVERSE);//設(shè)置動(dòng)畫(huà)循環(huán)模式。
anim.setInterpolator(new LinearInterpolator());
}
/**
* 延時(shí)一秒后可再次點(diǎn)擊武花。
*/
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("松開(kāi)停止搜索");
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("長(zhǎng)按開(kāi)始搜索");
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();
};
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者