做项目遇到的。N个Button按圆形旋转。
main.xml
<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" >
<Button
android:id="@+id/move"
android:layout_alignParentBottom="true"
android:onClick="move"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="移动"/>
<com.melon.pinwheel.ButtonController
android:id="@+id/mybc"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.melon.pinwheel.ButtonController>
</RelativeLayout>
bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/ic_launcher"/>
</selector>
MainActivity.java
package com.melon.pinwheel;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity {
private int[] picIds = {R.drawable.im1,R.drawable.im2,R.drawable.im3,R.drawable.im4,R.drawable.im5};
// private int[] picIds = {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
private ButtonController controller;
private Button bt_move;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
controller = (ButtonController) findViewById(R.id.mybc);
bt_move = (Button) findViewById(R.id.move);
//初始化Bt控件
for(int i=0;i<picIds.length;i++){
final ImageButton bt = new ImageButton(this);
bt.setTag(i);
// bt.setBackgroundColor(Color.TRANSPARENT);
bt.setBackgroundResource(R.drawable.bg);
bt.setImageResource(picIds[i]);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, bt.getTag().toString(), 0).show();
}
});
// bt.setOnClickListener(this);
controller.addView(bt);
}
}
public void move(View view){
bt_move.setClickable(false);
bt_move.setEnabled(false);
controller.move();
}
@Override
protected void onDestroy() {
super.onDestroy();
controller.stop();
}
}
//核心类:控制器
package com.melon.pinwheel;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageButton;
/**
* 控制器
*
* @author melon
*
*/
public class ButtonController extends ViewGroup {
Context ctx;
/**
* 屏幕中心点X坐标
*/
private int centerPointX;
/**
* 屏幕中心点Y坐标
*/
private int centerPointY;
/**
* 半径
*/
public static final double RADIUS = 200;//圆半径
private static final double ANGLENUM = Math.PI/180;//1度等于多少弧度
private Timer timer;
private TimerTask task;
private int picWidth;//图片宽度
private int picHeight;//图片高度
private int rotatedAngle;//旋转过的角度
public ButtonController(Activity activity) {
super(activity);
ctx = activity;
}
public ButtonController(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = context;
//计算屏幕中心点位置
DisplayMetrics outMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
// centerPointX = outMetrics.widthPixels/2 - BitmapFactory.decodeResource(getResources(),R.drawable.im1).getWidth() / 2;
// centerPointY = outMetrics.heightPixels/2 - BitmapFactory.decodeResource(getResources(),R.drawable.im1).getHeight() / 2;
centerPointX = outMetrics.widthPixels/2;
centerPointY = outMetrics.heightPixels/2;
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
if(rotatedAngle>360){
rotatedAngle = 0;
}
rotatedAngle++;
//让子VIEW重新定位,旋转
for (int i = 0; i < getChildCount(); i++) {
final ImageButton vChild = (ImageButton) getChildAt(i);
final int x = (int) (centerPointX + Math.cos(((360/getChildCount())*i+rotatedAngle)*ANGLENUM)*RADIUS);
final int y = (int) (centerPointY + Math.sin(((360/getChildCount())*i+rotatedAngle)*ANGLENUM)*RADIUS);
((Activity)ctx).runOnUiThread(new Runnable() {
@Override
public void run() {
vChild.layout(x, y, x+picWidth, y+picHeight);
}
});
}
//平移
// for (int i = 0; i < getChildCount(); i++) {
// ImageButton vChild = (ImageButton) getChildAt(i);
// //vChild.layout(vChild.getLeft()+50, vChild.getTop()+100, vChild.getLeft()+50+vChild.getWidth(), vChild.getTop()+100+vChild.getHeight());
// scrollBy(-1, -1);
// }
}
};
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//测量子VIEW
for (int i = 0; i < getChildCount(); i++) {
ImageButton vChild = (ImageButton) getChildAt(i);
vChild.measure(widthMeasureSpec, heightMeasureSpec);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
picWidth = ctx.getResources().getDrawable(R.drawable.im1).getIntrinsicWidth();
picHeight = ctx.getResources().getDrawable(R.drawable.im1).getIntrinsicHeight();
for (int i = 0; i < getChildCount(); i++) {
ImageButton vChild = (ImageButton) getChildAt(i);
int x = (int) (centerPointX + Math.cos(((360/getChildCount())*i)*ANGLENUM)*RADIUS);//第一次定位,左边距(左上角顶点对应坐标x)
int y = (int) (centerPointY + Math.sin(((360/getChildCount())*i)*ANGLENUM)*RADIUS);//第一次定位,上边距(左上角顶点对应坐标y)
// vChild.layout(l+i*picWidth, t, l+(i+1)*picWidth, t+picHeight);
vChild.layout(x, y, x+picWidth, y+picHeight);
Log.i("WYL", ("第"+i+"个角度: "+(360/getChildCount())*i));
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
public void move() {
timer.scheduleAtFixedRate(task, 0, 25);
}
public void stop(){
timer.cancel();
}
}