自定义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结构类型,无非就是实现可供用户进行查.增.改.删,其中查询用到最多,开发设计的场景 也最为 ...
随机推荐
- .NET中 MEF应用于IOC
IOC解释 IOC,控制反转的意思.所谓依赖,从程序的角度看,就是比如A要调用B的方法,那么A就依赖于B,反正A要用到B,则A依赖于B.所谓反转,你必须理解如果不反转,会怎么着,因为A必须要有B,才可 ...
- 初解DLL基本知识
1.DLL基本理论 在Windows操作系统中,几乎所有的内容都是以DLL的形式存在的. 1.DLL基本概念 语言程序要从目标代码(.obj)外部引用函数,可以通过俩种途径实现——静态链接和动态链接. ...
- FMS (端口问题)如何穿透防火墙
转自http://www.cnblogs.com/zhchongyao/archive/2010/01/22/1653803.html 先是管理端口,就是fms2_console文件连接到server ...
- mysql数据恢复
[1] 当数据库被删除后的恢复方法 首先建立一个测试用的数据库. mysql -u root -p123123 ← 用root登录到MySQL服务器 Enter password: ← ...
- 01-04-02【Nhibernate (版本3.3.1.4000) 出入江湖】HQL查询
public IList<Customer> GetAllHql() { IList<Customer> result = null; ISession session = _ ...
- 设置HTTP header方式
一, Server Code JSP----> <%@ page language="java" contentType="text/html; charse ...
- jQuery中的&& ||
jQuery1.2.6 clean方法中有这么一段第一眼看去会让人晕掉的方法.完全不知其所言. “||, && 可以这样用?”,“这段东西最终返回的是个什么对象啊?” // Trim ...
- 同一机器 部署 两个 jboss
当jboss和oracle在同一机器上时,通常oracle占用8080端口,这时只需要去修改\deploy\jbossweb-tomcat50.sar\server.xml中.当在同一台机器上运行两个 ...
- linux源代码阅读笔记 free_page_tables()分析
/* 77 * This function frees a continuos block of page tables, as needed 78 * by 'exit()'. As does co ...
- 【面试题032】从1到n整数中1出现的次数
[面试题032]从1到n整数中1出现的次数 题目: 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数. 例如输入12,从1到12这些整数中包含1的数字有1,10,11和1 ...