ImageButton动态旋转效果

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 stub
		buttonAnimation = AnimationUtils.loadAnimation(this.getContext(),
				R.anim.rotate);

		this.setOnTouchListener(new OnTouchListener()
		{
			@Override
			public boolean onTouch(View v, MotionEvent event)
			{
				// TODO Auto-generated method stub
				Log.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));


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值