实现像网易和QQ动态导航按钮



/**
 * @title 
 * 动态QQTab效果
 * <pre>
 * 要不断重画页面
 * </pre>
 * @author LiYaJie
 * @version 1.0 2010-12-11 下午08:39:25
 */
public class QQTabLayout extends LinearLayout {//自定义动画导航vie
/**
* the speed of the slide
*/
private static final int SLIP_SPEED = 1;
// 透明背景
private BitmapDrawable transprenceBackground = null;


private Rect mCurRect;// 当前的区域


private Rect mEndRect;// 结束的区域


public boolean isKill = false;// 是否线程运行


private boolean isSynChronize = true;// 重画与循环同步

/**
* 接收XML文件配置属性

* @param context
* @param attrs
*/
public QQTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initVariable(context);
}


public QQTabLayout(Context context) {
super(context);
initVariable(context);
}


private void initVariable(Context context) {
transprenceBackground = (BitmapDrawable) getResources().getDrawable(
R.drawable.bg_category_selected);
mCurRect = new Rect();
mEndRect = new Rect();
}


@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
transprenceBackground.setBounds(mCurRect);
transprenceBackground.draw(canvas);// 将背景画出来
}


@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
getChildAt(0).getHitRect(mCurRect);// 第一个组件在父亲组件中所占的大小
}


/**
* 计算视图并移动

* @param curView
*/
public void startMearsure(View curView, final ICallback callback) {
curView.getHitRect(mEndRect);// 放入结束距形
if (mCurRect.right < mEndRect.right) {// 向右滑动
startWork(new SlidForward() {
@Override
public void slidForward() {
mCurRect.left += SLIP_SPEED;
mCurRect.right += SLIP_SPEED;
if (mCurRect.right >= mEndRect.right) {// 如果到达目的地停止
toDestination();
callback.isEnd(true);
}
}
});
} else {// 向左滑动
startWork(new SlidForward() {
@Override
public void slidForward() {
mCurRect.left -= SLIP_SPEED;
mCurRect.right -= SLIP_SPEED;
if (mCurRect.left <= mEndRect.left) {// 左边结束处
toDestination();// 到达目的地
callback.isEnd(true);
}
}
});
}
}


public void toDestination() {
mCurRect.left = mEndRect.left;
mCurRect.right = mEndRect.right;
isKill = true;// 结束移动
}


/**
* 开始滑动
* @param forward
*/
public void startWork(SlidForward forward) {
isKill = false;
while (!isKill) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (isSynChronize) {// 不断移动与得画同步
forward.slidForward();
postInvalidate();
}
}
}
public interface ICallback{
public void isEnd(boolean b);
}

}


/**
 * @title 移动线程
 * @author LiYaJie
 * @version 1.0 2010-12-11 下午08:56:19
 */
public class SlidThread {

private static SlidThread instance;


private SlidThread() {};

public static SlidThread getInstance(){
if(instance == null){
instance = new SlidThread();
}
return instance;
}

public void getSlidInfo(final View curView, final QQTabLayout tabLayout, final ICallback callback){
new Thread(new Runnable(){
@Override
public void run() {
tabLayout.startMearsure(curView, new QQTabLayout.ICallback() {
@Override
public void isEnd(boolean b) {
if(b){
callback.isEnd(b);
}
}
});
}

}).start();
}

public interface ICallback{
public void isEnd(boolean b);
}

}

/**

package com.qq;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;


/**
 * @title 动态QQTab效果
 * @author XiangYuan
 * @version 1.0 2010-12-11 下午08:39:25
 */
public class QQTabLayoutActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private QQTabLayout qTabLayout = null;


private Thread mHandlThread = null;


private TextView bigNews;
private TextView sports;
private TextView fun;
private TextView finance;
private TextView technology;
private TextView more;
private TextView view;
private Handler handler = new Handler(){


@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch(msg.what){
case 1:
view.setTextColor(QQTabLayoutActivity.this.getResources().getColor(R.color.white));
break;
}
}

};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
// LayoutInflater flater = (LayoutInflater)
// getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// qTabLayout = (QQTabLayout) flater.inflate(R.layout.main, null);
// ImageView message = (ImageView)
// qTabLayout.findViewById(R.id.message);
// ImageView friends = (ImageView)
// qTabLayout.findViewById(R.id.friends);
// ImageView groups = (ImageView) qTabLayout.findViewById(R.id.groups);
qTabLayout = (QQTabLayout) this.findViewById(R.id.qqLayout);
// ImageView message = (ImageView) this.findViewById(R.id.message);
// ImageView friends = (ImageView) this.findViewById(R.id.friends);
// ImageView groups = (ImageView) this.findViewById(R.id.groups);
// message.setOnClickListener(this);
// friends.setOnClickListener(this);
// groups.setOnClickListener(this);


bigNews = (TextView) this.findViewById(R.id.txt_big_news);
sports = (TextView) this.findViewById(R.id.txt_sports);
fun = (TextView) this.findViewById(R.id.txt_fun);
finance = (TextView) this.findViewById(R.id.txt_finance);
technology = (TextView) this.findViewById(R.id.txt_technology);


more = (TextView) this.findViewById(R.id.txt_more);
bigNews.setTextColor(this.getResources().getColor(R.color.white));
bigNews.setOnClickListener(this);
sports.setOnClickListener(this);
fun.setOnClickListener(this);
finance.setOnClickListener(this);
technology.setOnClickListener(this);
more.setOnClickListener(this);


// setContentView(qTabLayout);
}


@Override
public void onClick(final View v) {
if (mHandlThread != null) {
qTabLayout.isKill = true;
try {
mHandlThread.join();// 将当进程序执行完成,则执行下面的程序
} catch (InterruptedException e) {
e.printStackTrace();
}
}
view = (TextView)v;
setTextBackground();
// mHandlThread = new Thread(new SlidThread(v, qTabLayout));
// mHandlThread.start();// 执行线程
SlidThread.getInstance().getSlidInfo(v, qTabLayout, new SlidThread.ICallback(){
@Override
public void isEnd(boolean b) {
if(b){
Message msg = new Message();
msg.what = 1;
msg.obj = b;
handler.sendMessage(msg);
}
}

});
}

private void setTextBackground() {
bigNews.setTextColor(this.getResources().getColor(R.color.gray));
sports.setTextColor(this.getResources().getColor(R.color.gray));
fun.setTextColor(this.getResources().getColor(R.color.gray));
finance.setTextColor(this.getResources().getColor(R.color.gray));
technology.setTextColor(this.getResources().getColor(R.color.gray));
more.setTextColor(this.getResources().getColor(R.color.gray));
}
}
 * @title
 * @author LiYaJie
 * @version 1.0 2010-12-11 下午08:55:15
 */
public interface SlidForward {


/**
* 开始滑动方法操作
*/
public void slidForward();
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/list_item_bg"
>
<LinearLayout
android:layout_width="fill_parent" 
android:layout_height="40.0dip"
android:background="@color/light_gray">
<com.qq.QQTabLayout android:id="@+id/qqLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="30.0dip"
android:background="@color/light_gray"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:layout_gravity="center"
>
<TextView
android:id="@+id/txt_big_news" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:text="头条"
android:textColor="@color/gray"
android:textSize="20sp"
android:clickable="true"
android:gravity="center"
/>

<TextView
android:id="@+id/txt_sports" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:text="体育"
android:textColor="@color/gray"
android:textSize="20sp"
android:clickable="true"
android:gravity="center"
/>
<TextView
android:id="@+id/txt_fun" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:text="娱乐"
android:textColor="@color/gray"
android:textSize="20sp"
android:clickable="true"
android:gravity="center"
/>
<TextView
android:id="@+id/txt_finance" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:text="财经"
android:textColor="@color/gray"
android:textSize="20sp"
android:clickable="true"
android:gravity="center"
/>
<TextView
android:id="@+id/txt_technology" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:text="科技"
android:textColor="@color/gray"
android:textSize="20sp"
android:clickable="true"
android:gravity="center"
/>
<TextView
android:id="@+id/txt_more" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:text="更多"
android:textColor="@color/gray"
android:textSize="20sp"
android:clickable="true"
android:gravity="center"
/>
</com.qq.QQTabLayout>
</LinearLayout>
</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值