自定义tab bar控件 学习资料
http://blog.csdn.net/zoeice/article/details/8068671
- import java.util.Vector;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.NinePatch;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.util.DisplayMetrics;
- import android.view.GestureDetector;
- import android.view.GestureDetector.OnGestureListener;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnTouchListener;
- import android.widget.PopupWindow;
- import com.example.zoeicetabgroup.R;
- /**
- * custom tab bar
- * @author zoeice
- *
- */
- public class LPTabGroupWithGesture extends View implements OnGestureListener, OnTouchListener{
- private int tabNum = 0;
- private int curSelect = 0;
- private int curX = 0;
- private int savedClickIndex = -1;
- private int savedX = 0;
- private boolean isDraging = false;
- private int dragCurrentX = 0;
- private int dragSavedX = 0;
- private int dragSelectIndex = -1;
- private final int TEXT_SIZE = 30;
- private final int TAB_MARGIN = 30;
- private int tabWidth = 200;
- private int tabHeight = 0;
- private int tabGap = 30;// IF
- private int tabAddWidth = 90;// IF
- private Bitmap bitCur;
- private NinePatch npCur;
- private Bitmap bitNor;
- private NinePatch npNor;
- private Bitmap bitAdd;
- private int screenWidth;
- private int screenHeight;
- private float screenDensity;
- private PopupWindow popMenu;
- private OnTabItemClickListener clickListener;
- private GestureDetector mGestureDetector;
- private Vector<TabItem> tabLists = new Vector<TabItem>();
- public LPTabGroupWithGesture(Context context, Vector<TabItem> vec) {
- this(context);
- tabLists = vec;
- tabNum = vec.size();
- }
- public LPTabGroupWithGesture(Context context) {
- super(context);
- if(tabNum == 0){
- tabNum = 1;
- tabLists.add(new TabItem());
- }
- initParams(context);
- }
- /**
- * init view's params
- */
- private void initParams(Context context){
- this.setLongClickable(true);
- this.setOnTouchListener(this);
- mGestureDetector = new GestureDetector(context, this);
- mGestureDetector.setIsLongpressEnabled(true);
- DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
- screenWidth = displayMetrics.widthPixels;
- screenHeight = displayMetrics.heightPixels;
- screenDensity = displayMetrics.density;
- bitCur = BitmapFactory.decodeResource(getResources(),
- R.drawable.tab);
- npCur = new NinePatch(bitCur, bitCur.getNinePatchChunk(), null);
- tabHeight = bitCur.getHeight();
- bitNor = BitmapFactory.decodeResource(getResources(),
- R.drawable.tab);
- npNor = new NinePatch(bitNor, bitNor.getNinePatchChunk(), null);
- bitAdd = BitmapFactory.decodeResource(getResources(),
- R.drawable.tab_add);
- tabAddWidth = bitAdd.getWidth();
- }
- /**设置当前选中选项的位置*/
- public void setCurrentSelect(int index) {
- curSelect = index;
- invalidate();
- }
- /**
- * @see android.view.View#measure(int, int)
- */
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- setMeasuredDimension(screenWidth, tabHeight);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- Paint paint = new Paint();
- paint.setColor(0xff707070);
- paint.setTextSize(TEXT_SIZE);
- /*draw bg*/
- canvas.drawARGB(0xff, 0xa1, 0x9c, 0xff);
- if(isDraging){
- int dX = dragCurrentX + (tabWidth - tabGap) * curSelect;
- /*draw normal tab*/
- for (int i = tabNum - 1; i >= 0; i--) {
- if (i != curSelect) {
- int nX = curX + (tabWidth - tabGap) * i ;
- if(i > savedClickIndex && savedClickIndex > -1){
- nX -= tabWidth - tabGap;
- }
- if(i > dragSelectIndex && dragSelectIndex > -1){
- nX += tabWidth - tabGap;
- }
- Rect nR = new Rect(nX, 0, nX + tabWidth, tabHeight);
- npNor.draw(canvas, nR);
- //draw text
- canvas.save();
- canvas.clipRect(new Rect(nX + TAB_MARGIN, 0, nX + tabWidth - TAB_MARGIN, tabHeight));
- canvas.drawText(tabLists.get(i).name, nX + TAB_MARGIN, (tabHeight + TEXT_SIZE) >> 1, paint);
- canvas.restore();
- }
- }
- /*draw shape for above normal tab*/
- canvas.drawARGB(0x50, 0x00, 0x00, 0x00);
- /*draw buttom line*/
- Paint paintShape = new Paint();
- paintShape.setColor(0x50000000);
- canvas.drawRect(0, tabHeight - 2, screenWidth, tabHeight, paintShape);
- /*draw current selected tab*/
- Rect dR = new Rect(dX, 0, dX + tabWidth, tabHeight);
- npCur.draw(canvas, dR);
- //draw current text
- paint.setColor(0xff456789);
- canvas.drawText(tabLists.get(curSelect).name, dX + tabGap, (tabHeight + TEXT_SIZE) >> 1, paint);
- /*draw add button*/
- int aX = curX + (tabWidth - tabGap) * tabNum;
- if(dX + tabWidth > aX)
- canvas.drawBitmap(bitAdd, dX + tabWidth - tabGap, 0, paint);
- else
- canvas.drawBitmap(bitAdd, aX, 0, paint);
- }else{
- /*draw normal tab*/
- for (int i = tabNum - 1; i >= 0; i--) {
- if (i != curSelect) {
- int nX = curX + (tabWidth - tabGap) * i ;
- Rect nR = new Rect(nX, 0, nX + tabWidth, tabHeight);
- npNor.draw(canvas, nR);
- //draw text
- canvas.save();
- canvas.clipRect(new Rect(nX + TAB_MARGIN, 0, nX + tabWidth - TAB_MARGIN, tabHeight));
- canvas.drawText(tabLists.get(i).name, nX + TAB_MARGIN, (tabHeight + TEXT_SIZE) >> 1, paint);
- canvas.restore();
- }
- }
- /*draw shape for above normal tab*/
- canvas.drawARGB(0x50, 0x00, 0x00, 0x00);
- /*draw buttom line*/
- Paint paintShape = new Paint();
- paintShape.setColor(0x50000000);
- canvas.drawRect(0, tabHeight - 2, screenWidth, tabHeight, paintShape);
- /*draw current selected tab*/
- int cX = curX + (tabWidth - tabGap) * curSelect;
- Rect cR = new Rect(cX, 0, cX + tabWidth, tabHeight);
- npCur.draw(canvas, cR);
- //draw current text
- paint.setColor(0xff456789);
- canvas.drawText(tabLists.get(curSelect).name, cX + tabGap, (tabHeight + TEXT_SIZE) >> 1, paint);
- /*draw add button*/
- canvas.drawBitmap(bitAdd, curX + (tabWidth - tabGap) * tabNum, 0, paint);
- }
- }
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if (mGestureDetector.onTouchEvent(event)) {
- return true;
- }
- if(event.getAction() == MotionEvent.ACTION_UP) {
- if(isDraging ) {
- if(dragSelectIndex != curSelect){
- if(dragSelectIndex >= tabNum){
- dragSelectIndex = tabNum - 1;
- }
- TabItem tempItem = tabLists.elementAt(curSelect);
- tabLists.removeElementAt(curSelect);
- tabLists.add(dragSelectIndex, tempItem);
- }
- float x = event.getX();
- curSelect = (int) ((x - curX) / (tabWidth - tabGap));
- if(curSelect >= tabNum){
- curSelect = tabNum - 1;
- }
- isDraging = false;
- dragSelectIndex = -1;
- dragCurrentX = 0;
- dragSavedX = 0;
- invalidate();
- }
- }
- return false;
- }
- /**选项点击事件设置*/
- public void setOnTabItemClickListener(OnTabItemClickListener click) {
- this.clickListener = click;
- }
- /**选项点击事件接口*/
- public interface OnTabItemClickListener {
- public void onclickListenr(int index);
- }
- @Override
- public boolean onDown(MotionEvent event) {
- float x = event.getX();
- savedClickIndex = (int) ((x - curX) / (tabWidth - tabGap));
- if(savedClickIndex == curSelect){
- isDraging = true;
- dragSavedX = savedX;
- clickListener.onclickListenr(savedClickIndex);
- }
- return false;
- }
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- if(isDraging ) {
- if(dragSelectIndex != curSelect){
- if(dragSelectIndex >= tabNum){
- dragSelectIndex = tabNum - 1;
- }
- TabItem tempItem = tabLists.elementAt(curSelect);
- tabLists.removeElementAt(curSelect);
- tabLists.add(dragSelectIndex, tempItem);
- }
- float x = e2.getX();
- curSelect = (int) ((x - curX) / (tabWidth - tabGap));
- if(curSelect >= tabNum){
- curSelect = tabNum - 1;
- }
- isDraging = false;
- dragSelectIndex = -1;
- dragCurrentX = 0;
- dragSavedX = 0;
- invalidate();
- }
- return true;
- }
- @Override
- public void onLongPress(MotionEvent event) {
- }
- @Override
- public boolean onScroll(MotionEvent e1, MotionEvent e2,
- float distanceX, float distanceY) {
- int tempWidth = (tabWidth - tabGap) * tabNum + tabAddWidth;
- if(isDraging){
- //drag
- int nextX = (int) (dragSavedX + e2.getX() - e1.getX());
- dragSelectIndex = (int) ((e2.getX() - curX) / (tabWidth - tabGap));
- dragCurrentX = nextX;
- invalidate();
- }else{
- //move
- int nextX = (int) (savedX - distanceX);
- if(tempWidth > screenWidth && (nextX <=0 && nextX + tempWidth >= screenWidth)){
- curX = nextX;
- savedX = curX;
- invalidate();
- }
- }
- return false;
- }
- @Override
- public void onShowPress(MotionEvent event) {
- }
- @Override
- public boolean onSingleTapUp(MotionEvent event) {
- float x = event.getX();
- int curClickIndex = (int) ((x - curX) / (tabWidth - tabGap));
- if(savedClickIndex == curClickIndex){
- if(curClickIndex < tabNum){
- setCurrentSelect(curClickIndex);
- clickListener.onclickListenr(curClickIndex);
- }else if(curClickIndex == tabNum){
- tabLists.add(new TabItem());
- tabNum++;
- curX = screenWidth - tabAddWidth - (tabWidth - tabGap) * tabNum;
- setCurrentSelect(tabNum - 1);
- clickListener.onclickListenr(curClickIndex);
- invalidate();
- }
- }
- savedClickIndex = -1;
- savedX = curX;
- return false;
- }
- }
tabItem类:
- public class TabItem{
- public int index;
- public String name = "New T
自定义tab bar控件 学习资料的更多相关文章
- 学习笔记:Tab Bar 控件使用详解
注意这里是:Tab Bar 不是Tab Bar Controller. Tab bar是继承UIView,所以可以添加到ViewController里.是View就可以add到另一个View上去.Ta ...
- 通过编写串口助手工具学习MFC过程——(七)添加Tab Control控件
通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...
- DevExpress控件学习总结(转)
DevExpress控件学习总结 1.Navigation & Layout 1.1 Bar Manager 如果想在窗体或用户控件(user control)上添加工具条(bars)或弹 ...
- (转)sl简单自定义win窗体控件
sl简单自定义win窗体控件 相信大家接触过不少win窗体控件ChildWin子窗口就的sl自带的一个 而且网上也有很多类似的控件,而今天我和大家分享下自己制作个win窗体控件,希望对初学 ...
- WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 下拉选 ...
- C# Winform 通过FlowLayoutPanel及自定义的编辑控件,实现快速构建C/S版的编辑表单页面
个人理解,开发应用程序的目的,不论是B/S或是C/S结构类型,无非就是实现可供用户进行查.增.改.删,其中查询用到最多,开发设计的场景也最为复杂,包括但不限于:表格记录查询.报表查询.导出文件查询等等 ...
- iOS开发UI篇—Date Picker和UITool Bar控件简单介绍
iOS开发UI篇—Date Picker和UITool Bar控件简单介绍 一.Date Picker控件 1.简单介绍: Date Picker显示时间的控件 有默认宽高,不用设置数据源和代理 如何 ...
- jquery和css自定义video播放控件
下面介绍一下通过jquery和css自定义video播放控件. Html5 Video是现在html5最流行的功能之一,得到了大多数最新版本的浏览器支持.包括IE9,也是如此.不同的浏览器提供了不同的 ...
- Winform 通过FlowLayoutPanel及自定义的编辑控件,实现快速构建C/S版的编辑表单页面 z
http://www.cnblogs.com/zuowj/p/4504130.html 不论是B/S或是C/S结构类型,无非就是实现可供用户进行查.增.改.删,其中查询用到最多,开发设计的场景 也最为 ...
随机推荐
- db2查看表空间
select substr(tbsp_name,1,20) as 表空间名称,substr(tbsp_content_type,1,10) as 表空间类型,sum(tbsp_total_size_k ...
- GOOGLE影像地图
卫星地图高清 //
- STL学习二:Vector容器
1.Vector容器简介 vector是将元素置于一个动态数组中加以管理的容器. vector可以随机存取元素(支持索引值直接存取, 用[]操作符或at()方法,这个等下会详讲). vector尾部添 ...
- C++中的运算符优先级
1 () [] . ->2 ! ~ -(负号) ++ -- &(取变量地址)* (type)(强制类型) sizeof 3 * / % 4 + ...
- 打造自己的程序员品牌(摘自Infoq)
John Sonmez是Simple Programmer的创始人.作者与程序员,关注于如何让复杂的事情变得简单.他是一位专业的软件开发者.架构师与讲师,感兴趣的领域包括测试驱动开发.如何编写整洁的代 ...
- Enabled AWE
sp_configure RECONFIGURE GO sp_configure RECONFIGURE GO sp_configure RECONFIGURE GO sp_configure REC ...
- PHP之session相关实例教程与经典代码
·php 中cookie和session的用法比较 ·phpmyadmin报错:Cannot start session without errors问题 ·php中cookie与session应用学 ...
- Open multiple Eclipse workspaces on the Mac
This seems to be the supported native method in OS X: cd /Applications/eclipse/ open -n Eclipse.app ...
- AIZU 0009
Prime Number Time Limit : 1 sec, Memory Limit : 65536 KB Japanese version is here Prime Number Write ...
- mac上eclipse上配置hadoop
在mac上安装了eclipse之后,配置hadoop其实跟在linux上配置差不多,只是mac上得eclipse和界面和linux上得有点不同. 一:安装eclipse eclipse得安装比较简单, ...