RSS订阅悠然探索,悠然索取
你的位置:首页 » Android » 正文

LcTab

选择字号: 超大 标准 发布时间:2011-3-25 14:56:46 | 作者:admin | 0个评论 | 人浏览

package com.lc.payment.common;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.widget.ImageView;

//MainActivity这个类里面的两个static类,看我写的注释和上篇博客的内容应该都能理解。
//其中还定义了触摸事件,实现点击tab出现不同布局的效果。接下来我们只需要在我们的layout上添加就可以了
public class LcTab extends ImageView {
 private Paint mPaint; // 背景画笔
 private Paint mActiveTextPaint;// 选中
 private Paint mInactiveTextPaint;// 未选中
 private ArrayList<TabMember> mTabMembers;// tab成员
 private int mActiveTab;
 private OnTabClickListener mOnTabClickListener = null;

 private static final float GESTURE_THRESHOLD_DIP = 16.0f;
 final float scale = getContext().getResources().getDisplayMetrics().density;

 public LcTab(Context context, AttributeSet attrs) // 构造器,View下构造器有三种方式,在xml中配置必须实现这种方式
 {
  super(context, attrs);

  mTabMembers = new ArrayList<LcTab.TabMember>();

  mPaint = new Paint();
  mActiveTextPaint = new Paint();// Paint类包含样式和颜色有关如何绘制几何形状,文本和位图的信息
  mInactiveTextPaint = new Paint();

  mPaint.setStyle(Paint.Style.FILL_AND_STROKE); // 设置样式
  mPaint.setColor(0xFFFFFF00);// 设置颜色

  Typeface font = Typeface.DEFAULT_BOLD;// (0);//l.create("",Typeface.BOLD);

  mActiveTextPaint.setTextAlign(Align.CENTER);// 设置文本对齐
  mActiveTextPaint.setTextSize((int) (18.0f * scale + 0.5f));// 设置大小
  mActiveTextPaint.setTypeface(font);
  mActiveTextPaint.setAntiAlias(true);
  mActiveTextPaint.setColor(0xFFFFFFFF);// 设置颜色
  // mActiveTextPaint.setFakeBoldText( true );//仿“粗体”设置
  // mActiveTextPaint.setStyle(style)
  mInactiveTextPaint.setTextAlign(Align.CENTER);// 设置文本对齐
  mInactiveTextPaint.setTextSize((int) (16.0f * scale + 0.5f));// 设置大小
  mInactiveTextPaint.setTypeface(font);
  mInactiveTextPaint.setAntiAlias(true);
  mInactiveTextPaint.setColor(0xFFFFFFFF);// 设置颜色
  // mInactiveTextPaint.setFakeBoldText( false );//仿“粗体”设置

  mActiveTab = 0;
 }

 protected Bitmap decodeResource(Resources resources, int id) {
  TypedValue value = new TypedValue();
  resources.openRawResource(id, value);
  BitmapFactory.Options opts = new BitmapFactory.Options();
  opts.inTargetDensity = value.density;
  // return BitmapFactory.decodeResource(resources, id, opts);
  return BitmapFactory.decodeResource(resources, id);
 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  Rect r = new Rect();
  this.getDrawingRect(r);
  // 计算每个标签能使用多少像素
  int singleTabWidth = r.right
    / (mTabMembers.size() != 0 ? mTabMembers.size() : 1);
  // 绘制背景
  // canvas.drawColor( 0xFF000000 );
  mPaint.setColor(0xFF434343);
  canvas.drawLine(r.left, r.top + 1, r.right, r.top + 1, mPaint);
  int color = 46;

  for (int i = 0; i < 24; i++) {
   mPaint.setARGB(255, color, color, color);
   // canvas.drawRect( r.left, r.top + i + 1, r.right, r.top + i + 2,
   // mPaint );
   color--;
  }

  // 绘制每一个tab
  for (int i = 0; i < mTabMembers.size(); i++) {
   TabMember tabMember = mTabMembers.get(i);
   // Bitmap icon = decodeResource( getResources(),
   // tabMember.getIconResourceId());
   // Bitmap iconColored = decodeResource( getResources(),
   // tabMember.getIconResourceId() );

   Paint p = new Paint(Paint.ANTI_ALIAS_FLAG
     | Paint.FILTER_BITMAP_FLAG);
   Canvas iconCanvas = new Canvas();
   // iconCanvas.setBitmap( iconColored );
   if (mActiveTab == i)// 为已选中的tab绘制一个白蓝的渐变色,未选中的绘制一个白灰的渐变色
   {
    // p.setShader( new LinearGradient( 0, 0, icon.getWidth(),
    // icon.getHeight(), 0xFFFFFFFF, 0xFF54C7E1,
    // Shader.TileMode.CLAMP ) );
   } else {
    // p.setShader( new LinearGradient( 0, 0, icon.getWidth(),
    // icon.getHeight(), 0xFFA2A2A2, 0xFF5F5F5F,
    // Shader.TileMode.CLAMP ) );
   }

   // iconCanvas.drawRect( 0, 0, icon.getWidth( ), icon.getHeight( ), p
   // );
   // 计算tab图片的位置
   // int tabImgX = singleTabWidth * i + ( singleTabWidth / 2 -
   // icon.getWidth( ) / 2 );

   // 绘制tab图片 选中的和未选中的
   if (mActiveTab == i) {
    mPaint.setARGB(37, 255, 255, 255);
    canvas.drawRoundRect(new RectF(r.left + singleTabWidth * i + 3,
      r.top + 3, r.left + singleTabWidth * (i + 1) - 3,
      r.bottom - 2), 5, 5, mPaint);
    // canvas.drawBitmap( iconColored, tabImgX , r.top + 5, null );
    canvas
      .drawText(tabMember.getText(), singleTabWidth * i
        + (singleTabWidth / 2), r.bottom - 14,
        mActiveTextPaint);
   } else {
    // canvas.drawBitmap( icon, tabImgX , r.top + 5, null );
    canvas.drawText(tabMember.getText(), singleTabWidth * i
      + (singleTabWidth / 2), r.bottom - 14,
      mInactiveTextPaint);
   }

  }
 }

 /*           * 触摸事件 */
 @Override
 public boolean onTouchEvent(MotionEvent motionEvent) {
  Rect r = new Rect();
  this.getDrawingRect(r);
  float singleTabWidth = r.right
    / (mTabMembers.size() != 0 ? mTabMembers.size() : 1);

  int pressedTab = (int) ((motionEvent.getX() / singleTabWidth) - (motionEvent
    .getX() / singleTabWidth) % 1);

  mActiveTab = pressedTab;

  if (this.mOnTabClickListener != null) {
   this.mOnTabClickListener.onTabClick(mTabMembers.get(pressedTab)
     .getId());
  }

  this.invalidate();

  return super.onTouchEvent(motionEvent);
 }

 public void addTabMember(TabMember tabMember) {
  mTabMembers.add(tabMember);
 }

 public void setOnTabClickListener(OnTabClickListener onTabClickListener) {
  mOnTabClickListener = onTabClickListener;
 }

 // 处理tab成员
 public static class TabMember {
  protected int mId;
  protected String mText;
  protected int mIconResourceId;

  public TabMember(int Id, String Text, int iconResourceId) {
   mId = Id;
   mIconResourceId = iconResourceId;
   mText = Text;
  }

  public int getId() {
   return mId;
  }

  public String getText() {
   return mText;
  }

  public int getIconResourceId() {
   return mIconResourceId;
  }

  public void setText(String Text) {
   mText = Text;
  }

  public void setIconResourceId(int iconResourceId) {
   mIconResourceId = iconResourceId;
  }
 }

 public static interface OnTabClickListener {
  public abstract void onTabClick(int tabId);
 }
}
 

标签:

猜你喜欢

发表评论

必填

选填

选填

必填,不填不让过哦,嘻嘻。

记住我,下次回复时不用重新输入个人信息

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。