本帥由于最近項目需要一種類似中國電信首頁這樣的特效脓鹃,于是我就去下載了一個看了一下忧设,還不錯崔泵。我艸又在這扯犢子干哈,別tm浪灰我時間S比险污,代你們說了哈
首先看看中國電信首頁(其實還有其他特效痹愚,不錯這個app)
中國電信首頁
沒做過啊,剛開始自己想的是自定義一個布局里面裝子控件按照圓形來排列蛔糯,然后旋轉(zhuǎn)這個布局拯腮,想著是沒毛病啊,但是我這個人技術(shù)不咋地蚁飒,自己敲估計是敲不出來了动壤,先看看人家怎么寫的吧,然后我們同事說這個就是旋轉(zhuǎn)木馬特效啊淮逻,我靠“旋轉(zhuǎn)木馬”感覺瞬間就高大上了
于是我就找了一下旋轉(zhuǎn)木馬的特效 找到如下這種的
旋轉(zhuǎn)木馬式
如果只需要這個的就不用往下看了琼懊,畢竟時間就是代碼阁簸,代碼就是錢啊
該項目的鏈接:非常方便的引用 去吧
然后感覺不對啊我要的洗過也沒這么復(fù)雜啊
于是我就找到了這個東西,圓形的菜單哼丈,那咱們先看看他的真身是啥
就是這個
這個妖怪呢可以說是旋轉(zhuǎn)启妹,交換(兩個點相互交換),事件監(jiān)聽那是樣樣精通啊
那么只需要這個東西的慢走不送
好吧削祈,那看一下我修改后的妖孽吧
這啥玩意兒啊
好吧 開始我的搬運工表演
第一個就不說了翅溺,賊猛
第二個如果不想去下載的直接copy吧
這就是一個自定義VIew RoundSpinView.java
packagechege.zycf.com.testroate.view;
importandroid.content.Context;
importandroid.content.res.TypedArray;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.DashPathEffect;
importandroid.graphics.Paint;
importandroid.graphics.PaintFlagsDrawFilter;
importandroid.graphics.PathEffect;
importandroid.graphics.Rect;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.util.AttributeSet;
importandroid.util.Log;
importandroid.view.GestureDetector;
importandroid.view.GestureDetector.SimpleOnGestureListener;
importandroid.view.MotionEvent;
importandroid.view.View;
importchege.zycf.com.testroate.R;
/**
* 圓盤式的view
*
*@authorboy
*
*/
public classRoundSpinViewextendsView {
privatePaintmPaint=newPaint();
privatePaintFlagsDrawFilterpfd;
private intstartMenu;//菜單的第一張圖片的資源id
// stone列表
privateBigStone[]mStones;
// 數(shù)目
private static final intSTONE_COUNT=20;
// 圓心坐標
private intmPointX=0,mPointY=0;
// 半徑
private intmRadius=0;
// 每兩個點間隔的角度
private intmDegreeDelta;
private intmenuRadius;// 菜單的半徑
private intmCur= -1;// 正在被移動的menu;
private boolean[]quadrantTouched;//對每個象限觸摸情況的記錄
// Touch detection
privateGestureDetectormGestureDetector;
privateonRoundSpinViewListenermListener;//自定義事件監(jiān)聽器
private final static intTO_ROTATE_BUTTON=0;//旋轉(zhuǎn)按鈕;
privateHandlerhandler=newHandler(){
public voidhandleMessage(Message msg) {
switch(msg.what) {
caseTO_ROTATE_BUTTON:
floatvelocity = Float.parseFloat(msg.obj.toString());
rotateButtons(velocity/75);
velocity /=1.0666F;
newThread(newFlingRunnable(velocity)).start();
break;
default:
break;
}
};
};
public interfaceonRoundSpinViewListener{
public voidonSingleTapUp(intposition);//監(jiān)聽每個菜單的單擊事件
}
publicRoundSpinView(Context context,AttributeSet attrs) {
super(context,attrs);
if(attrs!=null){
TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.RoundSpinView);
startMenu= a.getResourceId(R.styleable.RoundSpinView_menuStart,0);
}
pfd=newPaintFlagsDrawFilter(0,Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
mPaint.setColor(Color.WHITE);
mPaint.setStrokeWidth(2);
mPaint.setAntiAlias(true);//消除鋸齒
mPaint.setStyle(Paint.Style.STROKE);//繪制空心圓
PathEffect effects =newDashPathEffect(new float[]{5,5,5,5},1);
mPaint.setPathEffect(effects);
quadrantTouched=new boolean[] {false, false, false, false, false};
mGestureDetector=newGestureDetector(getContext(),
newMyGestureListener());
setupStones();
}
@Override
protected voidonMeasure(intwidthMeasureSpec, intheightMeasureSpec) {
//TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
mPointX=this.getMeasuredWidth()/2;
mPointY=this.getMeasuredHeight()/2;
//初始化半徑和菜單半徑
// mRadius = mPointX-mPointX/5;
// 修改過的
mRadius=600;
mPointY=mPointX-200;
menuRadius= (int)(mPointX/5.5);
//修改過的
computeCoordinates();
}
/**
* 初始化每個點
*/
private voidsetupStones() {
mStones=newBigStone[STONE_COUNT];
BigStone stone;
intangle =270;
mDegreeDelta=360/STONE_COUNT;
for(intindex =0;index
stone =newBigStone();
if(angle >=360) {
angle -=360;
}else if(angle <0){
angle +=360;
}
stone.angle= angle;
stone.bitmap= BitmapFactory.decodeResource(getResources(),
startMenu+ index %3);//修改過的
angle +=mDegreeDelta;
mStones[index] = stone;
}
}
/**
* 重新計算每個點的角度
*/
private voidresetStonesAngle(floatx, floaty) {
intangle = computeCurrentAngle(x,y);
Log.d("RoundSpinView","angle:"+ angle);
for(intindex =0;index
mStones[index].angle= angle;
angle +=mDegreeDelta;
}
}
/**
* 計算每個點的坐標
*/
private voidcomputeCoordinates() {
BigStone stone;
for(intindex =0;index
stone =mStones[index];
stone.x=mPointX
+ (float) (mRadius* Math.cos(Math.toRadians(stone.angle)));
stone.y=mPointY
+ (float) (mRadius* Math.sin(Math.toRadians(stone.angle)));
}
}
/**
* 計算某點的角度
*
*@paramx
*@paramy
*@return
*/
private intcomputeCurrentAngle(floatx, floaty) {
floatdistance = (float) Math
.sqrt(((x -mPointX) * (x -mPointX) + (y -mPointY)
* (y -mPointY)));
intdegree = (int) (Math.acos((x -mPointX) / distance) *180/ Math.PI);
if(y
degree = -degree;
}
Log.d("RoundSpinView","x:"+ x +",y:"+ y +",degree:"+ degree);
returndegree;
}
private doublestartAngle;
@Override
public booleandispatchTouchEvent(MotionEvent event) {
// resetStonesAngle(event.getX(), event.getY());
// computeCoordinates();
// invalidate();
intx,y;
if(event.getAction() == MotionEvent.ACTION_DOWN) {
x = (int) event.getX();
y = (int) event.getY();
mCur= getInCircle(x,y);
if(mCur== -1) {
startAngle= computeCurrentAngle(x,y);
}
}else if(event.getAction() == MotionEvent.ACTION_MOVE) {
x = (int) event.getX();
y = (int) event.getY();
if(mCur!= -1) {
mStones[mCur].x= x;
mStones[mCur].y= y;
invalidate();
}else{
doublecurrentAngle = computeCurrentAngle(x,y);
rotateButtons(startAngle- currentAngle);
startAngle= currentAngle;
}
}else if(event.getAction() == MotionEvent.ACTION_UP) {
x = (int) event.getX();
y = (int) event.getY();
if(mCur!= -1) {
computeCoordinates();
intcur = getInCircle(x,y);
if(cur !=mCur&& cur != -1) {
intangle =mStones[mCur].angle;
mStones[mCur].angle=mStones[cur].angle;
mStones[cur].angle= angle;
}
computeCoordinates();
invalidate();
mCur= -1;
}
}
// set the touched quadrant to true
quadrantTouched[getQuadrant(event.getX() -mPointX,
mPointY- event.getY())] =true;
mGestureDetector.onTouchEvent(event);
return true;
}
private classMyGestureListenerextendsSimpleOnGestureListener {
@Override
public booleanonFling(MotionEvent e1,MotionEvent e2, floatvelocityX,
floatvelocityY) {
// get the quadrant of the start and the end of the fling
intq1 =getQuadrant(e1.getX() -mPointX,mPointY- e1.getY());
intq2 =getQuadrant(e2.getX() -mPointX,mPointY- e2.getY());
// the inversed rotations
if((q1 ==2&& q2 ==2&& Math.abs(velocityX) < Math
.abs(velocityY))
|| (q1 ==3&& q2 ==3)
|| (q1 ==1&& q2 ==3)
|| (q1 ==4&& q2 ==4&& Math.abs(velocityX) > Math
.abs(velocityY))
|| ((q1 ==2&& q2 ==3) || (q1 ==3&& q2 ==2))
|| ((q1 ==3&& q2 ==4) || (q1 ==4&& q2 ==3))
|| (q1 ==2&& q2 ==4&&quadrantTouched[3])
|| (q1 ==4&& q2 ==2&&quadrantTouched[3])) {
// CircleLayout.this.post(new FlingRunnable(-1
// * (velocityX + velocityY)));
newThread(newFlingRunnable(velocityX+velocityY)).start();
}else{
// the normal rotation
// CircleLayout.this
// .post(new FlingRunnable(velocityX + velocityY));
newThread(newFlingRunnable(-(velocityX+velocityY))).start();
}
return true;
}
@Override
public booleanonSingleTapUp(MotionEvent e) {
intcur = getInCircle((int)e.getX(),(int)e.getY());
if(cur!=-1){
if(mListener!=null){
mListener.onSingleTapUp(cur);
}
// Toast.makeText(getContext(), "position:"+cur, 0).show();
return true;
}
return false;
}
}
private classFlingRunnableimplementsRunnable{
private floatvelocity;
publicFlingRunnable(floatvelocity){
this.velocity= velocity;
}
@Override
public voidrun() {
//TODO Auto-generated method stub
if(Math.abs(velocity)>=200){
Message message = Message.obtain();
message.what=TO_ROTATE_BUTTON;
message.obj=velocity;
handler.sendMessage(message);
}
}
}
/**
*@returnThe selected quadrant.
*/
private static intgetQuadrant(doublex, doubley) {
if(x >=0) {
returny >=0?1:4;
}else{
}
returny >=0?2:3;
}
/*
* 旋轉(zhuǎn)菜單按鈕
*/
private voidrotateButtons(doubledegree) {
for(inti =0;i
mStones[i].angle-= degree;
if(mStones[i].angle<0) {
mStones[i].angle+=360;
}else if(mStones[i].angle>=360){
mStones[i].angle-=360;
}
}
computeCoordinates();
invalidate();
}
@Override
public voidonDraw(Canvas canvas) {
//畫一個白色的圓環(huán)
canvas.drawCircle(mPointX,mPointY,mRadius,mPaint);
//將每個菜單畫出來
for(intindex =0;index
if(!mStones[index].isVisible)
continue;
drawInCenter(canvas,mStones[index].bitmap,mStones[index].x,
mStones[index].y);
}
}
/**
* 把中心點放到中心處
*
*@paramcanvas
*@parambitmap
*@paramleft
*@paramtop
*/
private voiddrawInCenter(Canvas canvas,Bitmap bitmap, floatleft,
floattop) {
Rect dst =newRect();
dst.left= (int) (left -menuRadius);
dst.right= (int) (left +menuRadius);
dst.top= (int) (top -menuRadius);
dst.bottom= (int) (top +menuRadius);
canvas.setDrawFilter(pfd);
canvas.drawBitmap(bitmap, null,dst,mPaint);
}
private intgetInCircle(intx, inty) {
for(inti =0;i
BigStone stone =mStones[i];
intmx = (int) stone.x;
intmy = (int) stone.y;
if(((x - mx) * (x - mx) + (y - my) * (y - my))
*menuRadius) {
returni;
}
}
return-1;
}
public voidsetOnRoundSpinViewListener(onRoundSpinViewListener listener){
this.mListener= listener;
}
classBigStone {
// 圖片
Bitmapbitmap;
// 角度
intangle;
// x坐標
floatx;
// y坐標
floaty;
// 是否可見
booleanisVisible=true;
}
}
自定義屬性里面 attrs
<declare-styleable name = "RoundSpinView">
<attr name="menuStart" formart="reference" />
</declare-styleable>
使用和監(jiān)聽
就是這個鬼
這兩個東西我合在了一起髓抑,源碼稍后為大家附上咙崎,請騷等各位大爺。大妹子
嘿嘿 傳上去了 我的git去吧
csdn下載地址地址