Android側(cè)邊欄滑動切換的view效果

廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:

專注于為中小企業(yè)提供網(wǎng)站建設(shè)、網(wǎng)站設(shè)計服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)彝良免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class SideBar extends View {
  private static final int MARGIN_BOTTOM = 2;
  private int mItemHeight = 18;
  private OnLetterTouchListener mOnLetterTouchListener;
  private Paint mPaint;
  private float mWidthCenter;
  private int backgroundColor = 0x00F0F0F0;
  private char[] chars = AppCommon.INDEXER_CHARS;
  public interface OnLetterTouchListener {
    public abstract void onPressedDown(int index, char ch);
    public abstract void onPressedUp(int index, char ch);
  }
  public SideBar(Context context) {
    this(context, null);
  }
  public SideBar(Context context, AttributeSet attrs) {
    this(context, attrs, -1);
  }
  public SideBar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }
  private int startY = 0;//其實位置的y軸坐標
  public SideBar setChars(char[] chars) {
    this.chars = chars;
//   for(int i = 0 ; i < (26 - chars.length) / 2 ; i++){
//     startY += i * mItemHeight;
//   }
    init();
    postInvalidate();
    return this;
  }
  private void init() {
    mPaint = new Paint();
    mPaint.setColor(0xFFCFCFD0);
    mPaint.setAntiAlias(true);
    mPaint.setFakeBoldText(true);
    mPaint.setTextSize(32);
    mPaint.setColor(getResources().getColor(R.color.blue));
    mPaint.setSubpixelText(true);
    mPaint.setTextAlign(Paint.Align.CENTER);
    setBackgroundColor(backgroundColor);
  }
  public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);
    int idx = (int) event.getY() / mItemHeight;
    if (idx >= chars.length) {
      idx = chars.length - 1;
    } else if (idx < 0) {
      idx = 0;
    }
    if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
      setBackgroundResource(R.color.sidebar_select_background);
      if (mOnLetterTouchListener != null) {
        mOnLetterTouchListener.onPressedDown(idx, chars[idx]);
      }
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
      setBackgroundDrawable(new BitmapDrawable());
      setBackgroundColor(backgroundColor);
      if (mOnLetterTouchListener != null) {
        mOnLetterTouchListener.onPressedUp(idx, chars[idx]);
      }
    }
    return true;
  }
  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    mItemHeight = (h - MARGIN_BOTTOM) / 26;
    mWidthCenter = getMeasuredWidth() / 2;
    super.onSizeChanged(w, h, oldw, oldh);
  }
  protected void onDraw(Canvas canvas) {
    for (int i = 0; i < chars.length; i++) {
      canvas.drawText(String.valueOf(chars[i]), mWidthCenter,startY + mItemHeight + (i * mItemHeight), mPaint);
    }
    super.onDraw(canvas);
  }
  public void setOnLetterTouchListener(OnLetterTouchListener listener) {
    mOnLetterTouchListener = listener;
  }
}

以上所述是小編給大家介紹的Android側(cè)邊欄滑動切換的view效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!

當前名稱:Android側(cè)邊欄滑動切換的view效果
URL分享:http://muchs.cn/article34/ijdhse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)站策劃、動態(tài)網(wǎng)站微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)