欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)實現(xiàn)根據(jù)字母快速定位側邊欄

 更新時間:2021年09月13日 17:35:10   作者:Joe_c  
這篇文章主要為大家詳細介紹了Android開發(fā)實現(xiàn)根據(jù)字母快速定位側邊欄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

按首字母對List排列,并根據(jù)首字母快速定位的實現(xiàn),在Android開發(fā)中被大量應用,今天我也來親自實現(xiàn)一下,將這個控件封裝起來,也方便以后的使用。大體上可以分為兩步來實現(xiàn)這個控件:首先使自己的控件繼承于View,并進行圖形繪制;然后根據(jù)觸摸位置計算當前觸摸的字母,并實現(xiàn)回調接口的方法。

下面來進行實踐:

1.創(chuàng)建自己的控件類并繼承于View,注意:不能只聲明含有一個構造參數(shù)Context的構造函數(shù),這樣我們的控件無法在xml文件中調用,因為Android中xml調用控件之間的參數(shù)傳遞是通過構造參數(shù)中的AttributeSet參數(shù)來進行的,沒有這個參數(shù)我們的控件不能在xml中使用。在這里我添加了父類View的三個構造函數(shù),這里只需要調用父類的構造函數(shù)即可,不需要額外的操作。

public QuicLocationBar(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  // TODO Auto-generated constructor stub
 }
 
 public QuicLocationBar(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
 }
 
 public QuicLocationBar(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
 }

2.繪制字符:繪制的部分通過復寫父類的onDraw方法來實現(xiàn),并通過Paint來來繪制

1)首先聲明一個成員變量來保存我們的字符數(shù)組,并初始化一個Paint類的成員變量來幫助我們繪制字符

private String characters[] = { "#", "A", "B", "C", "D", "E", "F", "G",
   "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
   "U", "V", "W", "X", "Y", "Z" };
 private Paint paint = new Paint();

2)根據(jù)總的高度除以字符串數(shù)組的長度來得到每一個字符的高度,然后循環(huán)遍歷整個數(shù)組來繪制字符

@Override
  protected void onDraw(Canvas canvas) {
  // TODO Auto-generated method stub
  super.onDraw(canvas);
  int width = getWidth();
  int height = getHeight();
   int singleHeight = height / characters.length;
  for (int i = 0; i < characters.length; i++) {
   //對paint進行相關的參數(shù)設置
   paint.setColor(getResources().getColor(R.color.myblack));
   paint.setTypeface(Typeface.DEFAULT_BOLD);
   paint.setAntiAlias(true);
   paint.setTextSize(20);
   if (i == choose) {//choose變量表示當前觸摸的字符位置,若沒有觸摸則為-1
    paint.setColor(getResources().getColor(R.color.myred));
    paint.setFakeBoldText(true);
   }
   //計算字符的繪制的位置
   float xPos = width / 2 - paint.measureText(characters[i]) / 2;
   float yPos = singleHeight * i + singleHeight;
   //在畫布上繪制字符
   canvas.drawText(characters[i], xPos, yPos, paint);
   paint.reset();//每次繪制完成后不要忘記重制Paint
  }
 }

注意:不要忘記在每次繪制完成后重置Paint

3.處理觸摸事件:通過復寫父類的dispatchTouchEvent方法來實現(xiàn)

1)首先我們要設計一個回調接口,當我們觸摸的字符發(fā)生改變時可以執(zhí)行該回調接口的方法

public interface OnTouchLetterChangedListener {
  public void touchLetterChanged(String s);
 }

2)當發(fā)生按下事件或移動事件時,我們根據(jù)觸摸點的位置計算出當前觸摸的字符,如果和我們顯示的字符不相同則執(zhí)行回調接口的方法,并進行View的重繪;當發(fā)生抬起事件時我們將當前顯示的字符更新為-1,表示當前沒有字符顯示,并進行View的重繪。

@Override
  public boolean dispatchTouchEvent(MotionEvent event) {
  int action = event.getAction();
  float y = event.getY();
  int c = (int) (y / getHeight() * characters.length);
 
  switch (action) {
  case MotionEvent.ACTION_UP:
   choose = -1;//
   setBackgroundColor(0x0000);
   invalidate();
   break;
 
  case MotionEvent.ACTION_DOWN:
  case MotionEvent.ACTION_MOVE:
   setBackgroundColor(getResources().getColor(R.color.darkgray));
   if (choose != c) {
    if (c >= 0 && c < characters.length) {
     if (mOnTouchLetterChangedListener != null) {
      mOnTouchLetterChangedListener
        .touchLetterChanged(characters[c]);
     }
     choose = c;
     invalidate();
    }
   }
   break;
  }
  return true;//返回true表示觸摸事件不在向下分發(fā)
 }

附上整體源碼:

package com.example.test.widget;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
 
import com.example.gymapp.R;
 
public class QuicLocationBar extends View {
 
 private String characters[] = { "#", "A", "B", "C", "D", "E", "F", "G",
   "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
   "U", "V", "W", "X", "Y", "Z" };
 private int choose = -1;
 private Paint paint = new Paint();
 private OnTouchLetterChangedListener mOnTouchLetterChangedListener;
 
 public QuicLocationBar(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  // TODO Auto-generated constructor stub
 }
 
 public QuicLocationBar(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
 }
 
 public QuicLocationBar(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
 }
 
 public void setOnTouchLitterChangedListener(
   OnTouchLetterChangedListener onTouchLetterChangedListener) {
  this.mOnTouchLetterChangedListener = onTouchLetterChangedListener;
 }
 
 @Override
 protected void onDraw(Canvas canvas) {
  // TODO Auto-generated method stub
  super.onDraw(canvas);
  int width = getWidth();
  int height = getHeight();
  int singleHeight = height / characters.length;
  for (int i = 0; i < characters.length; i++) {
   // 對paint進行相關的參數(shù)設置
   paint.setColor(getResources().getColor(R.color.myblack));
   paint.setTypeface(Typeface.DEFAULT_BOLD);
   paint.setAntiAlias(true);
   paint.setTextSize(20);
   if (i == choose) {// choose變量表示當前顯示的字符位置,若沒有觸摸則為-1
    paint.setColor(getResources().getColor(R.color.myred));
    paint.setFakeBoldText(true);
   }
   // 計算字符的繪制的位置
   float xPos = width / 2 - paint.measureText(characters[i]) / 2;
   float yPos = singleHeight * i + singleHeight;
   // 在畫布上繪制字符
   canvas.drawText(characters[i], xPos, yPos, paint);
   paint.reset();// 每次繪制完成后不要忘記重制Paint
  }
 }
 
 
 @Override
 public boolean dispatchTouchEvent(MotionEvent event) {
  int action = event.getAction();
  float y = event.getY();
  int c = (int) (y / getHeight() * characters.length);
 
  switch (action) {
  case MotionEvent.ACTION_UP:
   choose = -1;//
   setBackgroundColor(0x0000);
   invalidate();
   break;
 
  case MotionEvent.ACTION_DOWN:
  case MotionEvent.ACTION_MOVE:
   setBackgroundColor(getResources().getColor(R.color.darkgray));
   if (choose != c) {
    if (c >= 0 && c < characters.length) {
     if (mOnTouchLetterChangedListener != null) {
      mOnTouchLetterChangedListener
        .touchLetterChanged(characters[c]);
     }
     choose = c;
     invalidate();
    }
   }
   break;
  }
  return true;
 }
 
 public interface OnTouchLetterChangedListener {
  public void touchLetterChanged(String s);
 }
 
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論