ImageButton动态旋转效果

来源:互联网 发布:最优化理论与算法ppt 编辑:程序博客网 时间:2024/06/04 17:57

1、首先在res/anim下新建一个rotate.xml


<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="36000" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:duration="1000" />

其中toDegrees是旋转圈数,pivotX和privotY是以(x,y)点旋转,duration是速度。



2、重写ImageButton类,命名为MyCustomButton.java


public class MyCustomButton extends ImageButton{private static String TAG = "HIPPO_DEBUG";private Animation buttonAnimation;public MyCustomButton(Context context){super(context);// TODO Auto-generated constructor stubbuttonAnimation = AnimationUtils.loadAnimation(this.getContext(),R.anim.rotate);this.setOnTouchListener(new OnTouchListener(){@Overridepublic boolean onTouch(View v, MotionEvent event){// TODO Auto-generated method stubLog.i(TAG, "MyCustomButton onTouchListener()");MyCustomButton.this.startAnimation(buttonAnimation);return false;}});/* * No Use for Custom onClickListener() this.setOnClickListener(new * OnClickListener() { *  * @Override public void onClick(View v) { // TODO Auto-generated method * stub MyCustomButton.this.startAnimation(buttonAnimation); } }); */}public MyCustomButton(Context context, AttributeSet attrs, int defStyle){super(context, attrs, defStyle);// TODO Auto-generated constructor stub}public MyCustomButton(Context context, AttributeSet attrs){super(context, attrs);// TODO Auto-generated constructor stub}/* * 扩展学习 *  * @Override public boolean onTouchEvent(MotionEvent event) { // TODO * Auto-generated method stub Log.i(TAG, "MyCustomButton onTouchEvent()"); * return super.onTouchEvent(event); } */}


3、xml的布局文件



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    tools:context=".MainActivity"  ><LinearLayout     android:layout_height="wrap_content"    android:layout_width="wrap_content"    android:layout_alignParentBottom="true"    android:orientation="horizontal">    <LinearLayout android:id="@+id/layout1"        android:layout_height="80dp"        android:layout_width="80dp"        android:orientation="horizontal"></LinearLayout>       <LinearLayout android:id="@+id/layout2"        android:layout_height="80dp"        android:layout_width="80dp"        android:orientation="horizontal"></LinearLayout>          <LinearLayout android:id="@+id/layout3"        android:layout_height="80dp"        android:layout_width="80dp"        android:orientation="horizontal"></LinearLayout>       <LinearLayout android:id="@+id/layout4"        android:layout_height="80dp"        android:layout_width="80dp"        android:orientation="horizontal"></LinearLayout>    </LinearLayout></RelativeLayout>


4、创建和使用按钮,以其中一个为例


mLinearLayout1 = (LinearLayout) findViewById(R.id.layout1);LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);mImageButton1 = new MyCustomButton(MainActivity.this);mLinearLayout1.addView(mImageButton1, llp1);mImageButton1.setImageDrawable(getResources().getDrawable(R.drawable.open));