亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android側邊欄滑動切換的view效果

發布時間:2020-09-28 11:45:54 來源:腳本之家 閱讀:163 作者:cmwly 欄目:移動開發

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

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側邊欄滑動切換的view效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

包头市| 邛崃市| 静安区| 蒙城县| 肇州县| 隆德县| 宜章县| 文安县| 枝江市| 清镇市| 介休市| 高密市| 呼图壁县| 邯郸市| 利川市| 洱源县| 武清区| 万载县| 东台市| 黄山市| 民县| 沅江市| 云梦县| 新建县| 义乌市| 天长市| 临汾市| 青海省| 洪江市| 开原市| 临潭县| 文昌市| 虎林市| 隆子县| 凌源市| 建水县| 陈巴尔虎旗| 武强县| 赤壁市| 庄浪县| 北宁市|